MySQL修改密碼的幾種方式
:
在日常使用數(shù)據(jù)庫的過程中,難免會(huì)遇到需要修改賬號(hào)密碼的情景,比如密碼太簡單需要修改、密碼過期需要修改、忘記密碼需要修改等。本篇文章將會(huì)介紹需要修改密碼的場景及修改密碼的幾種方式。
1.忘記 root 密碼
忘記 root 密碼的場景還是比較常見的,特別是自己搭的測試環(huán)境經(jīng)過好久沒用過時(shí),很容易記不得當(dāng)時(shí)設(shè)置的密碼。這個(gè)時(shí)候一般常用的方法是跳過權(quán)限驗(yàn)證,然后更改 root 密碼,之后再啟用權(quán)限驗(yàn)證。以 MySQL 5.7 版本為例簡單講下主要過程:
首先修改配置文件,在[mysqld]部分加上一句:skip-grant-tables ,加上此參數(shù)的目的是跳過權(quán)限驗(yàn)證。然后重啟數(shù)據(jù)庫,數(shù)據(jù)庫再次啟動(dòng)后,我們就可以不用密碼直接登錄數(shù)據(jù)庫修改密碼了。
#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 參數(shù),然后重啟下數(shù)據(jù)庫即可。
2.幾種修改密碼的方法
除去忘記密碼,可能還有其他情景需要修改密碼,這時(shí)候就可以采取普通方式修改密碼了。還是以 MySQL 5.7 版本為例,介紹幾種常用的修改密碼的方法。
使用 alter user 修改
比如如果想更改 testuser 賬號(hào)的密碼,我們可以使用 root 賬號(hào)登錄,然后執(zhí)行 alter user 命令更改 testuser 賬號(hào)的密碼。
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 賬號(hào)可修改其他賬號(hào)的密碼。
mysql>SETPASSWORDFOR'testuser'@'%'=PASSWORD('Password2'); QueryOK,0rowsaffected,1warning(0.00sec) mysql>flushprivileges; QueryOK,0rowsaffected(0.00sec)
使用 mysqladmin 修改密碼
使用 mysqladmin 命令修改賬號(hào)密碼格式為 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,2018,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>
直接 update user 表
其實(shí) MySQL 所以的賬號(hào)信息都存儲(chǔ)在 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.設(shè)置 login-path 本地快捷登陸
為了防止密碼暴露及忘記密碼,我們還可以設(shè)置 login-path 來實(shí)現(xiàn)在本地不輸密碼快捷登錄。
login-path 是 MySQL 5.6 開始支持的新特性。通過借助 mysql_config_editor 工具將登陸 MySQL 服務(wù)的認(rèn)證信息加密保存在 .mylogin.cnf 文件(默認(rèn)位于用戶主目錄)。MySQL 客戶端工具可通過讀取該加密文件連接 MySQL ,實(shí)現(xiàn)快捷登錄。
假設(shè)我們想配置 root 賬號(hào)在本地快捷登錄,可以這么做:
#執(zhí)行回車后需要輸入一次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,2018,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>
總結(jié):
本篇文章主要介紹了修改數(shù)據(jù)庫賬號(hào)密碼的幾種方法,基本涵蓋了所有的場景。這里也提醒下各位,數(shù)據(jù)庫賬號(hào)最好限制ip段登錄,密碼盡量復(fù)雜些,最好能夠定期修改,特別是重要的環(huán)境不能有半點(diǎn)馬虎。年底了,安全才是王道。
以上就是MySQL修改密碼的幾種方式的詳細(xì)內(nèi)容,更多關(guān)于MySQL修改密碼的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。