Linux登录MySQL时报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

环境介绍:
我用的环境为:Centos7、Mysql5.7.26

安装部署完MySQL后通过命令mysql -u root -p登录时报错如下:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Linux登录MySQL时报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)_第1张图片
解决方案:
找到MySQL的配置文件/etc/my.cnf
在此配置文件中添加命令skip-grant-tables实现免密登录

Linux登录MySQL时报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)_第2张图片
添加命令后保存配置文件,重启MySQL服务:systemctl restart mysqld
Linux登录MySQL时报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)_第3张图片
再通过命令mysql -u root -p登录MySQL,输入密码时直接按Enter键登录
登录后修改密码、刷新、退出MySQL

mysql>update mysql.user set authentication_string=password('Mysql@123') where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
 
mysql> 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
 
mysql> quit;
Bye

修改密码退出后将配置文件中前面添加的命令:skip-grant-tables注释或删除
再次重启MySQL服务:systemctl restart mysqld
即可正常登录MySQL

你可能感兴趣的:(常见错误,MySQL)