溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL 5.7及8.0版本數(shù)據(jù)庫(kù)的root密碼遺忘的解決

發(fā)布時(shí)間:2020-08-03 19:52:34 來(lái)源:網(wǎng)絡(luò) 閱讀:649 作者:warrent 欄目:MySQL數(shù)據(jù)庫(kù)

注:MySQL5.7破解root密碼,跳過(guò)密碼認(rèn)證登錄到數(shù)據(jù)庫(kù),直接修改表中的密碼即可,但是MySQL 8.0則不可以這樣修改root密碼,需要跳過(guò)密碼認(rèn)證登錄到數(shù)據(jù)庫(kù)后,先將root密碼設(shè)置為空,然后才可以登錄到數(shù)據(jù)庫(kù),修改root密碼。

1、遺忘MySQL 5.7數(shù)據(jù)庫(kù)的root密碼解決辦法

方法1(推薦):
[root@mysql ~]# systemctl stop mysqld        #停止MySQL服務(wù)
[root@mysql ~]# mysqld --user=root --skip-grant-tables    #使用mysqld指令啟動(dòng)mysql服務(wù),跳過(guò)授權(quán)表
#上述命令執(zhí)行后,會(huì)一直占用當(dāng)前終端,需要再開啟一個(gè)終端,
#也不要想著放到后臺(tái)運(yùn)行了,放到后臺(tái)3306端口不會(huì)監(jiān)聽(tīng)的
[root@mysql ~]# ss -anpt | grep 3306     #再開啟一個(gè)終端,確定端口在監(jiān)聽(tīng)
LISTEN     0      80          :::3306                    :::*                   users:(("mysqld",pid=8282,fd=33))
[root@mysql ~]# mysql -uroot           #直接使用root用戶登錄,無(wú)需密碼
mysql> update mysql.user set authentication_string=password('1234') 
    -> where User='root' and Host='localhost';
        #更改root密碼為“1234”
mysql> flush privileges;          #刷新權(quán)限
[root@mysql ~]# kill 8282         #將之前mysqld啟動(dòng)時(shí)占用的終端進(jìn)程號(hào)kill掉,切忌不要使用-9選項(xiàng)
[root@mysql ~]# systemctl start mysqld      #啟動(dòng)MySQL服務(wù),使用新密碼登錄即可

如果上面的過(guò)程中,使用kill -9來(lái)結(jié)束mysqld占用的終端,那么再次啟動(dòng)可能會(huì)報(bào)錯(cuò),sock文件被鎖定,此時(shí),需要將你mysql的sock文件刪除掉,我這里的sock文件在/tmp下,分別時(shí)mysql.sock.lock和mysql.sock這兩個(gè)文件,刪除后再次啟動(dòng)MySQL即可。

方法2:
[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這行下寫入下面內(nèi)容
skip-grant-tables
            .................#省略部分內(nèi)容
[root@mysql01 ~]# systemctl restart mysqld      #重啟MySQL服務(wù),使配置文件生效
[root@mysql01 ~]# mysql -uroot           #跳過(guò)密碼驗(yàn)證,直接登錄數(shù)據(jù)庫(kù)
#修改root密碼為pwd@123,并刷新權(quán)限
mysql> use mysql;
mysql> update user set authentication_string = passwoord('pwd@123') where user = 'root';
mysql> flush privileges;     #刷新權(quán)限
mysql> exit
#配置密碼驗(yàn)證,使用新密碼登錄
[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數(shù)據(jù)庫(kù)的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這行下寫入下面內(nèi)容
skip-grant-tables
            .................#省略部分內(nèi)容
[root@mysql01 ~]# systemctl restart mysqld      #重啟MySQL服務(wù),使配置文件生效
[root@mysql01 ~]# mysql -uroot           #跳過(guò)密碼驗(yàn)證,直接登錄數(shù)據(jù)庫(kù)
#將root密碼設(shè)置為空
mysql> use mysql
mysql> update user set authentication_string='' where user = 'root';
mysql> flush privileges;
mysql> exit
#開啟密碼驗(yàn)證并重新登錄數(shù)據(jù)庫(kù)
[root@mysql01 ~]# vim /etc/my.cnf         #編輯主配置文件
[mysqld] 
skip-grant-tables            #刪除此行
[root@mysql01 ~]# systemctl restart mysqld          #重啟使更改生效
[root@mysql01 ~]# mysql -uroot            #直接登錄數(shù)據(jù)庫(kù)
mysql> alter user root@localhost identified by 'pwd@111';
mysql> flush privileges;
mysql> exit
#使用新密碼進(jìn)行登錄測(cè)試
[root@mysql01 ~]# mysql -uroot -ppwd@111

———————— 本文至此結(jié)束,感謝閱讀 ————————

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI