MySQL密码丢失破解方法

第1章 单实例破解方法

1.1 停止mysql服务

[root@mysql01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!

1.2 重新启动mysql启动

直接使用mysqld_safe命令启动,并添加参数--skip-grant-tables,跳过权限认证表

[root@mysql01 ~]# mysqld_safe --skip-grant-tables &
[1] 5985
[root@mysql01 ~]# 180125 05:17:49 mysqld_safe Logging to '/opt/mysql/data/mysql01.err'.
180125 05:17:49 mysqld_safe Starting mysqld daemon with databases from /opt/mysql/data

1.3 直接无密码登录mysql

步骤2启动成功之后,则不需要密码就能够登录数据库:

[root@mysql01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.55 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
 
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>

1.4 修改root密码

使用mysql命令中的UPDATE修改密码

mysql> update mysql.user SET password=PASSWORD("123456") WHERE user='root' and host='localhost'
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

update修改密码命令

mysql.user存放用户信息的数据库和表格

SET:设置密码参数

password=PASSWORD("123456")设置新的密码,其中PASSWORD的目的是给“123456”加密,必须添加该参数

WHERE user='root' and host='localhost'定位需要修改密码的用户

1.5 刷新用户权限:

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

1.6 退出并重启数据库

kill PID
[root@mysql01 ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!

修改完成!

[root@mysql01 ~]# mysql -uroot -p123456

第2章 多实例破解方法

多实例破解方式和但实例类似,只是要接的参数更多些。

2.1 停止MySQL服务

首先通过ps -ef查找到mysqld服务对应的进程号,然后通过kill命令将进程杀掉。


[root@localhost support-files]# ps -ef |grep 3308 | grep -v grep 
root      59695      1  0 04:15 pts/1    00:00:00 /bin/sh /opt/mysql/bin/mysqld_safe --defaults-file=/data/3308/my.cnf
mysql     60419  59695  0 04:15 pts/1    00:00:07 /opt/mysql-5.5.32/bin/mysqld --defaults-file=/data/3308/my.cnf --basedir=/opt/mysql --datadir=/data/3308/data --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=/data/3308/mysql_3308.err --open-files-limit=8192 --pid-file=/data/3308/mysqld.pid --socket=/data/3308/mysql.sock --port=3308
[root@localhost support-files]# kill 60419


注:此处不能使用kill -9参数,否则会造成严重的后果

2.2 使用mysqld_safe启动

[root@localhost 3308]# mysqld_safe --defaults-file=/data/3308/my.cnf --skip-grant-tables &
[1] 61039
[root@localhost 3308]# 180127 07:11:02 mysqld_safe Logging to '/var/log/mysqld.log'.
180127 07:11:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
180127 07:11:02 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
 
[1]+  Done                    mysqld_safe --default-file=/data/3308/my.cnf --skip-grant-tables

注:参数--skil-grant-tables需添加在命令末尾。

2.3 无密码登录数据库

多实例登录mysql数据库需要指定sock文件:

[root@localhost 3308]# mysql -uroot -p -S /data/3308/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 Source distribution
 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
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>

2.4 重新设置密码

mysql> update mysql.user SET password=PASSWORD("123456") where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql>

2.5 刷新用户权限

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

2.6 退出并重启数据库

[root@localhost 3308]# kill 61955
[root@localhost 3308]# 180127 07:19:08 mysqld_safe mysqld from pid file /data/3308/mysqld.pid ended
 
[1]+  Done                    mysqld_safe --defaults-file=/data/3308/my.cnf --skip-grant-tables
[root@localhost 3308]# /data/3308/mysql start
Starting MySQL......

2.7 重新登录

[root@localhost 3308]# mysql -uroot -p123456 -S /data/3308/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 Source distribution
 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
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>

 完成!!