前言:在日常使用数据库的过程中,难免会遇到需要修改账号密码的情况,比如密码太简单,需要修改被修改、密码过期需要修改、忘记密码需要修改等。本文将介绍需要修改密码的场景和修改密码的几种方式。1.忘记root密码忘记root密码是比较常见的,尤其是我搭建的测试环境已经很久没有使用了,很容易忘记当时设置的密码。这时候常用的方法是跳过权限验证,然后修改root密码,再开启权限验证。以MySQL5.7版本为例,简单描述一下主要过程:首先修改配置文件,在[mysqld]部分添加一句:skip-grant-tables,添加这个参数的目的是为了跳过权限验证。然后重启数据库,等数据库再次启动后,我们就可以不用密码直接登录数据库修改密码了。#skip-grant-tables模式修改root密码[root@host~]#mysqlWelcometotheMySQLmonitor.Commandsendwith;or\g.YourMySQLconnectionidis16Serverversion:5.7.23-logMySQLCommunityServer(GPL)Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved.Oracle是OracleCorporation和/或其附属公司的注册商标。其他名称可能是其各自所有者的商标。键入“help;”或“\h”以获得帮助。键入“\c”以清除当前输入语句。mysql>updatemysql.usersetauthentication_string=password('xxxxxx')whereuser='root'andhost='localhost,';Query0rowsaffected,1warning(0.00sec)Rowsmatched:1Changed:0Warnings:1mysql>flushprivileges;QueryOK,0rowsaffected(0.01sec)修改root密码后,再次去掉skip-grant-tables参数,然后重启数据库。2、修改密码的几种方法除了忘记密码外,还可能有其他情况需要修改密码。此时可以通过正常方式修改密码。仍然以MySQL5.7为例,介绍几种常用的修改密码的方法。使用alteruser进行修改例如要修改testuser账号的密码,我们可以使用root账号登录,然后执行alteruser命令修改testuser账号的密码。mysql>alteruser'testuser'@'%'identifiedby'Password1';QueryOK,0rowsaffected(0.01sec)mysql>flushprivileges;QueryOK,0rowsaffected(0.00sec)使用SETPASSWORD命令使用SETPASSWORD修改密码命令格式为SET'用户名'@'host'的密码=PASSWORD('newpass');也可以使用root账户修改其他账户的密码。mysql>SETPASSWORDFOR'testuser'@'%'=PASSWORD('Password2');QueryOK,0rowsaffected,1warning(0.00sec)mysql>flushprivileges;QueryOK,0rowsaffected(0.00sec)使用mysqladmin修改密码使用mysqladmin命令修改账号密码格式为mysqladmin-uusername-poldpasswordpasswordnewpassword[root@host~]#mysqladmin-utestuser-pPassword2passwordPassword3mysqladmin:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.Warning:Sincepasswordwillbesenttoserverinplaintext,usesslconnectiontoensurepasswordsafety.[root@host~usermysql-utestword3password3Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.WelcometotheMySQLmonitor.Commandsendwith;or\g.YourMySQLconnectionidis2388Serverversion:5.7.23-logMySQLCommunityServer(GPL)Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved.OracleisaregisteredtrademarkofOracleCorporationand/oritsaffiliates.Othernamesmaybetrademarksoftheirrespectiveowners.Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.mysql>实际上直接更新用户表mysql.user表中保存了所有的MySQL账户信息,我们也可以直接通过updateuser表修改密码#5.7及以后版本mysql>updatemysql.usersetauthentication_string=password('Password4')whereuser='testuser'andhost='%';QueryOK,1rowaffected,1warning(0.06sec)Rowsmatched:1Changed:1Warnings:1mysql>flushprivileges;QueryOK,0rowsaffected(0.01sec)#5.6及更早版本updatemysql.usersetpassword=password('newpassword')whereuser='username'和主机='主机';3、设置login-path实现本地快速登录为了防止密码泄露和忘记密码,我们还可以设置login-path实现本地快速登录,无需输入密码。login-path是MySQL5.6支持的新特性。使用mysql_config_editor工具将登录MySQL服务的认证信息加密保存在.mylogin.cnf文件中(默认位于用户家目录下)。MySQL客户端工具可以通过读取加密文件连接MySQL,实现快速登录。假设我们要配置root账号在本地快速登录,我们可以这样做:#执行回车后,需要输入一次root密码[root@host~]#mysql_config_editorset--login-path=root-uroot-hlocalhost-p-P3306Enterpassword:#配置完成后可以使用login-path登录[root@host~]#mysql--login-path=rootWelcometotheMySQLmonitor.Commandsendwith;or\g.YourMySQLconnectionidis2919Serverversion:5.7.23-logMySQLCommunityServer(GPL)Copyright(c)2000,2018,Oracleand/oritsaffiliates.Allrightsreserved.OracleisregisteredtrademarkofOracleCorporationand/oritsaffiliates.Othernamesmaybetrademarkoftheirrespectiveowners.Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.mysql>总结:本文主要介绍了几种修改数据库账户密码的方法,基本上涵盖了所有场景。在这里提醒大家,数据库账号的登录最好限制在ip段,密码越复杂越好,而且最好定期更换,尤其是在重要的环境下。年底了,安全为王。
