1. 程式人生 > >MySQL 8.0 以上版本重置 root 使用者密碼

MySQL 8.0 以上版本重置 root 使用者密碼

MySQL 8.0 以上版本重置 root 使用者密碼

  1. 在 /etc/my.cnf 檔案末尾追加 skip-grant-tables

     [[email protected] mysql]# vim /etc/my.cnf
     [mysql]
     # 設定mysql客戶端預設字符集
     default-character-set=utf8mb4 
    
     [mysqld]
     # 設定3306埠
     port = 3306 
     # 設定mysql的安裝目錄
     basedir=/usr/local/mysql
     # 設定mysql資料庫的資料的存放目錄
     datadir=/usr/local/mysql/data
     # 允許最大連線數
     max_connections=200
     # 服務端預設編碼(資料庫級別)
     character-set-server=utf8mb4
     # 建立新表時將使用的預設儲存引擎
     default-storage-engine=INNODB 
     lower_case_table_names=1
     max_allowed_packet=16M
     skip-grant-tables
    
  2. 重啟MySQL服務

     [[email protected] mysql]# service mysqld restart
    
  3. 免密碼登入 root 使用者(輸入密碼時直接回車即可)

     [[email protected] bin]# mysql -uroot -p
     Enter password: 
     Welcome to the MySQL monitor.  Commands end with ; or \g.
     Your MySQL connection id is 8
     Server version: 8.0.13 MySQL Community Server - GPL
    
     Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
     Oracle is a registered trademark of Oracle Corporation and/or its
     affiliates. Other names may be trademarks of their respective
     owners.
    
     Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
     mysql> 
    
  4. 重置密碼

     mysql> use mysql
    

    查詢一下

     mysql> select host, user, authentication_string, plugin from user;
    

    執行修改報錯

     mysql> alter user 'root'@'localhost'IDENTIFIED BY 'newpassword[@123](https://my.oschina.net/u/9520)';
     ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    

    先執行flush privileges;然後會alter成功,要不然會alter失敗

     mysql> flush privileges;
     Query OK, 0 rows affected (0.00 sec)
    
     mysql> alter user 'root'@'localhost'IDENTIFIED BY 'newpassword[@123](https://my.oschina.net/u/9520)';
     Query OK, 0 rows affected (0.03 sec)
    
     mysql> exit
     Bye
    
  5. 把/etc/my.cnf 檔案末尾的 skip-grant-tables 去掉,重啟MySQL服務,修改完成

參考文件:網路螞蟻 奧斯維克雞腿學徒