MySQL 密碼找回

環(huán)境

centos 7.x
MySQL 5.7.20

前提

  • 1.MySQL 可以重啟
  • 2.可以登錄服務(wù)器

操作

  • 1.原密碼
[root@1]# mysql -uroot -p12345
mysql: [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 4
Server version: 5.7.20-log MySQL Community Server (GPL)

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

(root@localhost) [(none)]>

  • 2.假設(shè)密碼忘記
- 1.配置 my.cnf
vim /etc/my.cnf // 注意每個配置文件不一定位置相同
[mysqld]
skip-grant-tables // 寫入 my.cnf 配置文件

- 2.重啟 MySQL
service mysqld restart // 重啟數(shù)據(jù)庫方法根據(jù)實際情況決定
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
  • 3.修改密碼
[root@1]# mysql // 以上做完之后,可以直接登錄
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.20-log MySQL Community Server (GPL)

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

(root@localhost) [(none)]> alter user 'root'@'localhost' identified by '123'; // 這時由于系統(tǒng)檢測 skip grant,是不會讓你修改密碼的潜索。
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

(root@localhost) [(none)]>flush PRIVILEGES; // 再次讓密碼生效
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [(none)]>alter user 'root'@'localhost' identified by '123'; // 修改密碼
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]>flush tables; // 刷內(nèi)存數(shù)據(jù)
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]>quit // 退出
Bye
  • 4.成功現(xiàn)象
[root@1]# mysql // 密碼生效藏斩,skip grant 失效
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@1]# mysql -uroot -p123 // 這時使用密碼登錄章咧,就可以正常使用了
mysql: [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 4
Server version: 5.7.20-log MySQL Community Server (GPL)

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

(root@localhost) [(none)]>

注意:記得把配置文件中 skip grant 注釋掉

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末惨险,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子催束,更是在濱河造成了極大的恐慌嘶摊,老刑警劉巖延蟹,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異叶堆,居然都是意外死亡阱飘,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沥匈,“玉大人蔗喂,你說我怎么就攤上這事「咛” “怎么了缰儿?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長散址。 經(jīng)常有香客問我乖阵,道長,這世上最難降的妖魔是什么预麸? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任义起,我火速辦了婚禮,結(jié)果婚禮上师崎,老公的妹妹穿的比我還像新娘。我一直安慰自己椅棺,他們只是感情好犁罩,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著两疚,像睡著了一般床估。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上诱渤,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天丐巫,我揣著相機與錄音,去河邊找鬼勺美。 笑死递胧,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的赡茸。 我是一名探鬼主播缎脾,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼占卧!你這毒婦竟也來了遗菠?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤华蜒,失蹤者是張志新(化名)和其女友劉穎辙纬,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體叭喜,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡贺拣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纵柿。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡蜈抓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出昂儒,到底是詐尸還是另有隱情沟使,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布渊跋,位于F島的核電站腊嗡,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拾酝。R本人自食惡果不足惜燕少,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蒿囤。 院中可真熱鬧客们,春花似錦、人聲如沸材诽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脸侥。三九已至建邓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間睁枕,已是汗流浹背官边。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留外遇,地道東北人注簿。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像跳仿,于是被迫代替她去往敵國和親滩援。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容