【踩坑】Mysql忘记密码

前言

Mysql忘记密码,或者安装后没有找到默认密码无法登录的解决办法

忘记密码

  • 编辑配置文件
1
vim /etc/my.cnf
  • 修改配置文件,在[mysqld]区域添加代码启动安全模式
1
skip-grant-tables
  • 重启mysql服务后,直接无密码登录mysql进行改密码(弹出密码提示直接回车)
1
mysql -uroot -p
  • 修改密码
1
2
USE mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

再次踩坑

  • 在安全模式下登录的Mysql在修改密码时报错:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

解决问题

  • 关闭只读权限
1
2
set global read_only=0;
flush privileges;

完成

参考文献

CSDN——Mr.路痴