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

來源:互聯網
上載者:User

如果哪天你忘記了線上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 -phelloWarning: 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 -phelloWarning: 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 2528Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> 

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

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.