"如何在Linux(CentOS)下重置MySQL根(Root)密码" 修訂間的差異
從 YTYZX有图有真相的百科
(创建页面,内容为“ 1.首先查看服务”) |
小 (恢复45.77.171.9(讨论)的编辑至Ytyzx的最后版本) |
||
(未顯示由 4 位使用者於中間所作的 12 次修訂) | |||
行 1: | 行 1: | ||
− | 1.首先查看服务 | + | 本例中以CentOS6.6下修改MySQL5.1.73及CentOS7修改MySQL5.7.20(同样适用于MySQL8)举例说明。 |
+ | __TOC__ | ||
+ | == Cent0S 6.6下修改MySQL5.1.73 == | ||
+ | MySQL5.7.5或较旧版本使用此方法。 | ||
+ | 1.首先 输入“service mysqld status” 查看 当前mysql服务状态,下图显示正在mysqld服务正在运行中。 | ||
+ | [[File:MySQLRoot1.png]] | ||
+ | 2.输入“killall -TERM mysqld”命令停止所有的mysqld进程。 | ||
+ | [[File:MySQLRoot2.png]] | ||
+ | 3.输入“service mysqld stop”命令停止mysqld 服务 。 | ||
+ | [[File:MySQLRoot3.png]] | ||
+ | 4.输入“mysqld_safe --skip-grant-tables &”命令以无密码方式进入MySQL安全模式。 | ||
+ | [[File:MySQLRoot4.png]] | ||
+ | 5.输入“mysql -u root”并按回车键即可。 | ||
+ | [[File:MySQLRoot5.png]] | ||
+ | 6.输入“use mysql;”挂载数据库。 | ||
+ | 注意:请勿忘记在最后输入分号(;)。 | ||
+ | [[File:MySQLRoot6.png]] | ||
+ | 7.输入"update user set password=password("New9ssw0rd") where user='root';"将Root密码修改为New9ssw0rd。 | ||
+ | [[File:MySQLRoot7.png]] | ||
+ | 8.输入"flush privileges;"更新权限。 | ||
+ | [[File:MySQLRoot8.png]] | ||
+ | 9.输入“quit”并按回车键退出。 | ||
+ | 注意:此处不需输入分号。 | ||
+ | [[File:MySQLRoot9.png]] | ||
+ | 10.输入"service mysqld restart"重启mysqld服务。 | ||
+ | [[File:MySQLRoot10.png]] | ||
+ | 11.输入“mysql -u root -p”并按回车键提示输入密码。 | ||
+ | [[File:MySQLRoot11.png]] | ||
+ | 12.输入新密码New9ssw0rd并按回车键,提示已经成功登录。 | ||
+ | [[File:MySQLRoot12.png]] | ||
+ | |||
+ | == CentOS7下修改MySQL5.7.20(同样适用于MySQL8) == | ||
+ | MySQL5.7.6及更新版本使用此方法。 | ||
+ | 13.因为忘记密码故提示以下错误。 | ||
+ | 错误提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) | ||
+ | [[File:MySQLRoot13.png]] | ||
+ | 14.输入“systemctl stop mysqld”停止MySQL服务。 | ||
+ | [[File:MySQLRoot14.png]] | ||
+ | 15.输入“systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"”设置为“--skip-grant-tables”模式。 | ||
+ | 备注:sudo mysqld_safe --skip-grant-tables --skip-networking & | ||
+ | [[File:MySQLRoot15.png]] | ||
+ | 16.输入“systemctl start mysqld”启动MySQL服务。 | ||
+ | [[File:MySQLRoot16.png]] | ||
+ | 17.输入“mysql -u root”免密码直接登录MySQL。 | ||
+ | [[File: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 | ||
+ | [[File:MySQLRoot18.png]] | ||
+ | 19.输入“FLUSH PRIVILEGES;”刷新权限。 | ||
+ | [[File:MySQLRoot19.png]] | ||
+ | 20.再次输入“ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lq^2017ii';”,提示已经成功将服务器localhost上MySQL数据库root用户密码修改为Lq^2017ii。 | ||
+ | [[File:MySQLRoot20.png]] | ||
+ | 21. 再次输入“FLUSH PRIVILEGES;”刷新权限。 | ||
+ | [[File:MySQLRoot19.png]] | ||
+ | 22.输入“quit”退出MySQL。 | ||
+ | [[File:MySQLRoot21.png]] | ||
+ | 23.输入“systemctl restart mysqld”重启MySQL服务。 | ||
+ | [[File:MySQLRoot22.png]] | ||
+ | 24.输入“mysql -u root -p”并使用新密码(Lq^2017ii)登录即可。 | ||
+ | [[File:MySQLRoot23.png]] |
於 2022年3月24日 (四) 20:48 的最新修訂
本例中以CentOS6.6下修改MySQL5.1.73及CentOS7修改MySQL5.7.20(同樣適用於MySQL8)舉例說明。
Cent0S 6.6下修改MySQL5.1.73
MySQL5.7.5或较旧版本使用此方法。 1.首先输入“service mysqld status”查看当前mysql服务状态,下图显示正在mysqld服务正在运行中。
2.输入“killall -TERM mysqld”命令停止所有的mysqld进程。
3.输入“service mysqld stop”命令停止mysqld服务。
4.输入“mysqld_safe --skip-grant-tables &”命令以无密码方式进入MySQL安全模式。
5.输入“mysql -u root”并按回车键即可。
6.输入“use mysql;”挂载数据库。 注意:请勿忘记在最后输入分号(;)。
7.输入"update user set password=password("New9ssw0rd") where user='root';"将Root密码修改为New9ssw0rd。
8.输入"flush privileges;"更新权限。
9.输入“quit”并按回车键退出。 注意:此处不需输入分号。
10.输入"service mysqld restart"重启mysqld服务。
11.输入“mysql -u root -p”并按回车键提示输入密码。
12.输入新密码New9ssw0rd并按回车键,提示已经成功登录。
CentOS7下修改MySQL5.7.20(同樣適用於MySQL8)
MySQL5.7.6及更新版本使用此方法。 13.因为忘记密码故提示以下错误。 错误提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
14.输入“systemctl stop mysqld”停止MySQL服务。
15.输入“systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"”设置为“--skip-grant-tables”模式。 备注:sudo mysqld_safe --skip-grant-tables --skip-networking &
16.输入“systemctl start mysqld”启动MySQL服务。
17.输入“mysql -u root”免密码直接登录MySQL。
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
19.输入“FLUSH PRIVILEGES;”刷新权限。
20.再次输入“ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lq^2017ii';”,提示已经成功将服务器localhost上MySQL数据库root用户密码修改为Lq^2017ii。
21. 再次输入“FLUSH PRIVILEGES;”刷新权限。
22.输入“quit”退出MySQL。
23.输入“systemctl restart mysqld”重启MySQL服务。
24.输入“mysql -u root -p”并使用新密码(Lq^2017ii)登录即可。