MySQL忘記root密碼--不重啓mysqd重置root密碼

先提個問題:如何不重啓mysqld,且沒有權限修改用戶帳號和權限的狀況下,如何從新設置root密碼?不知道不要緊,在此以前我也是不知道如何操做的,先看看下面的幾種重置root密碼的方法。mysql

一、skip-grant-tables

咱們經常使用的方法是使用skip-grant-tables選項,mysqld server啓動以後並不使用權限系統(privilege system)。用戶不須要任何帳號、不受任何限制的訪問數據庫中全部數據。爲了安全起見,一般加上skip-networking,mysqld不偵放任何TCP/IP鏈接請求。操做過程以下,sql

1)修改my.cnf配置文件,在mysqld選項中添加skip-grant-tables和skip-networking。數據庫

2)再重啓mysqld server。安全

3)經過sql語句修改mysql.user表中存儲密碼。執行flush privileges,從新啓用mysql權限系統。測試

UPDATE mysql.user SET password=PASSWORD('newpwd')WHERE user='root';

FLUSH PRIVILEGES;

4)刪除或者註釋配置文件中skip-grant-tables和skip-networking的參數選項。若是使用skip-networking,則須要再次重啓mysqld。由於skip-networking不是系統變量,只是mysqld的參數選項,而不能經過系統變量動態進行設置。若是沒有適用skip-networking,只須要執行flush privileges就可使權限系統從新生效。ui

2. --init-file

mysqld_safe可使–init-file參數選項來執行從新設定密碼的sql語句。this

1)新建一個初始化文件,如/tmp/initfile,文件內容爲上面修改密碼的sql語句。spa

UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
 
FLUSH PRIVILEGES;

2)關閉mysqld服務進程。code

3)使用mysqld_safe啓動mysqld;orm

mysqld_safe --init-file=/home/me/mysql-init &

上面的兩種方法是在忘記root密碼狀況下從新設置密碼的方法,能夠發現都須要重啓mysqld服務。不少人都是使用第一種進行重置root密碼,可是比較推薦的作法反而是第二種,即安全有快捷簡單。

三、不重啓mysqld的方法

一、首先得有一個能夠擁有修改權限的mysql數據庫帳號,當前的mysql實例帳號(較低權限的帳號,好比能夠修改test數據庫)或者其餘相同版本實例的帳號。把data/mysql目錄下面的user表相關的文件複製到data/test目錄下面。

[root@localhost mysql]# cp mysql/user.* test/

[root@localhost mysql]# chown mysql.mysql test/user.*

二、使用另外一個較低權限的帳號連接數據庫,設置test數據庫中的user存儲的密碼數據。

[root@localhost mysql]# mysql -utest -p12345
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, 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
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password('yayun') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5  Changed: 0  Warnings: 0

mysql>

三、把修改後的user.MYD和user.MYI複製到mysql目錄下,記得備份以前的文件。

mv mysql/user.MYD mysql/user.MYD.bak
mv mysql/user.MYI mysql/user.MYI.bak
cp test/user.MY* mysql/
chown mysql.mysql mysql/user.*

四、查找mysql進程號,而且發送SIGHUP信號,從新加載權限表。

[root@localhost mysql]# pgrep -n mysql
2184
[root@localhost mysql]#

[root@localhost mysql]# kill -SIGHUP 2184

5.登錄測試

[root@localhost mysql]# mysql -uroot -pyayun
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, 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>
相關文章
相關標籤/搜索