跳到主要内容
返回博客列表mysql8 忘记 root 密码,怎么破? 今天发现有一套 MySQL8 数据库 root 密码找不到了,无法连接: bash root@mysql ~ mysql -uroot -p Enter password: ERROR 1045 28000: Access denied for user 'root'@'localhost' using password: YES 折腾了一下,找到了几种解决方案,本文分享一下跟大家一起交流一下。
2738+ 篇实战文章 1500+ 步骤截图 10 大核心模块前言
本章目标 :掌握本章核心知识点
前置要求 :完成前序章节学习
预计时长 :60 分钟
今天发现有一套 MySQL8 数据库 root 密码找不到了,无法连接:
1 [root@mysql ~]# mysql -uroot -p
2 Enter password:
3 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
折腾了一下,找到了几种解决方案,本文分享一下跟大家一起交流一下。
方案一
通过设置 skip-grant-tables 参数来跳过加载权限表。
当 MySQL 启动时使用 --skip-grant-tables 参数时,MySQL 会跳过所有涉及用户权限和认证的检查,允许任何人以任何用户名和密码(或者不使用密码)登录数据库。
修改 my.cnf 配置文件,在 [mysqld] 下面增加一行:
1 [root@SSSZLMYSQL1 ~]# cat /etc/my.cnf
2 ## 在 []
3 [mysqld]
4 user = mysql
5 port = 3306
6 basedir = /usr/local/mysql
7 datadir = /data/mysql/data
8 socket = /data/mysql/tmp/mysql.sock
9 pid-file = /data/mysql/tmp/mysql.pid
10 skip-grant-tables
重启 MySQL 服务:
1 [root@mysql ~]# service mysql status
2 SUCCESS! MySQL running (23724)
3 [root@mysql ~]# service mysql restart
4 Shutting down MySQL.. SUCCESS!
5 Starting MySQL........... SUCCESS!
使用无密码方式登录:
1 [root@mysql ~]# mysql -uroot
2 Welcome to the MySQL monitor. Commands end with ; or \g.
3 Your MySQL connection id is 7
4 Server version: 8.0.26 MySQL Community Server - GPL
5
6 Copyright (c) 2000, 2021, Oracle and/or its affiliates.
7
8 Oracle is a registered trademark of Oracle Corporation and/or its
9 affiliates. Other names may be trademarks of their respective
10 owners.
11
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13
14 MySQL [(none)]>
修改 root 密码:
1 MySQL [(none)]> use mysql;
2 ## 必须先执行 FLUSH PRIVILEGES 命令通过强制 MySQL 重新加载 mysql 数据库中的权限信息,使修改生效
3 MySQL [mysql]> flush privileges;
4 MySQL [mysql]> ALTER USER 'root'@'%' IDENTIFIED BY 'mysql';
注意 :在 --skip-grant-tables 模式下,MySQL 会跳过权限验证,不会自动应用对 mysql.user 表的修改,必须通过执行 FLUSH PRIVILEGES 来手动刷新权限表,确保对用户表的修改生效。
再次编辑 my.cnf 配置文件,取消参数 --skip-grant-tables,然后重启 MySQL 服务:
1 [root@mysql ~]# sed -i '/skip-grant-tables/s/^/#/' /etc/my.cnf
2 [root@mysql ~]# service mysql restart
3 Shutting down MySQL.. SUCCESS!
4 Starting MySQL........... SUCCESS!
使用新密码登陆 MySQL 数据库:
1 [root@mysql ~]# mysql -uroot -p
2 Enter password:
3 Welcome to the MySQL monitor. Commands end with ; or \g.
4 Your MySQL connection id is 8
5 Server version: 8.0.26 MySQL Community Server - GPL
6
7 Copyright (c) 2000, 2021, Oracle and/or its affiliates.
8
9 Oracle is a registered trademark of Oracle Corporation and/or its
10 affiliates. Other names may be trademarks of their respective
11 owners.
12
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14
15 MySQL [(none)]>
成功登陆。
方案二
这个和方案一的原理相同,只是命令不相同,使用 mysqld_safe 命令启动 MySQL 安全模式(跳过权限表)。
首先关闭 MySQL 服务:
1 [root@mysql ~]# service mysql stop
2 Shutting down MySQL. SUCCESS!
安全模式启动:
1 [root@mysql ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
2 [1] 6736
3 [root@mysql ~]# 2026-01-07T09:06:02.741202Z mysqld_safe Logging to '/data/mysql/log/error.log'.
4 2026-01-07T09:06:02.765308Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
无密码模式登陆:
1 [root@mysql ~]# mysql -uroot
2 Welcome to the MySQL monitor. Commands end with ; or \g.
3 Your MySQL connection id is 7
4 Server version: 8.0.26 MySQL Community Server - GPL
5
6 Copyright (c) 2000, 2021, Oracle and/or its affiliates.
7
8 Oracle is a registered trademark of Oracle Corporation and/or its
9 affiliates. Other names may be trademarks of their respective
10 owners.
11
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13
14 MySQL [(none)]>
修改 root 密码:
1 MySQL [(none)]> use mysql;
2 ## 必须先执行 FLUSH PRIVILEGES 命令通过强制 MySQL 重新加载 mysql 数据库中的权限信息,使修改生效
3 MySQL [mysql]> flush privileges;
4 MySQL [mysql]> ALTER USER 'root'@'%' IDENTIFIED BY 'mysql1';
重启 MySQL 服务:
1 [root@mysql ~]# service mysql restart
2 Shutting down MySQL.2026-01-07T09:07:56.558733Z mysqld_safe mysqld from pid file /data/mysql/tmp/mysql.pid ended
3 SUCCESS!
4 [1]+ Done /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
使用新密码登陆 MySQL 数据库:
1 [root@mysql ~]# mysql -uroot -p
2 Enter password:
3 Welcome to the MySQL monitor. Commands end with ; or \g.
4 Your MySQL connection id is 8
5 Server version: 8.0.26 MySQL Community Server - GPL
6
7 Copyright (c) 2000, 2021, Oracle and/or its affiliates.
8
9 Oracle is a registered trademark of Oracle Corporation and/or its
10 affiliates. Other names may be trademarks of their respective
11 owners.
12
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14
15 MySQL [(none)]>
成功登陆。
总结
看了下网上大概就这两种方案,我都验证了没有问题,大家如果还有其他方案,欢迎在评论区交流!
mysql8 忘记 root 密码,怎么破? - DBA 学习之路