转载请注明出处:https://blog.csdn.net/l1028386804/article/details/92671953

1.关闭MySQL服务器

service mysqld stop

2.使用--skip-grant-tables选项重启MySQL服务

./bin/mysqld_safe --skip-grant-tables --user=root &

其中--skip-grant-tables选项的意思是启动MySQL服务的时候跳过权限表认证。启动后,连接到MySQL的root将不需要命令。

3.用空密码的root用户连接到MySQ,并且更新root口令:

mysql -uroot

MySQL 5.7以下版本:

update mysql.user set password=password('新密码') where user='root' and host='localhost';

MySQL 5.7版本:

update user set authentication_string=password('新密码') where user='root' and Host='localhost';

4.刷新权限表,使得权限认证重新生效:

flush privileges;

5.重启MySQL数据库

service mysqld restart

6.登录MySQL

mysql -uroot