《MySQL密码忘了?别慌!试试这个“黑客级“救援方案》_mysql忘记密码
目录
前言
一、skip-grant-tables重置MySQL密码
1.核心原理
2.具体行为
3.操作实践
二、init-file重置MySQL密码(推荐)
1.核心原理
2.关键特性
3.操作实践
总结
前言
作为数据库管理员,最尴尬的时刻莫过于被自己管理的MySQL数据库拒之门外。忘记root密码就像把钥匙锁在了保险箱里,而--skip-grant-tables
和init-file
就是你的专业开锁工具。本文以MySQL-8.0.41版本为例进行实践讲解。
一、skip-grant-tables重置MySQL密码
1.核心原理
MySQL的权限验证是通过mysql.user
表实现的,而--skip-grant-tables
启动参数会直接 绕过整个权限验证流程,相当于给数据库装了一个\"临时后门\"。
2.具体行为
-
✅ 禁用权限表加载:不读取
mysql.user
、mysql.db
等权限表 -
✅ 允许无密码登录:任何用户(包括匿名用户)都能以超级权限连接
-
✅ 仅限本地连接(默认情况下,防止远程滥用)
3.操作实践
①停用mysql服务
[root@localhost ~]# systemctl stop mysqld[root@localhost ~]# systemctl status mysqld○ mysqld.service - MySQL 8.0 database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; preset: disabled) Active: inactive (dead)6月 24 10:24:25 localhost systemd[1]: Starting MySQL 8.0 database server...6月 24 10:24:26 localhost systemd[1]: Started MySQL 8.0 database server.6月 24 10:34:15 localhost systemd[1]: Stopping MySQL 8.0 database server...6月 24 10:34:16 localhost systemd[1]: mysqld.service: Deactivated successfully.6月 24 10:34:16 localhost systemd[1]: Stopped MySQL 8.0 database server.6月 24 10:34:29 localhost systemd[1]: Starting MySQL 8.0 database server...6月 24 10:34:31 localhost systemd[1]: Started MySQL 8.0 database server.6月 24 14:37:07 localhost systemd[1]: Stopping MySQL 8.0 database server...6月 24 14:37:09 localhost systemd[1]: mysqld.service: Deactivated successfully.6月 24 14:37:09 localhost systemd[1]: Stopped MySQL 8.0 database server.[root@localhost ~]# ps -ef | grep mysqldroot 3763535 330474 0 14:37 pts/2 00:00:00 grep --color=auto mysqld[root@localhost ~]#
②编辑mysql配置文件,添加skip-grant-tables参数
[root@localhost ~]# vi /etc/my.cnf.d/mysql-server.cnf # 默认配置文件路径
在配置文件中的[mysqld]配置项
中添加内容:skip-grant-tables
,添加后保存即可。
③重启数据库,无密码登录验证
[root@localhost ~]# systemctl restart mysqld
命令行测试:mysql -uroot;
成功跳过了密码认证,进入到了MySQL命令行,接下来进行密码修改。
④root密码修改
mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)mysql> alter user \'root\'@\'localhost\' identified by \'Mydb@135246\';Query OK, 0 rows affected (0.03 sec)mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)
如上图修改密码成功,将MySQL配置文件中的skip-grant-tables配置注释或删除重启MySQL服务进行新密码登录验证。
[root@localhost ~]# systemctl restart mysqld
注:修改密码之前一定要flush privileges 刷新权限,否则会报错;如下:
mysql> alter user \'root\'@\'localhost\' identified by \'Mydb@135246\';ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
⑤验MySQL证新密码登录
[root@localhost ~]# mysql -u root -pMydb@135246Welcome to the MySQL monitor. Commands end with ; or \\g.Your MySQL connection id is 7Server version: 8.0.41 Source distributionCopyright (c) 2000, 2025, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.mysql>
成功进入,密码修改成功!
二、init-file重置MySQL密码(推荐)
1.核心原理
init-file
参数让MySQL在启动时 自动执行指定文件中的SQL语句,且这些语句会在权限系统初始化之前执行。
2.关键特性
-
🚀 优先于权限检查:SQL在权限系统生效前执行
-
📜 支持多语句:可包含
ALTER USER
、UPDATE mysql.user
等操作 -
⏱️ 一次性执行:仅在服务启动时运行一次
3.操作实践
①停用mysql服务
[root@localhost ~]# systemctl stop mysqld
②创建修改密码sql文件
[root@localhost mysql]# vi /var/lib/mysql/mysql-reset-pwd.sql
填写如下sql语句,然后保存。
ALTER USER \'root\'@\'localhost\' IDENTIFIED BY \'MyDB@0678\';
注:密码设置不能过于简单化,需8字符以上、包含大小写字母和特殊字符为佳。
给予sql文件执行权限:
[root@localhost mysql]# chmod +x /var/lib/mysql/mysql-reset-pwd.sql
③在MySQL配置文件添加参数指定启动时执行编写修改密码的sql文件并重启数据库
[root@localhost mysql]# vi /etc/my.cnf.d/mysql-server.cnf # MySQL默认配置文件路径
在配置文件中的[mysqld]
中添加init-file内容,文件路径记得更换为自己实际的路径,确保编写的sql文件及文件所在的目录拥有足够的权限。
④配置完成之后,systemctl restart mysqld 重启MySQL服务。
⑤验证新密码登录
[root@localhost ~]# mysql -uroot -pMyDB@0678mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \\g.Your MySQL connection id is 9Server version: 8.0.41 Source distributionCopyright (c) 2000, 2025, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.mysql>
成功进入MySQL命令行,密码修改成功!
总结
掌握--skip-grant-tables
和init-file
方法等于拥有了MySQL的万能钥匙,但请记住:
\"With great power comes great responsibility\"
——能力越大,责任越大。仅在必要时使用此方法,并始终遵循安全最佳实践。