如何解决MySQL 5.7 Access denied for user 'root'@'localhost' (using password YES)问题?

欢迎转载,转载请注明出处,谢谢~(作者:喝酒不骑马 Colton_Null)

from CSDN


最近,在CentOS 6.8上使用MySQL时,无法登录数据库,并提示Access denied for user ‘root’@’localhost’ (using password:YES)
经过查阅大量资料,现总结一下可用的解决方法

1.首先关闭MySQL服务

[root@localhost /]# service mysqld stop

2.安全启动MySQL(跳过密码验证)

[root@localhost /]# /usr/bin/mysqld_safe --skip-grant-tables

这时候会报

mysqld_safe Logging to '/var/lib/mysql/iZ23dq2wm0jZ.err'.
mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

我用SecureCRT连接的Linux,到这时候就卡住了,ctrl + c也不能退出。后来找到解决方法:卡住时,在用SecureCRT开启一个新的连接到系统上继续操作就可以了。

3.登录MySQL

[root@localhost /]#  mysql -u root

4.更改密码

mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123465' with grant option;

其中’root’@’localhost’ 是用户名
第二个’123465’是新密码

如果此处报错The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
则说明没有写入权限,这时候就要关闭数据库只读属性

mysql> set global read_only=0;// 关掉数据库的只读属性
mysql>flush privileges;// 刷新配置

之后,再执行修改密码操作
最后,可以再将属性设置回初始状态

mysql> set global read_only=1;// 关掉数据库的只读属性
mysql>flush privileges;// 刷新配置

5.重启数据库

[root@localhost /]# service mysqld restart

这时候,就可以用刚才设置的新密码123465登录数据库了。问题解决。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值