本博客由 [Pipe](https://github.com/b3log/pipe) 强力驱动

忘记 mysql 的 root 密码怎么找回

首先找到mysqld的路径

which mysqld

image.png

敲命令查看默认的mysql配置文件位置 /usr/sbin/mysqld --verbose --help |grep -A 1 'Default options'

image.png

打开配置文件my.cnf,并添加 skip-grant-tables,跳过权限的检查

用空密码连接数据库,并修改数据库的root密码

mysql -u root

mysql> set password = password('390032540');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> update user set password=password('390032540') where user='root' and host='localhost';
ERROR 1046 (3D000): No database selected
mysql> use mysql;
Database changed
mysql> update user set password=password('new password') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

然后刷新权限表

flush privileges;

再重新登录的时候就必须输入密码了

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

最后,不要忘记把刚才那个跳过权限检查的配置给删掉

留下你的脚步