1. 程式人生 > 實用技巧 >MySQL忘記root密碼,錯誤號碼1045解決辦法

MySQL忘記root密碼,錯誤號碼1045解決辦法

停止MySQL服務

Windows可以右鍵我的電腦--管理--服務和應用程式--服務--找到對應的服務停止掉

免密登入

切換到MySQL安裝路徑下:D:\mysql-5.7.24-winx64\bin;如果已經配了環境變數,則不用再安裝目錄

開啟CMD命令視窗,在命令列輸入:mysqld -nt --skip-grant-table

8.0以上資料庫輸入:mysqld --console --skip-grant-tables --shared-memory

進入資料庫

以管理員身份重新啟動一個cmd命令視窗,輸入:mysql -uroot -p,Enter進入資料庫

如果不想改密碼,只是想看原來的密碼的話,可以在命令列執行這個語句

select host,user,password from mysql.user;//即可檢視到使用者和密碼

注意:MySQL5.7以上的沒有password欄位了,被加密了改成authentication_string

修改密碼

在命令列下 依次 執行下面的語句 

use mysql
update user set password=password("new_password") where user="root";// 'new_password' 這裡改為你要設定的密碼
flush privileges;
quit

注意:MySQL5.7以上更改語句替換為

update mysql.user set authentication_string=password('new_password') where user='root';// 'new_password' 這裡改為你要設定的密碼

MySQL5.7版本密碼設定規則:
必須至少包含一個大寫字母、一個小寫字母、一個特殊符號、一個數字,密碼長度至少為8個字元

重啟MySQL

重新啟動MYSQL,輸入密碼登入即可!

mysql -u root -p 輸入密碼,即可登入成功