解決MySQL忘記root密碼


網上有很多關於忘記MySQL root密碼的一些文章,里面都有寫怎么去解決,但有時覺得寫得太惡心,要么一字不漏的抄別人的,要么就說得不清不楚,好了,不吐槽了,以下是解決的整個過程。

首先我們要知道忘記MySQL root密碼后,能否重啟mysql,能重啟的操作是怎么樣的?不能重啟的操作又會是怎么樣的?

 

情況一:(能重啟情況下)

修改my.cnf配置文件,在mysqld欄下添加skip-grant-tables選項,意思是mysqld server啟動之后並不使用權限系統(privilege system),也可以理解為跳過授權表。為了安全起見,通常加上skip-networking,mysqld不偵聽任何TCP/IP連接請求。

重啟mysqld,然后空密碼連接:

[root ~]$mysql -uroot -S /data/mysql-5.5/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> 

可以看到已經成功登錄了,然后修改root密碼:

mysql> update mysql.user set password=password('123456') where user='root';    
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges; 
Query OK, 0 rows affected (0.01 sec)

已經成功修改密碼了,但還有事情要做,就是把剛剛添加到my.cnf里的skip-grant-tables和skip-networking刪除掉或者注釋掉。

 

情況二:(不能重啟mysql的情況)

如果不能重啟,mysql.user 剛好有權限比較低的用戶,如果沒有,你請神仙來幫你吧,哈哈

1、為了測試,我自己創建一個用戶,可以沒什么權限

mysql> create user xuanzhi@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

2、進到數據目錄下:

[root mysql-5.5]$ pwd
/data/mysql-5.5
[root mysql-5.5]$ cp mysql/user.* test/
[root mysql-5.5]$ chown mysql.mysql test/user.*

3、用權限比較小的用戶登錄:

[root mysql-5.5]$mysql -uxuanzhi -p123456 -S /data/mysql-5.5/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> use test
Database changed
mysql> update user set password=password('123123') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

4、把修改后的user.MYD和user.MYI復制到mysql目錄下,記得備份之前的文件。

[root mysql-5.5]$ pwd
/data/mysql-5.5
[root mysql-5.5]$ mv mysql/user.MYD mysql/user.MYD.bak
[root mysql-5.5]$ mv mysql/user.MYI mysql/user.MYI.bak
[root mysql-5.5]$ cp test/user.MY* mysql/
[root mysql-5.5]$ chown mysql:mysql  mysql/user.*

5.查找mysql進程號,並且發送SIGHUP信號,重新加載權限表。(有時加載一次不行的時候,再加載多一次@。@)

[root mysql]$ pgrep -n mysql
23166
[root mysql]$ kill -SIGHUP 23166
[root mysql]$ /usr/local/mysql-5.5.40/bin/mysql -uroot -p123123 -S /data/mysql-5.5/mysql.sock 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root mysql]$ kill -SIGHUP 23166
[root mysql]$ /usr/local/mysql-5.5.40/bin/mysql -uroot -p123123 -S /data/mysql-5.5/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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、創建新的數據目錄,並給原來user相應的權限,忘記密碼對應的實例它的user是mysql,所以把權限給mysql用戶

[root data]$ mkdir -pv /dbdata/datadir/
mkdir: 已創建目錄 "/dbdata"
mkdir: 已創建目錄 "/dbdata/datadir/"
[root data]$ chown  -R mysql:mysql /dbdata/datadir/

2、執行初始化操作:(報錯了)

[root scripts]$ pwd
/usr/local/mysql-5.5.40/scripts
[root scripts]$ ./mysql_install_db --datadir=/dbdata/datadir/ --user=mysql2

FATAL ERROR: Could not find ./bin/my_print_defaults

If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.

If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.

解決方法:

[root scripts]$ /usr/local/mysql-5.6.10/scripts/mysql_install_db --datadir=/dbdata/datadir/ --user=mysql --datadir=/dbdata/datadir/ --basedir=/usr/local/mysql-5.6.10/
Installing MySQL system tables...
141210 16:09:24 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
OK
Filling help tables...
141210 16:09:24 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
OK

3、啟動一個新的進程,這里要注意一下port,sock文件,還有pid文件,這都是新的,user還是忘記密碼實例的user,而不是忘記密碼對應的那個數據庫實例的,這里我們不需要用到InnoDB引擎,設置默認引擎為MyISAM:

[root ~]$  /usr/local/mysql-5.6.10/bin/mysqld_safe --datadir=/dbdata/datadir --plugin-dir=/usr/local/mysql-5.6.10/lib/plugin/  --skip-innodb \
> --default-storage-engine=myisam --socket=/dbdata/datadir/mysql2.sock --user=mysql --port=3305 --log-error=/dbdata/datadir/error2.log --pid-file=/data/mysql-5.6/mysql.pid &
[1] 21204
[root ~]$
141210 16:56:11 mysqld_safe Logging to '/dbdata/datadir/error2.log'. 141210 16:56:11 mysqld_safe Starting mysqld daemon with databases from /dbdata/datadir

4、登錄新啟動的mysql實例,此時密碼為空密碼:

root datadir]$ /usr/local/mysql-5.6.10/bin/mysql -S /dbdata/datadir/mysql2.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.10-log 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> flush tables;
Query OK, 0 rows affected (0.00 sec)

修改root密碼:

mysql> select user, host, password from user where user like 'root';
+------+-----------------------+----------+
| user | host                  | password |
+------+-----------------------+----------+
| root | localhost             |          |
| root | localhost.localdomain |          |
| root | 127.0.0.1             |          |
| root | ::1                   |          |
+------+-----------------------+----------+
4 rows in set (0.02 sec)

mysql> update mysql.user set password=password('654321') where user='root'; 
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5、拷備新的user表到忘記密碼的實例數據庫的mysql目錄下

[root mysql]$ pwd
/dbdata/datadir/mysql
[root mysql]$ cp user.* /data/mysql-5.6/mysql/
cp:是否覆蓋"/data/mysql-5.6/mysql/user.frm"? y
cp:是否覆蓋"/data/mysql-5.6/mysql/user.MYD"? y
cp:是否覆蓋"/data/mysql-5.6/mysql/user.MYI"? y
[root mysql]$ chown -R mysql5.6:mysql5.6 /data/mysql-5.6/
[root mysql]$ chmod 660 /data/mysql-5.6/mysql/user.*

6、我們需要到mysqld發送一個sighup信號,MySQL響應這個信號加載授權表,刷新表,日志,線程緩存。
如果是單個MySQL實例,可以用這樣的方法去重新加載

[root ~]$ kill -1 $(/sbin/pidof mysqld)

如果是多個MySQL實例在一台服務器上的話,就要注意點了,可以通過這樣的方法找到pid,我舊實例的端口是3306:

[root mysql-5.6.10]$ netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      8414/mysqld         
tcp        0      0 0.0.0.0:3308                0.0.0.0:*                   LISTEN      6430/mysqld         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1144/sshd           
      
tcp        0      0 :::3310                     :::*                        LISTEN      17151/mysqld        
tcp        0      0 :::22                       :::*                        LISTEN      1144/sshd           
tcp        0      0 ::1:631                     :::*                        LISTEN      1109/cupsd          
tcp        0      0 :::3306                     :::*                        LISTEN      2091/mysqld [root mysql-5.6.10]$ kill -1 2091

有時kill -1一次不行,再執行一次就可以了:

[root mysql-5.6.10]$ kill -1 2091
[root mysql-5.6.10]$ /usr/local/mysql-5.6.10/bin/mysql -uroot -p654321 -S /data/mysql-5.6/mysql.sock 
Warning: Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root mysql-5.6.10]$ kill -1 2091
[root mysql-5.6.10]$ /usr/local/mysql-5.6.10/bin/mysql -uroot -p654321 -S /data/mysql-5.6/mysql.sock 
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.10-log MySQL Community Server (GPL)

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> 

OK,已經成功登錄了,如果有更多好的方法,我們可以再一起討論下

 

 

總結:

1)第一種方法簡單,但需要重啟MySQL,重啟會影響線上業務,一般不建議重啟

2)第二種方法比較好,不用重啟MySQL實例,修改密碼,只修改root用戶的,而且其它保持不變

3)第三種方法也不需要重啟,但是新的user表里,只有root一個用戶,如果之前服務器還存在別的用戶及權限,這就比較麻煩了

 

 

 參考資料:http://www.percona.com/blog/2014/12/10/recover-mysql-root-password-without-restarting-mysql-no-downtime/

PS:本人也是參考別人的博客做的,但我沒有照搬別人的東西,太惡心了,希望大家有自己的風格。^.^

 

作者:陸炫志

出處:xuanzhi的博客 http://www.cnblogs.com/xuanzhi201111

您的支持是對博主最大的鼓勵,感謝您的認真閱讀。本文版權歸作者所有,歡迎轉載,但請保留該聲明。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM