MySQL中root用戶密碼管理步驟詳解
更新時間:2025年10月30日 08:49:14 作者:huangSir-devops
文章介紹了MySQL中root用戶密碼的管理方式,包括首次部署、修改和忘記密碼的重置,本文分步驟結合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
前言
記錄一下mysql中root用戶密碼的管理方式,mysql中root用戶密碼管理方式主要分為三個場景:
- 場景一:首次部署mysql,需要設置root用戶密碼
- 場景二:已知mysql的root用戶密碼,但是需要修改對應的密碼(例如定期更新密碼)
- 場景三:忘記root密碼,需重置root用戶密碼
首次部署mysql,設置root用戶密碼
方式一:在初始化數(shù)據(jù)庫時設置
#此方式是給root用戶設置臨時密碼,但應用臨時密碼登錄數(shù)據(jù)庫服務后,需要將臨時密碼修改,否則不能進行任何操作 mysqld --initialize --user=mysql --datadir=/data/3306/data --basedir=/usr/local/mysql # 參數(shù)解析 # --user:指定Linux系統(tǒng)用戶 # --datadir:指定數(shù)據(jù)目錄 # --basedir:指定按照目錄
方式二:手動設置
mysql8.0版本
mysqladmin -uroot password '新密碼'; #或者 此方法需要登錄到MySQL系統(tǒng)中 alter user root@'localhost' identified by '新密碼';
mysql5.7版本
mysqladmin -uroot password '新密碼'; #或者 此方法需要登錄到MySQL系統(tǒng)中 alter user root@'localhost' identified by '新密碼';
mysql5.6版本
set password for 'oldboy'@'localhost'=PASSWORD('新密碼');
flush privileges;補充:flush privileges的作用:
- 作用一:將內存中操作的數(shù)據(jù)落到磁盤上
- 作用二:將磁盤中的數(shù)據(jù)加載到內存中
此命令只針對授權表中的數(shù)據(jù)
已知root用戶密碼,修改root用戶密碼
mysql8.0版本
mysqladmin -uroot -p老密碼 password '新密碼' #或者登錄進數(shù)據(jù)庫中執(zhí)行 alter mysql.user root@'localhost' identified by '新密碼';
mysql5.7版本
# 需登錄進數(shù)據(jù)庫中
update mysql.user set authentication_string=PASSWORD('新密碼') where user='root' and host='localhost';
flush privileges; mysql5.6版本
# 需登錄進數(shù)據(jù)庫中
set password for 'root'@'localhost'=PASSWORD('新密碼');
flush privileges;忘記root密碼,需重置root用戶密碼
步驟一:關閉數(shù)據(jù)庫服務
[root@master ~]# /etc/init.d/mysqld stop # 檢查是否已關閉 [root@master ~]# ps -ef | grep mysqld root 4070896 4068830 0 15:24 pts/0 00:00:00 grep mysqld
步驟二:采用安全模式啟動數(shù)據(jù)庫(可以免密連接數(shù)據(jù)庫)
# 輸入mysqld_safe --skip-grant-tables --skip-networking & 即可 [root@master ~]# mysqld_safe --skip-grant-tables --skip-networking & [1] 4071155 [root@master ~]# 2025-10-28T07:25:16.184768Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2025-10-28T07:25:16.211299Z mysqld_safe Starting mysqld daemon with databases from /data00/data/mysql
步驟三:進入數(shù)據(jù)庫修改密碼
[root@master ~]# 2025-10-28T07:25:16.184768Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2025-10-28T07:25:16.211299Z mysqld_safe Starting mysqld daemon with databases from /data00/data/mysql # 輸入mysql -uroot 即可 mysql -uroot # 修改密碼 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> alter user root@'localhost' identified by 'huangsir'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) # 退出數(shù)據(jù)庫 mysql> exit;
步驟四:恢復數(shù)據(jù)庫服務正常啟動
# 先殺掉進程 [root@master ~]# pkill mysql # 檢查進程是否殺掉 [root@master ~]# ps -ef | grep mysql root 4072026 4068830 0 15:29 pts/0 00:00:00 grep mysql # 啟動mysql [root@master ~]# /etc/init.d/mysqld start Starting mysqld (via systemctl): mysqld.service.
測試連接
[root@master ~]# mysql -uroot -phuangsir mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.26 MySQL Community Server - GPL Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
到此這篇關于MySQL中root用戶密碼管理的文章就介紹到這了,更多相關MySQL中root用戶密碼管理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java實現(xiàn)mysql自動更新創(chuàng)建時間與更新時間的兩種方式
在實際開發(fā)中,每條數(shù)據(jù)的創(chuàng)建時間和修改時間,盡量不需要應用程序去記錄,而由數(shù)據(jù)庫獲取當前時間自動記錄創(chuàng)建時間,本文主要介紹了java實現(xiàn)mysql自動更新創(chuàng)建時間與更新時間的兩種方式,感興趣的可以了解一下2024-01-01
MySQL大表數(shù)據(jù)碎片的判斷與整理優(yōu)化實戰(zhàn)指南
在MySQL數(shù)據(jù)庫運維過程中,大表的碎片問題是影響性能和磁盤空間利用率的常見痛點,刪除、更新數(shù)據(jù)后極易產(chǎn)生碎片,導致表文件臃腫、查詢變慢、磁盤空間浪費,本文將從碎片成因、判斷方法、整理操作、注意事項四個維度,手把手教你搞定MySQL大表碎片整理優(yōu)化2026-05-05

