MySQL5.7忘记密码 Linux

  1. 修改my.cnf

在数据库配置文件my.cnf的[mysqld]下面添加skip-grant-tables,忽略授权表

vim /etc/my.cnf

[mysqld]
skip-grant-tables
  1. 重启并登陆MySQL
[root@localhost etc]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 

#不输入密码直接回车
[root@localhost etc]# mysql -p
  1. 修改root密码

用不了grant语句改密码

mysql>update user set authentication_string = password('123456'), password_expired = 'N', password_last_changed = now() where User = 'root';
mysql>quit
  1. 恢复正常模式

删除数据库配置文件my.cnf的skip-grant-tables,并重启MySQL服务,然后用刚刚改好的密码登陆数据库

原文地址:https://www.cnblogs.com/ningzijie/p/15365632.html