MySQL数据库密码忘记了且没有其他可以修改账号密码的账户时怎么办呢?
登录MySQL,密码输入错误
[rootTESTDB ]# mysql uroot p P3306 Enter password: ERROR (): Access denied user (using password: YES)
如果忘记密码,对于MySQL而言处理起来也相对比较简单。但需要修改配置,重启数据库。可以按照如下步骤处理。
vim etcmy. 添加如下参数 skip_grant_tables
如果部署了服务 可以重启数据库服务重启,如果没有部署,需要杀掉数据库进程,在重新启动数据库。
init.mysqld restart 或 ps efgrep mysql kill
[rootTESTDB ]# mysql uroot P3306 Welcome to the MySQL monitor. end ; or g. MySQL connection id is Server version: log Percona Server (GPL), Release , Revision a9574 Copyright (c) Percona LLC andor its affiliates Copyright (c) , , Oracle andor its affiliates. rights reserved. is a registered trademark of Oracle Corporation andor its affiliates. names may be trademarks of their respective owners. or help. to clear the current input statement.
再修改密码
mysql update mysql. set authentication_stringpassword() where user and host; Query OK, rows affected, warning ( sec) Rows matched: Changed: Warnings: mysql flush privileges; Query OK, rows affected ( sec)
注:
a) 不可以使用set password命令修改密码,只能通过更新数据库表的方式
mysql set passwordpassword(); ERROR (HY000): The MySQL server is running the skipgranttables option so it cannot execute statement
b) 使用update表mysql.user的方式需要flush privileges生效
c) 不同的版本mysql.user的字段以及密码加密方式不同,例如MySQL5.6中密码存储在password中,MySQL8.0中加密方式有变更等,处理时需要根据版本来相应修改脚本处理。
去掉第1步中my.cnf配置文件中添加的skip_grant_tables参数
vim /etc/my.cnf #skip_grant_tables /* 注释掉该参数*/
Mysql5.7中可以直接在MySQL命令行中使用shutdown命令关闭数据库,之后再启动数据库即可。
mysql shutdown; Query OK, rows affected ( sec)
启动后,即可使用重置后的密码登录
[rootTESTDB ]# mysql uroot P3306 p mysql: [Warning] Using a password on the command line interface can be insecure. to the MySQL monitor. end ; or g. MySQL connection id is Server version: log Percona Server (GPL), Release , Revision a9574 Copyright (c) Percona LLC andor its affiliates Copyright (c) , , Oracle andor its affiliates. rights reserved. is a registered trademark of Oracle Corporation andor its affiliates. names may be trademarks of their respective owners. or help. to clear the current input statement.
至此,密码重置完毕。
TIPS: 生产环境的数据库密码一定要妥善保管,虽然可以找回,但需要重启,影响数据库可用性。