編碼的世界 / 優質文選 / 歷史

CentOS中對MySql的root用戶重置密碼


2022年5月15日
-   

使用修改啟動服務參數的方式來更新user表,達到無密碼登陸的目的。
1.關閉系統下運行的mysql服務
(1)殺掉進程號達到關閉目的
先使用ps -ef|grep mysql查找進程號,然後使用kill+ PID的方式殺掉進程。
(2)使用systemctl工具來關閉mysql服務
systemctl stop mysql
2.修改mysql的啟動選項更新user表,達到無密碼登陸的效果
systemctl set-environment MYSQLD_OPTS="skip-grant-tables";
3.重新啟動mysql
systemctl start mysql
4.無密碼登錄mysql數據庫
mysql -u root
5.修改密碼
首先使用 flush privileges刷新權限表
然後使用alter user語句修改用戶登錄位置以及密碼
alter user 'root'@'localhost' identified by 'password'
5.6版本的mysql語句修改為
update mysql.user set password=password(‘**********’) where host='localhost' and user='root';
5.7(authentication_string)
再次刷新權限表
6.再次關閉mysql服務
7.恢複mysql啟動選項
systemctl unset-enviroment MYSQL_OPTS
8.開啟mysql服務
9.使用mysql -u root -p登錄mysql
新mysql設置密碼
格式:mysql> set password for 用戶名@localhost = password('新密碼');
格式:mysqladmin -u用戶名 -p舊密碼 password 新密碼
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
mysql> flush privileges;
mysql>grant all on *.* to 'root'@'localhost' IDENTIFIED BY '你的密碼'with grant option ;mysql>flush privileges;

熱門文章