mysql忘記root密碼

一、mysql 5.x忘記root密碼

1、停止mariadb服務(wù)

systemctl stop mariadb

2、修改配置文件 vim /etc/my.cnf
在[mysqld]下添加skip-grant-tables,意思是忽略密碼, 保存并退出

3、啟動(dòng)Mariadb

systemctl start mariadb

4、修改密碼

# mysql
use mysql;
UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; 
flush privileges ; 
quit

5、改回配置,重啟服務(wù)

vim /etc/my.cnf 刪除skip-grant-tables
systemctl restart mariadb

6、進(jìn)入數(shù)據(jù)庫

# mysql -uroot -p
輸入新密碼new-password即可

二、mysql 8.x忘記root密碼

1、在配置文件mysqld.cnf下添加 skip-grant-tables

vim /etc/mysql/mysql.conf.d/mysqld.cnf
skip-grant-tables

2、重啟MySQL服務(wù)

service mysql restart

3、 修改MySQL密碼

mysql -u root -p

4、選擇管理user表的數(shù)據(jù)庫

use mysql;
 
# 將authentication_string 置空
update user set authentication_string='' where user='root';
# 將plugin改為以前版本的密碼認(rèn)證方式
update user set plugin='mysql_native_password' where user='root';
 
# 刷新
FLUSH PRIVILEGES;
 
# 修改密碼
alter user 'root'@'localhost' identified by 'newpassword';

5、 重啟MySQL

systemctl restart mysqld 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容