不重啓修改mysql root密碼
--------------------2014/09/28
1、通常忘記密碼的解決辦法,須要重啓Mysql
一、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權限系統。mysql
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語句。sql
2)關閉mysqld服務進程。
3)使用mysqld_safe啓動mysqld;數據庫
上面的兩種方法是在忘記root密碼狀況下從新設置密碼的方法,能夠發現都須要重啓mysqld服務。不少人都是使用第一種進行重置root密碼,可是比較推薦的作法反而是第二種,即安全有快捷簡單。安全
2、不重啓mysqld的方法測試
一、首先得有一個能夠擁有修改權限的mysql數據庫帳號,當前的mysql實例帳號(較低權限的帳號,好比能夠修改test數據庫)或者其餘相同版本實例的帳號。把data/mysql目錄下面的user表相關的文件複製到data/test目錄下面。ui
二、使用另外一個較低權限的帳號連接數據庫,設置test數據庫中的user存儲的密碼數據。this
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.spa
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.code
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.orm
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目錄下,記得備份以前的文件。
四、查找mysql進程號,而且發送SIGHUP信號,從新加載權限表。
五、登錄測試
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>