MySQL 8.0 以上版本重置 root 用戶密碼的終極辦法


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

 [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服務

service mysqld restart

3.免密碼登錄 root 用戶(輸入密碼時直接回車即可)

[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
mysql> select host, user, authentication_string, plugin from user;

注意:先執行flush privileges;然后會alter成功,要不然會alter失敗

mysql> alter user 'root'@'localhost'IDENTIFIED BY 'newpassword';

5. 修改密碼安全級別

set global validate_password.policy=0;
set global validate_password.length=1;

6. 把/etc/my.cnf 文件末尾的 skip-grant-tables 去掉,重啟MySQL服務,修改完成。

 


免責聲明!

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



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