為數(shù)據(jù)庫(kù)用戶修改密碼是DBA比較常見的工作之一。對(duì)于MySQL用戶賬戶的密碼修改,有幾種不同的方式,推薦的方式使用加密函數(shù)來(lái)修改密碼。本文主要描述了通過(guò)幾種不同的方式來(lái)修改用戶密碼以及mysql root賬戶密碼丟失(重置root密碼)的處理方法。
1、密碼修改的幾種方法
a、可以在創(chuàng)建用戶的時(shí)候指定密碼,以及直接使用grant創(chuàng)建用戶的時(shí)候指定密碼。
對(duì)于已經(jīng)存在的用戶直接使用grant方式也可以修改密碼
如下:
--演示版本
root@localhost[(none)]>showvariableslike'version%';
+-------------------------+------------------------------+
|Variable_name|Value|
+-------------------------+------------------------------+
|version|5.5.37|
|version_comment|MySQLCommunityServer(GPL)|
|version_compile_machine|x86_64|
|version_compile_os|Linux|
+-------------------------+------------------------------+
--下面我們使用grant方式創(chuàng)建一個(gè)新帳戶fred,并設(shè)定密碼
root@localhost[(none)]>grantusageon*.*to'fred'@'localhost'identifiedby'fred';
QueryOK,0rowsaffected(0.00sec)
--查看剛剛創(chuàng)建的賬戶
root@localhost[(none)]>selecthost,user,passwordfrommysql.userwhereuser='fred';
+-----------+------+-------------------------------------------+
|host|user|password|
+-----------+------+-------------------------------------------+
|localhost|fred|*6C69D17939B2C1D04E17A96F9B29B284832979B7|
+-----------+------+-------------------------------------------+
--下面可以成功登陸mysql
SZDB:~#mysql-ufred-pfred
fred@localhost[(none)]>
b、使用setpassword方式來(lái)修改賬戶密碼
--下面我們使用setpassword方式來(lái)設(shè)定密碼
root@localhost[(none)]>setpasswordfor'fred'@'localhost'=password('passwd');
QueryOK,0rowsaffected(0.00sec)
root@localhost[(none)]>flushprivileges;
QueryOK,0rowsaffected(0.00sec)
--再次登陸時(shí),之前的密碼已經(jīng)失效,無(wú)法登陸
SZDB:~#mysql-ufred-pfred
ERROR1045(28000):Accessdeniedforuser'fred'@'localhost'(usingpassword:YES)
--下面使用新密碼登陸成功
SZDB:~#mysql-ufred-ppasswd
fred@localhost[(none)]>
--檢索數(shù)據(jù)庫(kù)是否存在jack用戶,如下密碼為null
root@localhost[(none)]>selecthost,user,passwordfrommysql.userwhereuser='jack';
+-----------+------+----------+
|host|user|password|
+-----------+------+----------+
|localhost|jack||
+-----------+------+----------+
c、加密方式更新系統(tǒng)表user的password列
--我們嘗試直接更新密碼列(不使用加密函數(shù)方式)
root@localhost[(none)]>updatemysql.usersetpassword='jack'whereuser='jack';
QueryOK,1rowaffected(0.00sec)
Rowsmatched:1Changed:1Warnings:0
--由于直接使用明文,因此系統(tǒng)表user列password顯示為明文
root@localhost[(none)]>selecthost,user,passwordfrommysql.userwhereuser='jack';
+-----------+------+----------+
|host|user|password|
+-----------+------+----------+
|localhost|jack|jack|
+-----------+------+----------+
--Author:Leshami
--Blog:http://blog.csdn.net/leshami
root@localhost[(none)]>flushprivileges;
QueryOK,0rowsaffected(0.02sec)
--此時(shí)無(wú)法登陸
SZDB:~#mysql-ujack-pjack-hlocalhost
ERROR1045(28000):Accessdeniedforuser'jack'@'localhost'(usingpassword:YES)
--下面我們通過(guò)set方式來(lái)修改jack的密碼,提示找不到j(luò)ack用戶
root@localhost[(none)]>setpasswordfor'jack'@'localhost'=password('jack');
ERROR1133(42000):Can'tfindanymatchingrowintheusertable
--我們切換到mysql數(shù)據(jù)庫(kù)下嘗試,
root@localhost[(none)]>usemysql
root@localhost[mysql]>setpasswordfor'jack'@'localhost'=password('passwd');--在mysql數(shù)據(jù)庫(kù)下依舊無(wú)法更新用戶jack的密碼
ERROR1133(42000):Can'tfindanymatchingrowintheusertable
--下面我們嘗試用password函數(shù)方式來(lái)更新password列
root@localhost[mysql]>updateusersetpassword=password('passwd')whereuser='jack';--此方式更新成功
QueryOK,1rowaffected(0.04sec)
Rowsmatched:1Changed:1Warnings:0
root@localhost[mysql]>selecthost,user,passwordfromuserwhereuser='jack';--可以看到密碼已經(jīng)變成了密文
+-----------+------+-------------------------------------------+
|host|user|password|
+-----------+------+-------------------------------------------+
|localhost|jack|*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0|
+-----------+------+-------------------------------------------+
root@localhost[mysql]>flushprivileges;
QueryOK,0rowsaffected(0.00sec)
--此時(shí)登陸成功
robin@SZDB:~>mysql-ujack-ppasswd
jack@localhost[(none)]>
2、重置root帳戶密碼
[sql]
view plaincopyprint?
--假定此時(shí)我們的root帳戶忘記或遺失了密碼,如下面的演示,我們給出的是xxx,不能登陸到mysql(真實(shí)的密碼為mysql)
SZDB:~#mysql-uroot-pmysql
root@localhost[(none)]>
SZDB:~#mysql-uroot-pxxx#忘記密碼,此時(shí)無(wú)法正常登錄
Enterpassword:
ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)
--首先停止mysql服務(wù)器
SZDB:~#servicemysqlstop
ShuttingdownMySQL..done
--使用--skip-grant-tables選項(xiàng)跳過(guò)授權(quán)表驗(yàn)證,
SZDB:~#mysqld--help--verbose#獲取mysqld幫助信息
--skip-grant-tablesStartwithoutgranttables.ThisgivesallusersFULL
ACCESStoalltables.
--使用--skip-grant-tables啟動(dòng)mysql服務(wù)器
SZDB:~#mysqld--skip-grant-tables--user=mysql&
[1]10209
SZDB:~#ps-ef|grepmysql
mysql1020914240413:52pts/000:00:00mysqld--skip-grant-tables--user=mysql
root1022914240013:53pts/000:00:00grepmysql
SZDB:~#mysql
root@localhost[(none)]>selectuser,host,passwordfrommysql.userwhereuser='root';
+-------+-----------+-------------------------------------------+
|user|host|password|
+-------+-----------+-------------------------------------------+
|root|%|*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA|
|root|127.0.0.1|*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA|
+-------+-----------+-------------------------------------------+
--更新mysql賬戶密碼為NULL或設(shè)定為新密碼,注設(shè)定為空密碼時(shí)可以直接設(shè)置,無(wú)須使用加密函數(shù),2者等同
root@localhost[(none)]>updatemysql.usersetpassword=''whereuser='root';
QueryOK,2rowsaffected(0.00sec)
Rowsmatched:2Changed:2Warnings:0
root@localhost[(none)]>selectuser,host,passwordfrommysql.userwhereuser='root';
+------+-----------+----------+
|user|host|password|
+------+-----------+----------+
|root|%||
|root|127.0.0.1||
+------+-----------+----------+
root@localhost[(none)]>exit
Bye
#再次停止mysql數(shù)據(jù)庫(kù)服務(wù)器
SZDB:~#servicemysqlstop
ShuttingdownMySQL.done
[1]+Donemysqld--skip-grant-tables--user=mysql
SZDB:~#servicemysqlstart
StartingMySQL..done
SZDB:~#mysql#重啟后再次登陸,不再需要任何密碼
root@localhost[(none)]>
3、小結(jié)
a、可以使用set password for'user_name'@'host_name'password=password('new_pwd')方式來(lái)修改密碼
b、可以使用update系統(tǒng)表方式,update user set password=password('passwd') where user='user_name'
注: 對(duì)于user表password類,如果不用password函數(shù)的話,導(dǎo)致更新后無(wú)法登陸。
但如果將賬戶更新為空密碼,可以使用加密函數(shù),也可以不使用,2者等同。
c、也可以在用戶創(chuàng)建后直接使用grant方式來(lái)更新用戶密碼。
d、對(duì)應(yīng)root密碼丟失或需要重置root密碼的情形,需要使用系統(tǒng)選項(xiàng)--skip-grant-tables啟動(dòng)服務(wù)器后進(jìn)行重置。
e、有關(guān)mysql權(quán)限及用戶管理,創(chuàng)建用戶時(shí)指定密碼,請(qǐng)參考:MySQL 用戶與權(quán)限管理
新聞熱點(diǎn)
疑難解答
圖片精選