mysql8 忘记 root 密码,怎么破?

前言

今天发现有一套 MySQL8 数据库 root 密码找不到了,无法连接:

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

折腾了一下,找到了几种解决方案,本文分享一下跟大家一起交流一下。

方案一

通过设置 skip-grant-tables 参数来跳过加载权限表。

当 MySQL 启动时使用 --skip-grant-tables 参数时,MySQL 会跳过所有涉及用户权限和认证的检查,允许任何人以任何用户名和密码(或者不使用密码)登录数据库。

修改 my.cnf 配置文件,在 mysqld 下面增加一行:

bash 复制代码
[root@SSSZLMYSQL1 ~]# 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

重启 MySQL 服务:

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

使用无密码方式登录:

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

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

MySQL [(none)]>

修改 root 密码:

bash 复制代码
MySQL [(none)]> use mysql;
## 必须先执行 FLUSH PRIVILEGES 命令通过强制 MySQL 重新加载 mysql 数据库中的权限信息,使修改生效
MySQL [mysql]> flush privileges;
MySQL [mysql]> ALTER USER 'root'@'%' IDENTIFIED BY 'mysql';

注意 :在 --skip-grant-tables 模式下,MySQL 会跳过权限验证,不会自动应用对 mysql.user 表的修改,必须通过执行 FLUSH PRIVILEGES 来手动刷新权限表,确保对用户表的修改生效。

再次编辑 my.cnf 配置文件,取消参数 --skip-grant-tables,然后重启 MySQL 服务:

bash 复制代码
[root@mysql ~]# sed -i '/skip-grant-tables/s/^/#/' /etc/my.cnf
[root@mysql ~]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL........... SUCCESS!

使用新密码登陆 MySQL 数据库:

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

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

MySQL [(none)]>

成功登陆。

方案二

这个和方案一的原理相同,只是命令不相同,使用 mysqld_safe 命令启动 MySQL 安全模式(跳过权限表)。

首先关闭 MySQL 服务:

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

安全模式启动:

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

无密码模式登陆:

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

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

MySQL [(none)]>

修改 root 密码:

bash 复制代码
MySQL [(none)]> use mysql;
## 必须先执行 FLUSH PRIVILEGES 命令通过强制 MySQL 重新加载 mysql 数据库中的权限信息,使修改生效
MySQL [mysql]> flush privileges;
MySQL [mysql]> ALTER USER 'root'@'%' IDENTIFIED BY 'mysql1';

重启 MySQL 服务:

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

使用新密码登陆 MySQL 数据库:

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

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

MySQL [(none)]>

成功登陆。

总结

看了下网上大概就这两种方案,我都验证了没有问题,大家如果还有其他方案,欢迎在评论区交流!


📚 推荐阅读:DBA 学习之路

如果这篇文章对你有帮助,推荐访问我的 Oracle DBA 系统学习站点,涵盖 100 天完整学习路线:

  • 🔧 Oracle 安装部署 · RMAN 备份恢复 · Data Pump 数据迁移
  • 🏗️ RAC 高可用 · DataGuard 容灾 · 多租户架构
  • 🔍 故障排查 · 升级迁移 · GoldenGate 数据同步

👉 立即访问 ora100.com →

相关推荐
一朵小花2 分钟前
记一次Docker 容器 NFS 文件写入延迟排查过程
后端
FF2501_940228582 小时前
Grid 构建月历网格:7 列模板 + 日期占位算法
后端·华为·harmonyos·鸿蒙系统
神奇小汤圆2 小时前
线程池的 DiscardPolicy 为什么是「静默杀手」?一个生产事故的完整复盘
后端
大鸡腿同学2 小时前
个人博主 AI 剪辑实操:口播视频 3 步出片,省 80% 剪辑时间
后端
其美杰布-富贵-李2 小时前
Spring Boot 工程开发全流程说明
java·spring boot·后端
xianjixiance_2 小时前
HarmonyOS应用开发实战:萌宠日记 - 整体架构设计与技术选型解析
后端
掘金者阿豪2 小时前
数据库优化器到底在想什么:一个SQL今天能跑明天崩的背后
后端
b130538100492 小时前
HarmonyOS应用开发实战:萌宠日记 - 回调函数模式
后端
Conan在掘金2 小时前
鸿蒙报错速查:Cannot find name 'image',忘 import 编译就炸,根因 + 真解法
后端
雪隐3 小时前
个人电脑玩AI-13让5060 Ti给你打工——我用 0.9B 小模型终结了"谁来记会议纪要"这个世纪难题
前端·人工智能·后端