編碼的世界 / 優質文選 / 歷史

mysql8.0以上的版本忘記root密碼如何重置


2021年9月04日
-   

轉載自網絡螞蟻大大的https://www.cnblogs.com/jjg0519/p/9034713.html
作者還舉例了過程中遇到的問題,比如password字段及其函數 ,最新的版本已經不可用了。
安裝完 最新版的 mysql8.0.1後忘記了密碼,向重置root密碼;找了網上好多資料都不盡相同,根據自己的問題總結如下:
第一步:修改配置文件免密碼登錄mysql
[html] view plain copy
 
  • vim /etc/my.cnf  

  • 1.2 在 [mysqld]最後加上如下語句 並保持退出文件;
    [html] view plain copy
     
  • skip-grant-tables  

  • 1.3 重啟mysql服務:  
  • service mysqld restart  

  • 第二步免密碼登錄到mysql上;直接在命令行上輸入:
  • mysql  
  • //或者  
  • mysql -u root -p   
  • //password直接回車  

  • 第三步: 給root用戶重置密碼;
    3.1 首先查看當前root用戶相關信息,在mysql數據庫的user表中;
  • select host, user, authentication_string, plugin from user;  

  • host: 允許用戶登錄的ip‘位置’%表示可以遠程;
    user:當前數據庫的用戶名;
    authentication_string: 用戶密碼;在mysql 5.7.9以後廢棄了password字段和password()函數;
    plugin: 密碼加密方式;
    3.2 如果當前root用戶authentication_string字段下有內容,先將其設置為空;
  • use mysql;  
  • update user set authentication_string='' where user='root';  

  • 3.3 退出mysql, 刪除/etc/my.cnf文件最後的 skip-grant-tables 重慶mysql服務;
    3.4 使用root用戶進行登錄,因為上面設置了authentication_string為空,所以可以免密碼登錄;
  • mysql -u root -p  
  • passwrod:直接回車;  

  • 3.5使用ALTER修改root用戶密碼;
  • ALTER user 'root'@'localhost' IDENTIFIED BY 'Qian123#'  

  • 至此修改成功; 從新使用用戶名密碼登錄即可;

    熱門文章