不重啟Mysql修改root密碼的方法
一、一般忘記密碼的解決辦法,需要重啟Mysql
1、skip-grant-tables
我們常用的方法是使用skip-grant-tables選項(xiàng),mysqld server啟動(dòng)之后并不使用權(quán)限系統(tǒng)(privilege system)。用戶不需要任何賬號(hào)、不受任何限制的訪問(wèn)數(shù)據(jù)庫(kù)中所有數(shù)據(jù)。為了安全起見(jiàn),通常加上 skip-networking ,mysqld不偵聽(tīng)任何TCP/IP連接請(qǐng)求。操作過(guò)程如下,
1)修改my.cnf配置文件,在mysqld選項(xiàng)中添加skip-grant-tables和skip-networking。
2)再重啟mysqld server。
3)通過(guò)sql語(yǔ)句修改mysql.user表中存儲(chǔ)密碼。執(zhí)行flush privileges,重新啟用mysql權(quán)限系統(tǒng)。
FLUSH PRIVILEGES;
4)刪除或者注釋配置文件中skip-grant-tables和skip-networking的參數(shù)選項(xiàng)。如果使用skip-networking,則需要再次重啟mysqld。因?yàn)閟kip-networking不是系統(tǒng)變量,只是mysqld的參數(shù)選項(xiàng),而不能通過(guò)系統(tǒng)變量動(dòng)態(tài)進(jìn)行設(shè)置。如果沒(méi)有適用skip-networking,只需要執(zhí)行flush privileges就可以使權(quán)限系統(tǒng)重新生效。
2. --init-file
mysqld_safe可以使–init-file參數(shù)選項(xiàng)來(lái)執(zhí)行重新設(shè)定密碼的sql語(yǔ)句。
1)新建一個(gè)初始化文件,如/tmp/initfile,文件內(nèi)容為上面修改密碼的sql語(yǔ)句。
FLUSH PRIVILEGES;
2)關(guān)閉mysqld服務(wù)進(jìn)程。
3)使用mysqld_safe啟動(dòng)mysqld;
上面的兩種方法是在忘記root密碼情況下重新設(shè)置密碼的方法,可以發(fā)現(xiàn)都需要重啟mysqld服務(wù)。很多人都是使用第一種進(jìn)行重置root密碼,但是比較推薦的做法反而是第二種,即安全有快捷簡(jiǎn)單。
二、不重啟mysqld的方法
1、首先得有一個(gè)可以擁有修改權(quán)限的mysql數(shù)據(jù)庫(kù)賬號(hào),當(dāng)前的mysql實(shí)例賬號(hào)(較低權(quán)限的賬號(hào),比如可以修改test數(shù)據(jù)庫(kù))或者其他相同版本實(shí)例的賬號(hào)。把data/mysql目錄下面的user表相關(guān)的文件復(fù)制到data/test目錄下面。
[root@localhost mysql]# cp mysql/user.* test/
[root@localhost mysql]# chown mysql.mysql test/user.*
2、使用另一個(gè)較低權(quán)限的賬號(hào)鏈接數(shù)據(jù)庫(kù),設(shè)置test數(shù)據(jù)庫(kù)中的user存儲(chǔ)的密碼數(shù)據(jù)。
[root@localhost mysql]# mysql -utest -p12345
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.25a-log Source distribution
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> 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>
3、把修改后的user.MYD和user.MYI復(fù)制到mysql目錄下,記得備份之前的文件。
mv mysql/user.MYD mysql/user.MYD.bak
mv mysql/user.MYI mysql/user.MYI.bak
cp test/user.MY* mysql/
chown mysql.mysql mysql/user.*
4、查找mysql進(jìn)程號(hào),并且發(fā)送SIGHUP信號(hào),重新加載權(quán)限表。
[root@localhost mysql]# pgrep -n mysql
2184
[root@localhost mysql]#
[root@localhost mysql]# kill -SIGHUP 2184
5、登陸測(cè)試
[root@localhost mysql]# mysql -uroot -pyayun
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.5.25a-log Source distribution
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>
相關(guān)文章
基于mysql中delete的語(yǔ)法別名問(wèn)題
這篇文章主要介紹了mysql中delete的語(yǔ)法別名問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09mysql的存儲(chǔ)過(guò)程、游標(biāo) 、事務(wù)實(shí)例詳解
這篇文章主要介紹了mysql的存儲(chǔ)過(guò)程、游標(biāo) 、事務(wù)實(shí)例詳解的相關(guān)資料,這里舉實(shí)例說(shuō)明MySQL 存儲(chǔ)過(guò)程與游標(biāo)和事務(wù),需要的朋友可以參考下2017-08-08Mysql大表全表查詢的全過(guò)程(分析底層的數(shù)據(jù)流轉(zhuǎn)過(guò)程)
這篇文章主要介紹了Mysql大表全表查詢的全過(guò)程(分析底層的數(shù)據(jù)流轉(zhuǎn)過(guò)程),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08mysql 5.7.5 m15 winx64安裝配置方法圖文教程
這篇文章主要為大家分享了mysql 5.7.15 m15 winx64安裝配置方法圖文教程,感興趣的朋友可以參考一下2016-09-09mysql中null(IFNULL,COALESCE和NULLIF)相關(guān)知識(shí)點(diǎn)總結(jié)
這篇文章主要介紹了mysql中null(IFNULL,COALESCE和NULLIF)相關(guān)知識(shí)點(diǎn),結(jié)合實(shí)例形式總結(jié)分析了mysql中關(guān)于null的判斷、使用相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-12-12myeclipse中連接mysql數(shù)據(jù)庫(kù)示例代碼
這篇文章主要為大家詳細(xì)介紹了MyEclipse連接MySQL數(shù)據(jù)庫(kù)圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10