mysql5.7忘记root密码

1、编辑mysql配置文件/etc/my.cnf,在[mysqld]中添加
skip-grant-tables

2、重启mysql服务
/etc/init.d/mysqld restart

3、用户无密码登录
mysql -uroot -p (直接点击回车,密码为空)

4、选择数据库
use mysql;

5、修改root密码
update user set authentication_string=password(‘root’) where user=’root’;
到这里,提示报错:
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
备注:
mysql 5.7版本及以后的版本的password字段更改成了authentication_string,5.7之前的版本是password

6、根据提示,是因为MySql运行在safe-updates模式下,该模式会导致非主键条件下无法执行update或者delete命令。查看开关状态:
show variables like ‘SQL_SAFE_UPDATES’;

7、修改数据库模式
SET SQL_SAFE_UPDATES = 0;

8、再次执行update修改密码:
update user set authentication_string=password(‘root’) where user=’root’;

9、刷新
flush privileges;

10、在配置文件中删除skip-grant-tables,重启mysql服务