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

来源:互联网 发布:got it get it gotcha 编辑:程序博客网 时间:2024/06/15 16:17

欢迎转载,转载请注明出处,谢谢~(作者:喝酒不骑马 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登录数据库了。问题解决。

阅读全文
0 0
原创粉丝点击