1. 程式人生 > 資料庫 >MySQL修改密碼的幾種方式

MySQL修改密碼的幾種方式

前言:

在日常使用資料庫的過程中,難免會遇到需要修改賬號密碼的情景,比如密碼太簡單需要修改、密碼過期需要修改、忘記密碼需要修改等。本篇文章將會介紹需要修改密碼的場景及修改密碼的幾種方式。

1.忘記 root 密碼

忘記 root 密碼的場景還是比較常見的,特別是自己搭的測試環境經過好久沒用過時,很容易記不得當時設定的密碼。這個時候一般常用的方法是跳過許可權驗證,然後更改 root 密碼,之後再啟用許可權驗證。以 MySQL 5.7 版本為例簡單講下主要過程:

首先修改配置檔案,在[mysqld]部分加上一句:skip-grant-tables ,加上此引數的目的是跳過許可權驗證。然後重啟資料庫,資料庫再次啟動後,我們就可以不用密碼直接登入資料庫修改密碼了。

#skip-grant-tables模式下修改root密碼
[root@host~]#mysql
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis16
Serverversion:5.7.23-logMySQLCommunityServer(GPL)

Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved.

OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.

Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>updatemysql.usersetauthentication_string=password('xxxxxx')whereuser='root'andhost='localhost';
QueryOK,0rowsaffected,1warning(0.00sec)
Rowsmatched:1Changed:0Warnings:1

mysql>flushprivileges;
QueryOK,0rowsaffected(0.01sec)

修改完 root 密碼後,再次去除 skip-grant-tables 引數,然後重啟下資料庫即可。

2.幾種修改密碼的方法

除去忘記密碼,可能還有其他情景需要修改密碼,這時候就可以採取普通方式修改密碼了。還是以 MySQL 5.7 版本為例,介紹幾種常用的修改密碼的方法。

使用 alter user 修改

比如如果想更改 testuser 賬號的密碼,我們可以使用 root 賬號登入,然後執行 alter user 命令更改 testuser 賬號的密碼。

mysql>alteruser'testuser'@'%'identifiedby'Password1';
QueryOK,0rowsaffected(0.01sec)

mysql>flushprivileges;
QueryOK,0rowsaffected(0.00sec)

使用 SET PASSWORD 命令

使用 SET PASSWORD 修改密碼命令格式為 SET PASSWORD FOR 'username'@'host' = PASSWORD('newpass'); 同樣是使用 root 賬號可修改其他賬號的密碼。

mysql>SETPASSWORDFOR'testuser'@'%'=PASSWORD('Password2');
QueryOK,1warning(0.00sec)

mysql>flushprivileges;
QueryOK,0rowsaffected(0.00sec)

使用 mysqladmin 修改密碼

使用 mysqladmin 命令修改賬號密碼格式為 mysqladmin -u使用者名稱 -p舊密碼 password 新密碼

[root@host~]#mysqladmin-utestuser-pPassword2passwordPassword3
mysqladmin:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.
Warning:Sincepasswordwillbesenttoserverinplaintext,usesslconnectiontoensurepasswordsafety.
[root@host~]#mysql-utestuser-pPassword3
mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2388
Serverversion:5.7.23-logMySQLCommunityServer(GPL)

Copyright(c)2000,Oracleand/oritsaffiliates.Allrightsreserved.

OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.

Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>

直接 update user 表

其實 MySQL 所以的賬號資訊都儲存在 mysql.user 表裡面,我們也可以直接通過 update user 表來修改密碼。

#5.7及之後版本
mysql>updatemysql.usersetauthentication_string=password('Password4')whereuser='testuser'andhost='%';
QueryOK,1rowaffected,1warning(0.06sec)
Rowsmatched:1Changed:1Warnings:1

mysql>flushprivileges;
QueryOK,0rowsaffected(0.01sec)

#5.6及之前版本
updatemysql.usersetpassword=password('新密碼')whereuser='使用者名稱'andhost='host';

3.設定 login-path 本地快捷登陸

為了防止密碼暴露及忘記密碼,我們還可以設定 login-path 來實現在本地不輸密碼快捷登入。

login-path 是 MySQL 5.6 開始支援的新特性。通過藉助 mysql_config_editor 工具將登陸 MySQL 服務的認證資訊加密儲存在 .mylogin.cnf 檔案(預設位於使用者主目錄)。MySQL 客戶端工具可通過讀取該加密檔案連線 MySQL ,實現快捷登入。

假設我們想配置 root 賬號在本地快捷登入,可以這麼做:

#執行回車後需要輸入一次root密碼
[root@host~]#mysql_config_editorset--login-path=root-uroot-hlocalhost-p-P3306
Enterpassword:

#配置完成後可以使用login-path登入
[root@host~]#mysql--login-path=root
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2919
Serverversion:5.7.23-logMySQLCommunityServer(GPL)

Copyright(c)2000,Oracleand/oritsaffiliates.Allrightsreserved.

OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.

Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.

mysql>

總結:

本篇文章主要介紹了修改資料庫賬號密碼的幾種方法,基本涵蓋了所有的場景。這裡也提醒下各位,資料庫賬號最好限制ip段登入,密碼儘量複雜些,最好能夠定期修改,特別是重要的環境不能有半點馬虎。年底了,安全才是王道。

以上就是MySQL修改密碼的幾種方式的詳細內容,更多關於MySQL修改密碼的資料請關注我們其它相關文章!