MySQL8.0 忘記 root 密碼下如何修改密碼

zhangdeTalk發表於2019-12-21

環境:Linux centos8 4.18.0-80.el8.x86_64、Mysql8.0.18

1、vim /etc/my.cnf,新增skip-grant-table(跳過許可權驗證)

Mysql8.0忘記root密碼下如何修改密碼

2、重啟mysql服務

systemctl stop mysqld //停止服務
systemctl restart mysqld //重啟服務
systemctl status mysqld //服務狀態檢視

MySQL8.0 忘記 root 密碼下如何修改密碼

3、重新整理許可權表:flush privileges;

MySQL8.0 忘記 root 密碼下如何修改密碼

4、mysql登入:mysql -uroot -p(無需密碼)

MySQL8.0 忘記 root 密碼下如何修改密碼

5、密碼重置(Mysql8.0+有變化)

先把root的舊密碼置空
use mysql;
update user set authentication_string='' where user='root';
備註:Mysql5.7+ password欄位 已改成 authentication_string欄位

MySQL8.0 忘記 root 密碼下如何修改密碼

重置成新密碼
alter user 'root'@'localhost' identified by 'newpassword';
備註:Mysql8.0修改密碼方式已有變化(此處是個坑,需要注意)
Mysql8.0之前:
update user set password=password('root') where user='root';

MySQL8.0 忘記 root 密碼下如何修改密碼

6、vim /etc/my.cnf,刪除skip-grant-table

7、再次重啟mysql服務

8、使用新密碼登入mysql

mysql -uroot -proot

MySQL8.0 忘記 root 密碼下如何修改密碼

相關文章