MySQL -- 04 -- 解决Access denied for user 'root'@'localhost'
当我们在服务器上使用 MySQL,使用 mysql -uroot -p 进行登陆时,有时候会碰到以下错误,踩个坑记录下解决方法

一、跳过密码验证
-
vim /etc/my.cnf
-
skip-grant-tables (在任意空白行加上即可)
-
:wq
-
systemctl restart mysql
二、无密码登陆
- mysql -uroot
MySQL5.7 版本
-
update mysql.user set authentication_string=PASSWORD(‘123456’) where user=‘root’;
-
flush privileges;
-
quit;
MySQL8.0 版本
-
use mysql;
-
alter user ‘root’@’%’ identified with mysql_native_password by ‘123456’;
需要注意的是,执行上面的 sql,可能会报以下错误

这是我们只需要先执行下
flush privileges;,然后再执行上面的 sql 就可以了 -
flush privileges;
-
quit;
三、重启 MySQL 服务
这里我们需要将 my.cnf 给改回去,即去掉之前添加的 skip-grant-tables
- systemctl restart mysql
四、重新登陆
- mysql -uroot
此时我们输入之前设置的密码 123456,即可正常登陆

浙公网安备 33010602011771号