MySQL 8 忘记 root 密码?这两种方法帮你轻松解决

MySQL 8 忘记 root 密码?这两种方法帮你轻松解决

    • [📝 前言](#📝 前言)
    • [🎯 解决方案一:使用 skip-grant-tables 参数](#🎯 解决方案一:使用 skip-grant-tables 参数)
      • [步骤 1:修改配置文件](#步骤 1:修改配置文件)
      • [步骤 2:重启 MySQL 服务](#步骤 2:重启 MySQL 服务)
      • [步骤 3:无密码登录 MySQL](#步骤 3:无密码登录 MySQL)
      • [步骤 4:修改 root 密码](#步骤 4:修改 root 密码)
      • [步骤 5:恢复配置文件并重启](#步骤 5:恢复配置文件并重启)
      • [步骤 6:验证新密码](#步骤 6:验证新密码)
    • [🚀 解决方案二:使用 mysqld_safe 安全模式](#🚀 解决方案二:使用 mysqld_safe 安全模式)
      • [步骤 1:停止 MySQL 服务](#步骤 1:停止 MySQL 服务)
      • [步骤 2:安全模式启动](#步骤 2:安全模式启动)
      • [步骤 3:无密码登录](#步骤 3:无密码登录)
      • [步骤 4:修改密码](#步骤 4:修改密码)
      • [步骤 5:重启服务](#步骤 5:重启服务)
      • [步骤 6:验证新密码](#步骤 6:验证新密码)
    • [💡 总结与建议](#💡 总结与建议)

|-----------------------------|
| 🌺The Begin🌺点点关注,收藏不迷路🌺 |

📝 前言

今天维护服务器时遇到一个棘手问题:MySQL 8 数据库的 root 密码找不到了,无法连接数据库:

bash 复制代码
[root@mysql ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

相信很多 DBA 和运维人员都遇到过这种尴尬情况。折腾一番后,我找到了两种可行的解决方案,现在分享给大家,希望能帮到遇到同样问题的朋友。

🎯 解决方案一:使用 skip-grant-tables 参数

这种方法通过设置 skip-grant-tables 参数来跳过权限表的加载。当 MySQL 以该参数启动时,会跳过所有用户权限和认证检查,允许任何人无需密码登录。

步骤 1:修改配置文件

编辑 MySQL 配置文件 my.cnf,在 [mysqld] 部分添加 skip-grant-tables:

bash 复制代码
[root@mysql ~]# cat /etc/my.cnf
[mysqld]
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /data/mysql/data
socket = /data/mysql/tmp/mysql.sock
pid-file = /data/mysql/tmp/mysql.pid
skip-grant-tables   # 添加这一行

步骤 2:重启 MySQL 服务

bash 复制代码
[root@mysql ~]# service mysql status
 SUCCESS! MySQL running (23724)

[root@mysql ~]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL........... SUCCESS!

步骤 3:无密码登录 MySQL

bash 复制代码
[root@mysql ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.26 MySQL Community Server - GPL

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]>

步骤 4:修改 root 密码

sql 复制代码
MySQL [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

-- 注意:必须先执行 FLUSH PRIVILEGES
MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

-- 修改密码(根据你的需要修改 'mysql' 为 desired password)
MySQL [mysql]> ALTER USER 'root'@'%' IDENTIFIED BY 'mysql';
Query OK, 0 rows affected (0.01 sec)

⚠️ 关键提醒 :在 --skip-grant-tables 模式下,MySQL 会跳过权限验证,不会自动应用对 mysql.user 表的修改。必须 先执行 FLUSH PRIVILEGES 手动刷新权限表,才能确保修改生效。

步骤 5:恢复配置文件并重启

bash 复制代码
# 注释掉 skip-grant-tables 参数
[root@mysql ~]# sed -i '/skip-grant-tables/s/^/#/' /etc/my.cnf

# 重启 MySQL 服务
[root@mysql ~]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL........... SUCCESS!

步骤 6:验证新密码

bash 复制代码
[root@mysql ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL

MySQL [(none)]>

成功登陆!🎉


🚀 解决方案二:使用 mysqld_safe 安全模式

这种方法原理和方案一相同,只是使用 mysqld_safe 命令启动 MySQL 安全模式来跳过权限表。

步骤 1:停止 MySQL 服务

bash 复制代码
[root@mysql ~]# service mysql stop
Shutting down MySQL. SUCCESS!

步骤 2:安全模式启动

bash 复制代码
[root@mysql ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
[1] 6736
[root@mysql ~]# 2026-01-07T09:06:02.741202Z mysqld_safe Logging to '/data/mysql/log/error.log'
2026-01-07T09:06:02.765308Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data

步骤 3:无密码登录

bash 复制代码
[root@mysql ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.26 MySQL Community Server - GPL

MySQL [(none)]>

步骤 4:修改密码

sql 复制代码
MySQL [(none)]> use mysql;
Database changed

-- 同样需要先执行 flush privileges
MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

-- 设置新密码
MySQL [mysql]> ALTER USER 'root'@'%' IDENTIFIED BY 'mysql1';
Query OK, 0 rows affected (0.01 sec)

步骤 5:重启服务

bash 复制代码
[root@mysql ~]# service mysql restart
Shutting down MySQL.2026-01-07T09:07:56.558733Z mysqld_safe mysqld from pid file /data/mysql/tmp/mysql.pid ended
 SUCCESS!
[1]+  Done                    /usr/local/mysql/bin/mysqld_safe --skip-grant-tables

步骤 6:验证新密码

bash 复制代码
[root@mysql ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL

MySQL [(none)]>

成功登陆!🎊


💡 总结与建议

两种方法的对比

方案 适用场景 优点 缺点
方案一(修改配置文件) 可以重启 MySQL 服务 操作规范,适合生产环境 需要修改配置文件并重启
方案二(mysqld_safe) 无法修改配置文件或需要快速处理 无需改配置文件,启动灵活 需要知道 mysqld_safe 的路径

注意事项

  1. MySQL 8 的密码修改语法 :MySQL 8 不再支持 SET PASSWORD 或 UPDATE user SET password 的旧语法,必须使用 ALTER USER 命令

  2. FLUSH PRIVILEGES 的重要性 :在跳过权限表模式下,一定要先执行 FLUSH PRIVILEGES,否则修改不会生效

  3. 安全性考虑 :操作完成后务必关闭 skip-grant-tables 模式,否则数据库处于无防护状态,极其危险

  4. 备份配置文件 :修改 my.cnf 前建议先备份

预防措施

为了防止再次忘记密码,建议:

  • 使用密码管理工具(如 LastPass、1Password)保存密码
  • 定期更换密码并记录
  • 配置 MySQL 连接使用 socket 认证(本地连接无需密码)
  • 设置 root 用户的多因素认证

希望这篇文章对你有帮助!如果你在操作过程中遇到任何问题,欢迎在评论区留言交流。如果觉得文章有用,请点赞收藏支持一下~ 😊

|---------------------------|
| 🌺The End🌺点点关注,收藏不迷路🌺 |

相关推荐
李兆龙的博客7 小时前
问津集 #26:Lakebase——Postgres 的版本化页面存储、数据库分支与计算弹性
数据库
倔强的石头_9 小时前
聊聊金仓KFS:一款把数据同步软件做扎实的产品
数据库
闲云野鹤在人间9 小时前
MySQL|从理论、安装、备份到主从复制、MHA高可用详解
linux·运维·数据库·mysql·云计算
禾小西9 小时前
Redis:从两大维度和三大主线建立知识体系
数据库·redis·缓存
禾小西10 小时前
Redis 数据结构:快速的 Redis 有哪些慢操作?
数据结构·数据库·redis
数据库小学妹10 小时前
数据共享交换平台选型:交换方式对比与避坑指南
数据库·信创·数据同步·数据交换平台·政务数据共享·数据共享交换平台·数据库底座
要开心吖ZSH10 小时前
MySQL 慢 SQL 排查操作手册-个人笔记版
java·笔记·sql·mysql·慢查询
loong_XL12 小时前
决策模型做内容安全检测:从踩坑到上线
数据库·安全·jev·决策模型
这个DBA有点耶12 小时前
数据库双轨并行实战:全量并行策略、增量延迟控制、双向回切与一致性校验
数据库·架构·dba
Gl�ria12 小时前
MySQL 单机版 vs 高可用版:宕机排查 + 故障处理
mysql·adb·架构