如何解決MySQL的root密碼忘記的問題_MySQL

如果哪天你忘記了線上mysql數(shù)據(jù)庫的root密碼,怎么辦?

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

1. 關(guān)閉mysql數(shù)據(jù)庫,因為root密碼忘記了,mysqladmin無法使用,此時,只能通過kill pid關(guān)閉程序。

在這里,科普一下kill 和kill -9的區(qū)別

默認(rèn)參數(shù)下,kill 發(fā)送SIGTERM信號給進(jìn)程,告訴進(jìn)程,你需要被關(guān)閉,請自行停止運(yùn)行并退出。

kill -9 發(fā)送SIGKILL信號給進(jìn)程,告訴進(jìn)程,你被終結(jié)了,請立刻退出。與SIGTERM相比,這個信號不能被捕獲或忽略,同時接收這個信號的進(jìn)程在收到這個信號時不能執(zhí)行任何清理

所以,萬不得已,不要通過kill -9殺掉進(jìn)程,這可能導(dǎo)致MySQL數(shù)據(jù)庫的物理結(jié)構(gòu)損壞,無法重新啟動。

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

3. 登錄數(shù)據(jù)庫,修改root賬戶的密碼

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

1> mysql> set password for ‘root’@’localhost’=password(‘123’); 無需刷新權(quán)限表

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. 關(guān)閉數(shù)據(jù)庫,注釋掉skip-grant-tables參數(shù),重新啟動數(shù)據(jù)庫。

上面這種方式雖然不錯,但是有個問題,你必須重啟數(shù)據(jù)庫,對于線上環(huán)境,這可能是不被允許的。

下面來談?wù)劻硪环N方法,有點(diǎn)“黑暗科技”的味道

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

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

?

如何解決MySQL的root密碼忘記的問題_MySQL

?

2. 登錄另一個實例數(shù)據(jù)庫,修改上述三個文件的權(quán)限,并修改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回源數(shù)據(jù)庫

?

如何解決MySQL的root密碼忘記的問題_MySQL

?

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

?

如何解決MySQL的root密碼忘記的問題_MySQL

?

[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之后,就可以直接登錄了。

當(dāng)然,以上方法僅供參考,在生產(chǎn)上慎用,畢竟安全壓倒一切,天曉得哪里會出現(xiàn)問題。

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

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊7 分享