Mysql8 忘記Root密碼(轉)

第一步:修改配置文件免密碼登陸mysqlhtml

vim /etc/my.cnf

1.2 在 [mysqld]最後加上以下語句 並保持退出文件;mysql

skip-grant-tables

1.3 重啓mysql服務:

sql

service mysqld restart

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

  1.  
    mysql
  2.  
    //或者
  3.  
    mysql -u root -p
  4.  
    //password直接回車

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

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

select host, user, authentication_string, plugin from user;

host: 容許用戶登陸的ip‘位置’%表示能夠遠程;加密

user:當前數據庫的用戶名;.net

authentication_string: 用戶密碼;在mysql 5.7.9之後廢棄了password字段和password()函數;命令行

plugin: 密碼加密方式;rest

3.2 若是當前root用戶authentication_string字段下有內容,先將其設置爲空;

  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爲空,因此能夠免密碼登陸;

  1.  
    mysql -u root -p
  2.  
    passwrod:直接回車;

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

ALTER user 'root'@'localhost' IDENTIFIED BY 'Qian123#'

至此修改爲功; 重新使用用戶名密碼登陸便可;

 

修改中遇到的問題:

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

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

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

  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. 必定不要採起以下形式該密碼:

  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

相關文章
相關標籤/搜索