一、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