程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MySQL的root密碼忘記怎麼辦 修改root密碼的方式

MySQL的root密碼忘記怎麼辦 修改root密碼的方式

編輯:關於MYSQL數據庫

如果哪天你忘記了線上MySQL數據庫的root密碼,怎麼辦?

大家往往會想到skip-grant-tables參數,具體步驟如下:

1. 關閉MySQL數據庫,因為root密碼忘記了,mysqladmin無法使用,此時,只能通過kill pid關閉程序。

在這裡,科普一下kill 和kill -9的區別

默認參數下,kill 發送SIGTERM信號給進程,告訴進程,你需要被關閉,請自行停止運行並退出。
kill -9 發送SIGKILL信號給進程,告訴進程,你被終結了,請立刻退出。與SIGTERM相比,這個信號不能被捕獲或忽略,同時接收這個信號的進程在收到這個信號時不能執行任何清理
所以,萬不得已,不要通過kill -9殺掉進程,這可能導致MySQL數據庫的物理結構損壞,無法重新啟動。

2. 在my.cnf文件[mysqld]部分添加skip-grant-tables參數

3. 登錄數據庫,修改root賬戶的密碼

以下是修改root密碼的三種方式

    1> mysql> set password for 'root'@'localhost'=password('123'); 無需刷新權限表

    2> mysql> update mysql.user set password=password("456") where user="root" and host="localhost";

         mysql> flush privileges;

    3> # mysqladmin -u root password "123"

4. 關閉數據庫,注釋掉skip-grant-tables參數,重新啟動數據庫。

 上面這種方式雖然不錯,但是有個問題,你必須重啟數據庫,對於線上環境,這可能是不被允許的。

下面來談談另一種方法,有點“黑暗科技”的味道

這個方法利用的是mysql.user表還是MyISAM引擎的特性。

1. 將該實例的mysql.user表copy到另一個實例的目錄下,譬如,test數據庫的目錄下

2. 登錄另一個實例數據庫,修改上述三個文件的權限,並修改root密碼

mysql> select user,host,password from test.user;
+------+-----------+-------------------------------------------+
| user | host | password     |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> update test.user set password=password("hello") where user="root" and host="localhost";
Query OK, 1 row affected (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 0

3. 將上述三個文件copy回源數據庫

4. 獲取mysqld的pid,通過kill -HUP `pidof mysqld`方式讓mysqld進程重新加載配置文件

[root@keepalived01 ~]# mysql -phello
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@keepalived01 ~]# kill -HUP 4283
[root@keepalived01 ~]# mysql -phello
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 2528
Server version: 5.6.26 MySQL Community Server (GPL)

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

通過上述輸出可以看出,kill -HUP之前,直接用密碼hello登錄被拒絕,kill -HUP之後,就可以直接登錄了。

當然,以上方法僅供參考,在生產上慎用,畢竟安全壓倒一切,天曉得哪裡會出現問題。

以上就是本文的全部內容,希望可以幫助大家解決root密碼忘記的困擾,謝謝大家的閱讀。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved