1.在 /etc/my.cnf 中的 [mysqld] 中添加 skip-grant-table
2.使用 [root]# server mysql restart 重啟服務
3.輸入 [root]# mysql 無密碼登錄到mysql中
4.使用 mysql > use mysql #選擇數據庫
5.使用 mysql > update user set authentication_string='' where user='root'; #將root的權限驗證字符串更新為空
6.使用 mysql > select Host,User,authentication_string from user; #查詢是否更新成功
7.使用 mysql > flush privileges #刷新權限
8.使用 mysql > exit #退出MySQL
9.在配置文件[root]# vim /etc/my.cnf 中注釋 #skip-grant-table
10.重復第2步驟 重啟服務
11.使用 [root]# mysql -uroot -p 后直接回車登錄到MySQL中 【注意其他語句可能有兼容問題導致【Mysql8.0】ERROR 1820 (HY000): You must reset your password using ALTER USER statement】
12.使用 mysql > set password='your password'; #輸入密碼
13.重復第7步。