標籤:style blog http io ar color os 使用 sp
網上有很多關於忘記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.sockWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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: 0mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
已經成功修改密碼了,但還有事情要做,就是把剛剛添加到my.cnf裡的skip-grant-tables和skip-networking刪除掉或者注釋掉。
情況二:(不能重啟mysql的情況)
如果不能重啟,mysql.user 剛好有許可權比較低的使用者,如果沒有,你請神仙來幫你吧,哈哈
1、為了測試,我自己建立一個使用者,可以沒什麼許可權
mysql> create user [email protected]‘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 3Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql> use testDatabase changedmysql> 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 mysql23166[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 5Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql>
OK,已經成功登入了,如果有更多好的方法,我們可以再一起討論下
PS:本人也是參考別人的部落格做的,但我沒有照搬別人的東西,太噁心了,希望大家有自己的風格。^.^
解決MySQL忘記root密碼