MySQL8.0忘记root密码解决方法

1.跳过密码进入mysql

(1)管理员权限打开cmd,先暂停mysql服务
C:\Windows\system32>net stop mysql
(2)进入mysql安装目录bin目录,运行命令mysqld --console --skip-grant-tables --shared-memory
在这里插入图片描述
(3)运行成功时,打开另外一个cmd窗口进入mysql
C:\Windows\system32>mysql -uroot -p
不需要输入密码直接Enter

2.修改密码

(1)刷新权限

mysql>flush privileges

(2)查看authentication_string是否为空,authentication_string字段用于存储加密后的密码

mysql>select user,host,authentication_string from user

(3)authentication_string字段为空的话就可以用ALTER USER修改,不能的话先将该字段设为空,否则可能会报错 ERROR 1396 (HY000): Operation ALTER USER failed for ‘root’@'localhost’

use mysql;
update user set authentication_string=’’ where user='root’

(4)修改密码,若需要支持navicat,使用mysql_native_password修改密码,毕竟8.0加密方法不一样

ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密码’;

不需要支持navicat:
ALTER user ‘root’@‘localhost’ IDENTIFIED BY '新密码’

修改完毕 退出第一个cmd命令,重启服务即可

你可能感兴趣的:(mysql)