MySQL出现root用户密码错误,或者修改root用户默认密码

root 用户输入密码出现错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

步骤:

1. 打开管理员窗口界面输入cmd命令:net stop mysql  将MySQL服务停止

2. 接着输入 mysqld --shared-memory --skip-grant-tables  如下图(root用户可以不用输入密码,mysqld --skip-grant-tables这个命令我试了,但是没有发现作用):

 

3. 再打开一个管理员cmd窗口,输入 mysql -uroot -p ,直接回车,不用输入密码,进入mysql

4. 输入:use mysql

5. 接着输入:select host, user, authentication_string, plugin from user;

注意:该命令可以查看root用户对应及其他用户的信息,此时root用户对应的authentication_string是为空的,如果不为空则使用命令 update user set authentication_string='' where user='root';需要注意的是在8.0版本之后MySQL的password函数设置密码已经被取消,所以,update mysql.user set password='newpassword'  where user='root'; 和 update mysql.user set password=PASSWORD('newpassword') where User='root';这两条命令已经不起作用了。

6.输入命令: alter user 'root'@'localhost' identified by 'newpassword'; 设置密码

7.出现设置密码成功的提示,那么接着输入命令:flush privileges;

8. 设置密码成功之后,启动MySQL服务

9.验证是否设置密码成功,如下图:显示设置密码成功