Mysql8忘记密码解决策略 Linux

第一步:修改my.cnf配置文件

执行 vim /etc/my.conf 命令进入mysql的配置文件

在配置文件默认加入 skip-grant-tables 开启免密登录 看图一 (不需要注释掉)

Mysql8忘记密码解决策略 Linux

第二步:登录mysql

然后执行 systemctl restart mysqld 将mysql进行重启

直接mysql -u root -p即可直接进入mysql

第三步:修改密码

在修改密码之前我们可以执行 select host, user, authentication_string from user;查看一下

  • host :允许用户登录的ip '%'表示远程,localhost本机
  • user:mysql用户
  • authentication_string:加密后的用户密码

我们首先尝试将密码设置为空查看修改密码是否有效执行update user set authentication_string='' where user='root';然后退出mysql。在 vim /etc/my.conf中将 skip-grant-tables注释,再次systemctl restart mysqld重启。

然后进入mysql 执行 alter user 'root'@'localhost' identified by 'new_password' 即可。
注意:localhost不一定 以host关键字为主

如果有什么问题,欢迎大家在评论区留言,大家一起爬坑!