MySQL 5.7及8.0版本數據庫的root密碼遺忘的解決辦法

注:MySQL5.7破解root密碼,跳過密碼認證登錄到數據庫,直接修改表中的密碼即可,但是MySQL 8.0則不可以這樣修改root密碼,需要跳過密碼認證登錄到數據庫後,先將root密碼設置爲空,然後纔可以登錄到數據庫,修改root密碼。

1、遺忘MySQL 5.7數據庫的root密碼解決辦法

[root@mysql01 ~]# mysql --version        #確定MySQL版本
mysql  Ver 14.14 Distrib 5.7.28, for linux-glibc2.12 (x86_64) using  EditLine wrapper
[root@mysql01 ~]# vim /etc/my.cnf         #編輯主配置文件
[mysqld]      #在mysqld這行下寫入下面內容
skip-grant-tables
            .................#省略部分內容
[root@mysql01 ~]# systemctl restart mysqld      #重啓MySQL服務,使配置文件生效
[root@mysql01 ~]# mysql -uroot           #跳過密碼驗證,直接登錄數據庫
#修改root密碼爲pwd@123,並刷新權限
mysql> use mysql;
mysql> update user set authentication_string = passwoord('pwd@123') where user = 'root';
mysql> flush privileges;     #刷新權限
mysql> exit
#配置密碼驗證,使用新密碼登錄
[root@mysql01 ~]# vim /etc/my.cnf         #編輯主配置文件
[mysqld] 
skip-grant-tables            #刪除此行
[root@mysql01 ~]# systemctl restart mysqld          #重啓使更改生效
#使用新密碼即可成功登錄
[root@mysql01 ~]# mysql -uroot -ppwd@123       

2、遺忘MySQL 8.0數據庫的root密碼解決辦法

[root@mysql01 ~]# mysql --version        #查看MySQL版本
mysql  Ver 8.0.18 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
[root@mysql01 ~]# vim /etc/my.cnf         #編輯主配置文件
[mysqld]      #在mysqld這行下寫入下面內容
skip-grant-tables
            .................#省略部分內容
[root@mysql01 ~]# systemctl restart mysqld      #重啓MySQL服務,使配置文件生效
[root@mysql01 ~]# mysql -uroot           #跳過密碼驗證,直接登錄數據庫
#將root密碼設置爲空
mysql> use mysql
mysql> update user set authentication_string='' where user = 'root';
mysql> flush privileges;
mysql> exit
#開啓密碼驗證並重新登錄數據庫
[root@mysql01 ~]# vim /etc/my.cnf         #編輯主配置文件
[mysqld] 
skip-grant-tables            #刪除此行
[root@mysql01 ~]# systemctl restart mysqld          #重啓使更改生效
[root@mysql01 ~]# mysql -uroot            #直接登錄數據庫
mysql> alter user root@localhost identified by 'pwd@111';
mysql> flush privileges;
mysql> exit
#使用新密碼進行登錄測試
[root@mysql01 ~]# mysql -uroot -ppwd@111

———————— 本文至此結束,感謝閱讀 ————————

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章