可以停止服務(wù)
5.6及之前版本
1.停服務(wù)
systemctl stop mariadb
2.啟服務(wù)——mysql_safe
mysqld_safe --skip-grant-tables &
#跳過(guò)認(rèn)證授權(quán)表啟動(dòng)MySQL服務(wù)(危險(xiǎn),任意地址無(wú)需用戶密碼驗(yàn)證都可以進(jìn)入)
mysql
#進(jìn)入數(shù)據(jù)庫(kù)
3.修改密碼
update mysql.user set password=password('新密碼') where user='root' and host='localhost';
4.停止服務(wù)
ps -ef |grep mysqld
kill -9 [mysqld_safe的pid]
kill -9 [mysqld的pid]
5.啟服務(wù)
systemctl start mariadb
5.7以及以后版本
1.mysql5.7以后的版本取消了skip-grant-tables,只能通過(guò)改配置文件實(shí)現(xiàn)
sed -i '/[mysqld]/a\skip-grant-tables' /etc/my.cnf
#在mysql的配置文件中的[mysqld]這一行后面追加一行skip-grant-tables
2.重啟服務(wù)
systemctl restart mariadb
mysql
3.修改密碼
update mysql.user set password=password('新密碼') where user='root' and host='localhost';
4.改回配置文件
sed -i 's#skip-grant-tables# #g' /etc/my.cnf
#在mysql的配置文件中的skip-grant-tables替換為空
5.重啟服務(wù)
systemctl restart mariadb
不可以停止服務(wù)
1.擁有一個(gè)低權(quán)限賬號(hào)A(對(duì)某個(gè)庫(kù)db1擁有寫(xiě)權(quán)限的賬號(hào))
2.擁有操作系統(tǒng)root權(quán)限
3.系統(tǒng)root用戶復(fù)制認(rèn)證權(quán)限表mysql.user到A賬號(hào)
cp -p mysql/user* DB1/
4.A賬號(hào)登陸數(shù)據(jù)庫(kù)修改db1下的user表中數(shù)據(jù)庫(kù)root密碼
mysql -utest -ptest123
show database;
use DB1;
show tables;
update DB1.user set password=password('Xin950512') where user='root';
5.系統(tǒng)root用戶備份mysql.user表
tar -cf /tmp/mysql.user.tar /var/lib/mysql/mysql/user*
6.系統(tǒng)root用戶拷貝db1.user表覆蓋mysql.user表
cp -p DB1/user* mysql
7.系統(tǒng)root用戶向mysql發(fā)送SIGHUP信號(hào)重新加載認(rèn)證授權(quán)表
ps -ef |grep mysql
kill -SIGHUP [mysqld的pid]
8.root用戶測(cè)試新密碼登陸
mysql -uroot -pXin950512
9.失敗回滾事項(xiàng)——將備份的認(rèn)證授權(quán)表還原,重新來(lái)過(guò)
注
有錯(cuò)請(qǐng)指出,不喜勿噴,不接受噴我小白,雖然這是事實(shí),如果要噴請(qǐng)預(yù)約