MySQL中root用戶密碼管理步驟詳解
前言
記錄一下mysql中root用戶密碼的管理方式,mysql中root用戶密碼管理方式主要分為三個(gè)場(chǎng)景:
- 場(chǎng)景一:首次部署mysql,需要設(shè)置root用戶密碼
- 場(chǎng)景二:已知mysql的root用戶密碼,但是需要修改對(duì)應(yīng)的密碼(例如定期更新密碼)
- 場(chǎng)景三:忘記root密碼,需重置root用戶密碼
首次部署mysql,設(shè)置root用戶密碼
方式一:在初始化數(shù)據(jù)庫(kù)時(shí)設(shè)置
#此方式是給root用戶設(shè)置臨時(shí)密碼,但應(yīng)用臨時(shí)密碼登錄數(shù)據(jù)庫(kù)服務(wù)后,需要將臨時(shí)密碼修改,否則不能進(jìn)行任何操作 mysqld --initialize --user=mysql --datadir=/data/3306/data --basedir=/usr/local/mysql # 參數(shù)解析 # --user:指定Linux系統(tǒng)用戶 # --datadir:指定數(shù)據(jù)目錄 # --basedir:指定按照目錄
方式二:手動(dòng)設(shè)置
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;補(bǔ)充:flush privileges的作用:
- 作用一:將內(nèi)存中操作的數(shù)據(jù)落到磁盤(pán)上
- 作用二:將磁盤(pán)中的數(shù)據(jù)加載到內(nèi)存中
此命令只針對(duì)授權(quán)表中的數(shù)據(jù)
已知root用戶密碼,修改root用戶密碼
mysql8.0版本
mysqladmin -uroot -p老密碼 password '新密碼' #或者登錄進(jìn)數(shù)據(jù)庫(kù)中執(zhí)行 alter mysql.user root@'localhost' identified by '新密碼';
mysql5.7版本
# 需登錄進(jìn)數(shù)據(jù)庫(kù)中
update mysql.user set authentication_string=PASSWORD('新密碼') where user='root' and host='localhost';
flush privileges; mysql5.6版本
# 需登錄進(jìn)數(shù)據(jù)庫(kù)中
set password for 'root'@'localhost'=PASSWORD('新密碼');
flush privileges;忘記root密碼,需重置root用戶密碼
步驟一:關(guān)閉數(shù)據(jù)庫(kù)服務(wù)
[root@master ~]# /etc/init.d/mysqld stop # 檢查是否已關(guān)閉 [root@master ~]# ps -ef | grep mysqld root 4070896 4068830 0 15:24 pts/0 00:00:00 grep mysqld
步驟二:采用安全模式啟動(dòng)數(shù)據(jù)庫(kù)(可以免密連接數(shù)據(jù)庫(kù))
# 輸入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
步驟三:進(jìn)入數(shù)據(jù)庫(kù)修改密碼
[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ù)庫(kù) mysql> exit;
步驟四:恢復(fù)數(shù)據(jù)庫(kù)服務(wù)正常啟動(dòng)
# 先殺掉進(jìn)程 [root@master ~]# pkill mysql # 檢查進(jìn)程是否殺掉 [root@master ~]# ps -ef | grep mysql root 4072026 4068830 0 15:29 pts/0 00:00:00 grep mysql # 啟動(dòng)mysql [root@master ~]# /etc/init.d/mysqld start Starting mysqld (via systemctl): mysqld.service.
測(cè)試連接
[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>
到此這篇關(guān)于MySQL中root用戶密碼管理的文章就介紹到這了,更多相關(guān)MySQL中root用戶密碼管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql5.7的安裝及Navicate長(zhǎng)久免費(fèi)使用的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了mysql5.7的安裝及Navicate長(zhǎng)久免費(fèi)使用的實(shí)現(xiàn)過(guò)程,本文給大家分享問(wèn)題及解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
The MySQL server is running with the --read-only option so i
1209 - The MySQL server is running with the --read-only option so it cannot execute this statement2020-08-08
MySQL SELECT同時(shí)UPDATE同一張表問(wèn)題發(fā)生及解決
例如用統(tǒng)計(jì)數(shù)據(jù)更新表的字段(此時(shí)需要用group子句返回統(tǒng)計(jì)值),從某一條記錄的字段update另一條記錄,而不必使用非標(biāo)準(zhǔn)的語(yǔ)句,等等感興趣的朋友可以參考下哈2013-03-03
關(guān)于SQL語(yǔ)句中的AND和OR執(zhí)行順序遇到的問(wèn)題
在SQL語(yǔ)句中的AND和OR執(zhí)行順序中我們經(jīng)常會(huì)遇到一些問(wèn)題,下面有簡(jiǎn)單的解決方法,小編來(lái)和大家一起來(lái)看看2019-05-05
MySQL的從庫(kù)Seconds_Behind_Master延遲總結(jié)
這篇文章主要介紹了MySQL的從庫(kù)Seconds_Behind_Master延遲的相關(guān)資料,需要的朋友可以參考下2021-08-08
myeclipse中連接mysql數(shù)據(jù)庫(kù)示例代碼
這篇文章主要為大家詳細(xì)介紹了MyEclipse連接MySQL數(shù)據(jù)庫(kù)圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

