如何在Linux(CentOS)下重置MySQL根(Root)密码

来自YTYZX有图有真相的百科
45.77.171.9讨论2022年3月22日 (二) 22:34的版本 (e)
跳转至: 导航搜索

本例中以CentOS6.6下修改MySQL5.1.73及CentOS7修改MySQL5.7.20(同样适用于MySQL8)举例说明。

e

CentOS7下修改MySQL5.7.20(同样适用于MySQL8)

MySQL5.7.6及更新版本使用此方法。
13.因为忘记密码故提示以下错误。
     错误提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

MySQLRoot13.png

14.输入“systemctl stop mysqld”停止MySQL服务。

MySQLRoot14.png

15.输入“systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"”设置为“--skip-grant-tables”模式。
     备注:sudo mysqld_safe --skip-grant-tables --skip-networking &

MySQLRoot15.png

16.输入“systemctl start mysqld”启动MySQL服务。

MySQLRoot16.png

17.输入“mysql -u root”免密码直接登录MySQL。

MySQLRoot17.png

18.输入“ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lq^2017ii';”尝试修改密码,提示以下错误。
    错误提示: ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

MySQLRoot18.png

19.输入“FLUSH PRIVILEGES;”刷新权限。

MySQLRoot19.png

20.再次输入“ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lq^2017ii';”,提示已经成功将服务器localhost上MySQL数据库root用户密码修改为Lq^2017ii。

MySQLRoot20.png

21. 再次输入“FLUSH PRIVILEGES;”刷新权限。

MySQLRoot19.png

22.输入“quit”退出MySQL。

MySQLRoot21.png

23.输入“systemctl restart mysqld”重启MySQL服务。

MySQLRoot22.png

24.输入“mysql -u root -p”并使用新密码(Lq^2017ii)登录即可。

MySQLRoot23.png