MySQL數據庫root密碼丟失破解方法

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>

 完成!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章