場(chǎng)景說(shuō)明
要連接到某服務(wù)的數(shù)據(jù)庫(kù),但是忘記了root密碼,試了幾次都無(wú)法登陸
找回密碼步驟
1.在/etc/my.cnf [MySQLd] 配置部分添加"skip-grant-tables"
vim /etc/my.cnf
[mysqld]
skip-grant-tables
2.重啟mysql服務(wù)
systemctl restart mysqld
3.登入mysql
mysql -uroot -p123 #密碼隨便輸入都可以
4.刷新一下庫(kù)權(quán)限
mysql> flush privileges;
如果不執(zhí)行這句話,會(huì)報(bào)以下錯(cuò)誤! ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
5.新增一個(gè)遠(yuǎn)程管理員賬號(hào),然后退出數(shù)據(jù)庫(kù)
grant all privileges on *.* to 'admin'@'%' identified by 'foshan123' with grant option;
6.去除配置后重啟數(shù)據(jù)庫(kù)
(1)注釋/etc/my.cnf中skip-grant-tables
(2)執(zhí)行systemctl restart mysqld命令重啟數(shù)據(jù)庫(kù)