1. 程式人生 > >MySQL 8.0.12忘記root密碼如何找回

MySQL 8.0.12忘記root密碼如何找回

今天安裝了win7 64bit的MySQL 社群版,版本號為 8.0.12,安裝步驟:(參考https://www.cnblogs.com/reyinever/p/8551977.html)

  1. 下載、解壓介質
  2. 設定MySQL_HOME環境變數,並將";%MYSQL_HOME%\bin"新增到PATH最後(不要忽略首位分號)
  3. 生成data檔案(管理員執行CMD,cd到bin/,“mysqld --initialize-insecure --user=mysql”)
  4. “mysql -install”(避免啟動mysql報錯“服務名無效”)
  5. 啟動mysql,net start mysql

啟動後首次使用root連線無需密碼,登入後嘗試更改root密碼:“update mysql.user set authentication_string=password(‘123456’) where user = ‘root’;”
結果報錯,報錯資訊為“”
刪掉password字樣,update執行成功,flush後重連,結果使用新密碼死活無法登入root

只好設定跳過密碼驗證

  1. 停止mysql服務:“net stop mysql”
  2. 設定跳過驗證:“mysqld --shared-memory --skip-grant-tables”,(注意:一定要有–shared-memory,否則無法正常設定–skip-grant-tables並啟動mysql服務)
  3. 無密碼登入:在mysql/bin/目錄下新開一個CMD視窗,無需重複啟動mysql,直接輸入"mysql",此時應該可以連線成功

重設root密碼時又遇到問題
由於此時處於"–skip-grant-tables",故使用"ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’" 無法修改root密碼,會報錯;
嘗試使用“update mysql.user set _string=password(‘123456’) where user = ‘root’”;再次報錯,提示(‘123456’)附近有語法錯誤,刪去‘password()’字樣可執行,但此時依舊無法使用新密碼登入.

最後使用update語句,將root密碼設定為空update mysql.user set authertication_string=’’ where user = ‘root’;),竟然可以"mysql -uroot -p"顯示"Enter password:"時直接回車,無密碼正常登入。

原因: