(二)mysql忘记root密码

(1)mysql 5.7.6以前版本

  • 修改配置文件vim /etc/my.cnf
[mysqld]
skip-grant-tables
  • 重启mysql
systemctl restart mysqld
  • 修改密码和刷新授权表
mysql>update mysql.user set password=password("redhat") where user="root" and host="localhost";
mysql>flush privileges;
  • 再次修改配置文件
#skip-grant-tables
  • 重启mysql
systemctl restart mysqld 

(2)mysql 5.7.6以后的版本

  • 修改配置文件vim /etc/my.cnf
[mysqld]
skip-grant-tables
  • 重启mysql
systemctl restart mysqld 
  • 修改密码和刷新授权表
mysql> select user,host,authentication_string from mysql.user;
mysql> update mysql.user set authentication_string=password('redhat') where user="root" and host="localhost";
mysql> flush privileges;
  • 再次修改配置文件vim /etc/my.cnf
#skip-grant-tables
  • 重启mysql
systemctl restart mysqld 
原文地址:https://www.cnblogs.com/lovelinux199075/p/8878131.html