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

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

  1. 在 /etc/my.cnf 文件末尾追加 skip-grant-tableshtml

    [root@abdefg 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服務mysql

    [root@abdefg mysql]# service mysqld restart
  3. 免密碼登陸 root 用戶(輸入密碼時直接回車便可)sql

    [root@abdefg 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

    查詢一下vim

    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失敗this

    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服務,修改完成編碼

參考文檔:網絡螞蟻 奧斯維克雞腿學徒.net

相關文章
相關標籤/搜索