Centos7重置Mysql 8.0.1 root 密碼


問題產生背景:

安裝完 最新版的 mysql8.0.1后忘記了密碼,向重置root密碼;找了網上好多資料都不盡相同,根據自己的問題總結如下:

第一步:修改配置文件免密碼登錄mysql

[html]  view plain copy
 
  1. vim /etc/my.cnf  

1.2 在 [mysqld]最后加上如下語句 並保持退出文件;

[html]  view plain copy
 
  1. skip-grant-tables  

1.3 重啟mysql服務:

[html]  view plain copy
 
  1. service mysqld restart  

第二步免密碼登錄到mysql上;直接在命令行上輸入:

[html]  view plain copy
 
  1. mysql  
  2. //或者  
  3. mysql -u root -p   
  4. //password直接回車  

第三步: 給root用戶重置密碼;

3.1 首先查看當前root用戶相關信息,在mysql數據庫的user表中;

[html]  view plain copy
 
  1. 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字段下有內容,先將其設置為空;

[html]  view plain copy
 
  1. use mysql;  
  2. update user set authentication_string='' where user='root';  

3.3 退出mysql, 刪除/etc/my.cnf文件最后的 skip-grant-tables 重慶mysql服務;

3.4 使用root用戶進行登錄,因為上面設置了authentication_string為空,所以可以免密碼登錄;

[html]  view plain copy
 
  1. mysql -u root -p  
  2. passwrod:直接回車;  

3.5使用ALTER修改root用戶密碼;

[html]  view plain copy
 
  1. ALTER user 'root'@'localhost' IDENTIFIED BY 'Qian123#'  

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

 

修改中遇到的問題:

1. 根據網上的這篇文章進行修改,報錯;

網友文章:Linux-CentOS7下修改root密碼和密碼過期問題

在使用這句話修改密碼時報錯:

[html]  view plain copy
 
  1. update user set password = password('new-password') where user = 'root' ;   
  2.   
  3. or  
  4.   
  5. update user set authentication_string= password('new-password') where user = 'root' ;   

報錯原因:mysql5.7.6以后廢棄了user表中的password字段和 password() 方法;

所以上面的方法對 mysql8.0.1是行不通的;

 

2. 根據網友的這篇文章進行修改,報錯;

網友文章: 修改MySQL 5.7.9版本的root密碼方法以及一些新變化整理

3. 參考MYSQL8的官網文檔, 感覺寫的也很水;

MySQL8官網文檔: mysql8.0 reference for manual

4. 一定不要采取如下形式該密碼:

[html]  view plain copy
 
  1. use mysql;  
  2. update user set authentication_string="newpassword" where user="root";  

這樣會給user表中root用戶的authentication_string字段下設置了newpassword值;

當再使用ALTER USER 'root'@'localhost' IDENTIFITED BY 'newpassword'時會報錯的;

因為authentication_string字段下只能是mysql加密后的41位字符串密碼;其他的會報格式錯誤;

*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM