Table of Contents

  1. 忘记MySQL的root密码
  2. 编辑MySQL配置文件my.cnf
  3. 进入MySQL控制台
  4. 修改root密码
  5. 取消/etc/my.cnf中的skip-grant-tables
  6. 重启mysql
  7. 进入mysql控制台

忘记MySQL的root密码

编辑MySQL配置文件my.cnf

编辑文件,找到[mysqld],在下面添加一行skip-grant-tables

vi /etc/my.cnf  

[mysqld]
skip-grant-tables

:wq!

保存退出
重启MySQL服务

service mysqld restart 

进入MySQL控制台

mysql -uroot -p

修改root密码

update mysql.user set password=password('123456') where User="root" and Host="localhost";

flush privileges;  #刷新系统授权表

grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;

取消/etc/my.cnf中的skip-grant-tables

编辑文件,找到[mysqld],删除skip-grant-tables这一行

vi /etc/my.cnf

重启mysql

service mysqld restart

进入mysql控制台

mysql -uroot -p  #进入mysql控制台