本人环境为MySQL 5.7 + CentOS 6.3。
1. 编辑MySQL配置文件/etc/my.cnf
- 找到[mysqld],在下面添加一行skip-grant-tables
![](https://i-blog.csdnimg.cn/blog_migrate/d57ae998d459e702cfa794367b9a16eb.png)
2. 重启MySQL
- service mysqld restart
![](https://i-blog.csdnimg.cn/blog_migrate/1c735a206f79b8f8e8d9f2f8a920899e.png)
3. root用户登录MySQL,提示密码直接回车
- mysql -uroot -p
![](https://i-blog.csdnimg.cn/blog_migrate/a619408cdad429443183d471f7cab4f1.png)
4. 修改root密码,并刷新权限
- update mysql.user set authentication_string=password('新密码') where user='root' and host='localhost';
- flush privileges;
![](https://i-blog.csdnimg.cn/blog_migrate/1b45edb24811181192a426664f1030ad.png)
5. 退出MySQL,并编辑MySQL配置文件/etc/my.cnf
6. 重启MySQL
- service mysqld restart
注意:如果MySQL是5.5,则修改密码的语句是update mysql.user set password=password('123456') where user='root' and host='localhost';