MySQL数据库root密码丢失后的找回

Posted bkwong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL数据库root密码丢失后的找回相关的知识,希望对你有一定的参考价值。

实验前准备:
1.环境
端口3306 该实例是生产环境,欲找回3306的 root密码
测试3307 测试时使用
2.开启 3306,3307 mysqld 服务

[[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3306/my.cnf &
[[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3307/my.cnf & 

 

两种方法:
一.使用 skip_grant_tables 参数

[[email protected] ~]# ps -ef|grep 3306|grep -v grep
mysql 947 365 0 19:00 pts/0 00:00:00 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf
[[email protected] ~]# kill -9 947
[[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --skip_grant_tables & 
[[email protected] ~]# mysql -S /tmp/mysql3306.sock
mysql> alter user user() identified by zstzst;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> update mysql.user set authentication_string=password(zstzst3306) where host=localhost and user=root; 
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> shutdown;
Query OK, 0 rows affected (0.00 sec)
[[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3306/my.cnf &
[[email protected] ~]# mysql -S /tmp/mysql3306.sock -pzstzst3306 #登陆成功

这个方法需要重启MySQL服务,由于免密码登陆,所以再启动数据库时,最好也要加上参数 --skip__networking

 

二.拷贝修改user.*

3306 老密码是zstzst,假装遗忘,通过拷贝user*文件方式修改成zstzst3306
3307 password‘zstzst3307‘

[[email protected] ~]# mysql -S /tmp/mysql3306.sock -pzstzst3306
mysql> alter user [email protected] identified by zstzst; #3306密码修改成 zstzst
[[email protected] ~]# ps -ef|grep mysqld|grep -v grep
mysql 1032 365 0 19:00 pts/0 00:00:00 mysqld --defaults-file=/data/mysql/mysql3307/my.cnf
mysql 3960 365 0 19:05 pts/0 00:00:00 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf
[[email protected] ~]# kill -9 1032 #杀掉3307
[[email protected] ~]# cp /data/mysql/mysql3306/data/mysql/user.frm /data/mysql/mysql3307/data/mysql/user.frm 
[[email protected] ~]# cp /data/mysql/mysql3306/data/mysql/user.MYD /data/mysql/mysql3307/data/mysql/user.MYD
[[email protected] ~]# cp /data/mysql/mysql3306/data/mysql/user.MYI /data/mysql/mysql3307/data/mysql/user.MYI
[[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3307/my.cnf --skip_grant_tables & #这样不用重启生产库
[[email protected] ~]# mysql -S /tmp/mysql3307.sock
mysql> update mysql.user set authentication_string=password(zstzst3306) where host=localhost and user=root; 
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> shutdown;
Query OK, 0 rows affected (0.01 sec)
[[email protected] ~]# cp /data/mysql/mysql3307/data/mysql/user.frm /data/mysql/mysql3306/data/mysql/user.frm 
[[email protected] ~]# cp /data/mysql/mysql3307/data/mysql/user.MYD /data/mysql/mysql3306/data/mysql/user.MYD
[[email protected] ~]# cp /data/mysql/mysql3307/data/mysql/user.MYI /data/mysql/mysql3306/data/mysql/user.MYI
[[email protected] ~]# kill -HUP 3960
[[email protected] ~]# mysql -S /tmp/mysql3306.sock -pzstzst3306 #验证登陆成功

 







以上是关于MySQL数据库root密码丢失后的找回的主要内容,如果未能解决你的问题,请参考以下文章

[读书笔记]mysql 找回丢失的root密码

MySQL数据库应用管理实战-- 找回密码

linux系统下使用xampp 丢失mysql root密码 只能远程访问,本地无法连接数据库

linux 5.7.20和5.6.38版本 数据库忘记root密码怎么找回?

Linux 6.8 root密码丢失找回

linux系统下使用xampp 丢失mysql root密码xampp的初始密码为空