資料庫 MySQL忘記密碼了怎麼辦?

2021-09-04 05:07:45 字數 2029 閱讀 5234

接手乙個專案時,如果上一位負責人沒有把專案文件、賬號密碼整理好是一件很頭疼的事情。。 例如,當你想開啟mysql資料庫的時候 輸入:

mysql -u root -p 一回車想輸入密碼,發現密碼錯誤!!

# [mysql for linux錯誤 error 1045 (28000): access denied for user 'root'@'localhost' (using password: yes)](

立馬有種想要砸鍵盤的衝動~不過別慌,程式設計師最厲害的地方就是,上網找解決方案哈哈;

下面就和大家分享一下mysql忘記密碼的解決方案

windows下修改的是my.ini,這裡主要說的是linux的具體操作,

linux系統使用 whereis 能夠很方便地找到檔案的位置:

$ whereis my

$ my:/etc/my.cnf

如果你是 root 使用者,直接vim /etc/my.cnf (注:windows下修改的是my.ini)

如果你是普通使用者 , sudo vim /etc/my.cnf

按 esc 退出編輯,鍵入:wq,回車即儲存並退出。

$ sudo /etc/init.d/mysqld restart
普通使用者在這個命令最前面加上sudo,root 使用者就不用啦。

$ mysql -u root -p
出現entering password 無需理會,回車即可進入 mysql 資料庫。

進入到 mysql 資料庫之後,我們就可以更新 "mysql"資料庫中「user」表的 root 記錄的密碼了

mysql > use mysql;

mysql > update user set password=password('your_new_password') where user='root';

mysql > flush privileges;

步驟解析:​use mysql; 進入到名為 「mysql」 的資料庫,

​然後使用 update 語句更新密碼,

​更新密碼之後使用 flush privileges; 重新整理許可權。

注意:password()函式是用於密碼儲存的加密函式,該函式的加密過程不可逆!因此除非用大量的資料進行比對,不然沒辦法使用什麼逆函式檢視舊的密碼,只能使用該函式更新密碼,your_new_password 就是自己設定的新密碼。

設定好新密碼之後,ctrl + d退出 mysql 資料庫。

重新用 vim 開啟 my.cnf ,將剛才的 skip-grant-tables 刪除或者在前面加上#號注釋,儲存並退出。

$ sudo /etc/init.d/mysqld restart
$ mysql -u root -p
出現entering password 時,輸入你的新密碼,回車進入,出現 mysql 歡迎介面,恭喜你,修改密碼完成!

本篇文章中提到mysql版本是5.6 ,mysql8.+ 的密碼字段已經不是password,改為了authentication_string ,

如果是使用 mysql8.+ 的朋友記得把更新語句改為:

update user set authentication_string=password('your_new_password') where user='root';

mysql資料庫忘記密碼了怎麼辦?

mysql有時候忘記密碼了怎麼辦?我給出案例和說明 一下就解決了 windows下的實際操作如下 1.關閉正在執行的mysql。2.開啟dos視窗,轉到mysql bin目錄。3.輸入mysqld skip grant tables回車。如果沒有出現提示資訊,那就對了。4.再開乙個dos視窗 因為剛...

mysql資料庫忘記密碼

方法一 1 在dos視窗下輸入net stop mysql5或net stop mysql 2 開乙個dos視窗,這個需要切換到mysql的bin目錄。一般在bin目錄裡面建立乙個批處理1.bat,內容是cmd.exe執行一下即可就切換到當前目錄,然後輸入 mysqld nt skip grant ...

mysql資料庫忘記密碼

mysql資料庫忘記root密碼如何處理 1.關閉mysql資料庫 service mysql stop 2.找到etc my.cnf 檔案 在mysqld下面增加 skip grant tables skip name resolve 3.啟動mysql資料庫 service mysql star...