mysql忘記密碼真的需要重啓服務嗎?

先提個問題:如何不重啓mysqld,且沒有權限修改用戶賬號和權限的情況下,如何重新設置root密碼?不知道沒關係,在此之前我也是不知道如何操作的,先看看下面的幾種重置root密碼的方法。

 

1、skip-grant-tables

我們常用的方法是使用skip-grant-tables選項,mysqld server啓動之後並不使用權限系統(privilege system)。用戶不需要任何賬號、不受任何限制的訪問數據庫中所有數據。爲了安全起見,通常加上skip-networking,mysqld不偵聽任何TCP/IP連接請求。操作過程如下,

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('newpassword') where user='root';
flush privileges;

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

 

2. --init-file

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

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

update mysql.user set password=password('newpassword') where user='root';

flush privileges;

2)關閉mysqld服務進程。

3)使用mysqld_safe啓動mysqld;

mysqld_safe --init-file=/tmp/initfile &

上面的兩種方法是在忘記root密碼情況下重新設置密碼的方法,可以發現都需要重啓mysqld服務。很多人都是使用第一種進行重置root密碼,但是比較推薦的做法反而是第二種,即安全有快捷簡單。

 

3、不重啓mysqld的方法

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

mv mysql/user.* test/
chown mysql:mysql test/user.*

2)使用另一個較低權限的賬號鏈接數據庫,設置test數據庫中的user存儲的密碼數據。

[root@xiaoya test]# mysql -utest -ptest
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 5
Server version: 5.6.31-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
(product)test@localhost [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)
(product)test@localhost [(none)]> update test.user set password=password(123456) where user='root';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    6
Current database: *** NONE ***
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
(product)test@localhost [(none)]> select user,host from test.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
| test | localhost |
+------+-----------+
2 rows in set (0.00 sec)
(product)test@localhost [(none)]> quit
Bye

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

mv test/user.* ../mysql/
chown mysql:mysql test/user.*

4、查找mysql進程號,並且發送SIGHUP信號,重新加載權限表。

[root@xiaoya mysql]# pgrep -n mysql

19764

[root@xiaoya mysql]# kill -SIGHUP 19764

5.登陸測試

[root@xiaoya mysql]# mysql -uroot -p123456
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 7
Server version: 5.6.31-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
(product)root@localhost [(none)]> quit
Bye

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