mysql 8.0数据库忘记root密码

发布时间 2023-11-09 10:34:11作者: 五官一体即忢

最底部有遇到的问题及解决方案,欢迎补充

Windows系统mysql 8.0忘记root密码,修改步骤

第一步: 使用管理员权限打开命令提示符,在命令行输入 net stop mysql,或者直接打开服务,停止mysql服务

第二步: 服务停止后,在命令行中输入mysqld --console --skip-grant-tables --shared-memory,此时该窗口被锁定

image.png

第三步: 打开一个新的命令提示符窗口,输入mysql -uroot -p回车,提示输入密码,直接点回车进入

第四步: 输入 use mysql;

第五步: 输入 update user set authentication_string='' where user='root';,将authentication_string置空,不清空可能引起修改失败,可以尝试不清(未验证过)。

注:在mysql8.0以上版本, update mysql.user set password='newpassword' where user='root'; update mysql.user set password=PASSWORD('newpassword') where User='root';这两条命令已经不起作用了。

第六步: 输入 alter user 'root'@'localhost' identified by 'newpassword'; newpassword是要设的新密码。

第七步: 如果提示设置成功,则输入: flush privileges;(必须刷新) 。完成后Ctrl+Z退出mysql,使用新密码重新登录即可。

第八步: 如果提示不成功,则关闭两个命令提示符,重新打开一个管理员权限的命令提示符窗口,输入: mysql -uroot -p 后回车,输入密码时直接按回车进入(因为此时密码已被置空)。

第九步: 重复步骤4,步骤6,步骤7

 

linux MySQL密码重置

步骤参考Windows第三步开始

 

坑 坑

第一个坑: MySQL关于ERROR 1290 (HY000)报错

解决方案:刷新MySQL权限相关表即可解决-:flush privileges

image.png

 

第二个坑: mysql -uroot -p 登陆mysql提示Character set ‘utf-8’ is not a compiled character set and is not specified

解决方案:
在mysql安装目录下找到my.ini(默认路径D:/Program Files(x86)/MySQL/MySQL Server 8.0), 找到default-character-set将值改成utf8,重新打开命令提示符再次登录即可成功

image.png