如果哪天你忘記了線上MySQL數(shù)據(jù)庫的root密碼,怎么辦?
大家往往會(huì)想到skip-grant-tables參數(shù),具體步驟如下:
1. 關(guān)閉MySQL數(shù)據(jù)庫,因?yàn)閞oot密碼忘記了,mysqladmin無法使用,此時(shí),只能通過kill pid關(guān)閉程序。
在這里,科普一下kill 和kill -9的區(qū)別
默認(rèn)參數(shù)下,kill 發(fā)送SIGTERM信號(hào)給進(jìn)程,告訴進(jìn)程,你需要被關(guān)閉,請(qǐng)自行停止運(yùn)行并退出。
kill -9 發(fā)送SIGKILL信號(hào)給進(jìn)程,告訴進(jìn)程,你被終結(jié)了,請(qǐng)立刻退出。與SIGTERM相比,這個(gè)信號(hào)不能被捕獲或忽略,同時(shí)接收這個(gè)信號(hào)的進(jìn)程在收到這個(gè)信號(hào)時(shí)不能執(zhí)行任何清理
所以,萬不得已,不要通過kill -9殺掉進(jìn)程,這可能導(dǎo)致MySQL數(shù)據(jù)庫的物理結(jié)構(gòu)損壞,無法重新啟動(dòng)。
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ù),重新啟動(dòng)數(shù)據(jù)庫。
上面這種方式雖然不錯(cuò),但是有個(gè)問題,你必須重啟數(shù)據(jù)庫,對(duì)于線上環(huán)境,這可能是不被允許的。
下面來談?wù)劻硪环N方法,有點(diǎn)“黑暗科技”的味道
這個(gè)方法利用的是mysql.user表還是MyISAM引擎的特性。
1. 將該實(shí)例的mysql.user表copy到另一個(gè)實(shí)例的目錄下,譬如,test數(shù)據(jù)庫的目錄下
2. 登錄另一個(gè)實(shí)例數(shù)據(jù)庫,修改上述三個(gè)文件的權(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. 將上述三個(gè)文件copy回源數(shù)據(jù)庫
4. 獲取mysqld的pid,通過kill -HUP `pidof mysqld`方式讓mysqld進(jìn)程重新加載配置文件
[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)上慎用,畢竟安全壓倒一切,天曉得哪里會(huì)出現(xiàn)問題。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com