MySQL 改動使用者password及重設rootpassword

來源:互聯網
上載者:User

標籤:

    為資料庫使用者改動password是DBA比較常見的工作之中的一個。對於MySQL使用者賬戶的password改動,有幾種不同的方式。推薦的方式使用加密函數來改動password。

本文主要描寫敘述了通過幾種不同的方式來改動使用者password以及mysql root賬戶password丟失(重設rootpassword)的處理方法。

 

1、password改動的幾種方法

a、能夠在建立使用者的時候指定password。以及直接使用grant建立使用者的時候指定password。   對於已經存在的使用者直接使用grant方式也能夠改動password例如以下:--示範版本號碼[email protected][(none)]> show variables like ‘version%‘;  +-------------------------+------------------------------+   | Variable_name           | Value                        |  +-------------------------+------------------------------+   | version                 | 5.5.37                       |  | version_comment         | MySQL Community Server (GPL) |  | version_compile_machine | x86_64                       |  | version_compile_os      | Linux                        |  +-------------------------+------------------------------+   --以下我們使用grant方式建立一個新帳戶fred,並設定password[email protected][(none)]> grant usage on *.* to ‘fred‘@‘localhost‘ identified by ‘fred‘;Query OK, 0 rows affected (0.00 sec)--查看剛剛建立的賬戶[email protected][(none)]> select host,user,password from mysql.user where user=‘fred‘;+-----------+------+-------------------------------------------+| host      | user | password                                  |+-----------+------+-------------------------------------------+| localhost | fred | *6C69D17939B2C1D04E17A96F9B29B284832979B7 |+-----------+------+-------------------------------------------+--以下能夠成功登陸mysqlSZDB:~ # mysql -ufred -pfred[email protected][(none)]> b、使用set password方式來改動賬戶密碼--以下我們使用set password方式來設定密碼[email protected][(none)]> set password for ‘fred‘@‘localhost‘=password(‘passwd‘);Query OK, 0 rows affected (0.00 sec)[email protected][(none)]> flush privileges;Query OK, 0 rows affected (0.00 sec)--再次登陸時。之前的password已經失效。無法登陸SZDB:~ # mysql -ufred -pfredERROR 1045 (28000): Access denied for user ‘fred‘@‘localhost‘ (using password: YES)--以下使用新password登陸成功SZDB:~ # mysql -ufred -ppasswd[email protected][(none)]> --檢索資料庫是否存在jack使用者。例如以下password為null[email protected][(none)]> select host,user,password from mysql.user where user=‘jack‘;+-----------+------+----------+| host      | user | password |+-----------+------+----------+| localhost | jack |          |+-----------+------+----------+c、加密方式更新系統資料表user的password列--我們嘗試直接更新password列(不使用加密函數方式)[email protected][(none)]> update mysql.user set password=‘jack‘ where user=‘jack‘;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0--因為直接使用明文。因此系統資料表user列password顯示為明文[email protected][(none)]> select host,user,password from mysql.user where user=‘jack‘;+-----------+------+----------+| host      | user | password |+-----------+------+----------+| localhost | jack | jack     |+-----------+------+----------+--Author : Leshami--Blog   :http://blog.csdn.net/leshami[email protected][(none)]> flush privileges;Query OK, 0 rows affected (0.02 sec)--此時無法登陸SZDB:~ # mysql -ujack -pjack -h localhost      ERROR 1045 (28000): Access denied for user ‘jack‘@‘localhost‘ (using password: YES)--以下我們通過set方式來改動jack的password,提示找不到jack使用者[email protected][(none)]> set password for ‘jack‘@‘localhost‘=password(‘jack‘);ERROR 1133 (42000): Can‘t find any matching row in the user table--我們切換到mysql資料庫下嘗試,[email protected][(none)]> use mysql   [email protected][mysql]> set password for ‘jack‘@‘localhost‘=password(‘passwd‘);  --在mysql資料庫下依然無法更新使用者jack的密碼ERROR 1133 (42000): Can‘t find any matching row in the user table--以下我們嘗試用password函數方式來更新password列[email protected][mysql]> update user set password=password(‘passwd‘) where user=‘jack‘; --此方式更新成功Query OK, 1 row affected (0.04 sec)Rows matched: 1  Changed: 1  Warnings: 0[email protected][mysql]> select host,user,password from user where user=‘jack‘;    --能夠看到密碼已經變成了密文+-----------+------+-------------------------------------------+| host      | user | password                                  |+-----------+------+-------------------------------------------+| localhost | jack | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |+-----------+------+-------------------------------------------+[email protected][mysql]> flush privileges;Query OK, 0 rows affected (0.00 sec)--此時登陸成功[email protected]:~> mysql -ujack -ppasswd          [email protected][(none)]> 

2、重設root帳戶password

--假定此時我們的root帳戶忘記或遺失了password,如以下的示範,我們給出的是xxx,不能登陸到mysql(真實的password為mysql)SZDB:~ # mysql -uroot -pmysql[email protected][(none)]> SZDB:~ # mysql -uroot -pxxx       #忘記password,此時無法正常登入  Enter password: ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)--首先停止mysqlserverSZDB:~ # service mysql stopShutting down MySQL..                               done--使用--skip-grant-tables選項跳過授權表驗證,SZDB:~ # mysqld --help --verbose     #擷取mysqld協助資訊--skip-grant-tables Start without grant tables. This gives all users FULL                      ACCESS to all tables.--使用--skip-grant-tables啟動mysqlserverSZDB:~ # mysqld --skip-grant-tables --user=mysql &[1] 10209SZDB:~ # ps -ef | grep mysqlmysql    10209 14240  4 13:52 pts/0    00:00:00 mysqld --skip-grant-tables --user=mysqlroot     10229 14240  0 13:53 pts/0    00:00:00 grep mysqlSZDB:~ # mysql   [email protected][(none)]> select user,host,password from mysql.user where user=‘root‘;+-------+-----------+-------------------------------------------+| user  | host      | password                                  |+-------+-----------+-------------------------------------------+| root  | %         | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA || root  | 127.0.0.1 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |+-------+-----------+-------------------------------------------+--更新mysql賬戶password為NULL或設定為新password,注設定為空白password時能夠直接設定。無須使用加密函數,2者等同[email protected][(none)]> update mysql.user set password=‘‘ where user=‘root‘;Query OK, 2 rows affected (0.00 sec)Rows matched: 2  Changed: 2  Warnings: 0[email protected][(none)]> select user,host,password from mysql.user where user=‘root‘;+------+-----------+----------+| user | host      | password |+------+-----------+----------+| root | %         |          || root | 127.0.0.1 |          |+------+-----------+----------+[email protected][(none)]> exitBye#再次停止mysql資料庫serverSZDB:~ # service mysql stopShutting down MySQL.                                                  done[1]+  Done                    mysqld --skip-grant-tables --user=mysqlSZDB:~ # service mysql startStarting MySQL..                                                      doneSZDB:~ # mysql            #重新啟動後再次登陸。不再須要不論什麼password[email protected][(none)]>   

3、小結
a、能夠使用set password for
‘user_name‘@‘host_name‘=password(‘new_pwd‘)方式來改動密碼  #更正@20141031
b、能夠使用update系統資料表方式,update user set password=password(‘passwd‘) where user=‘user_name‘
       註: 對於user表password類,假設不用password函數的話。導致更新後無法登陸。
            但假設將賬戶更新為空白密碼,能夠使用加密函數。也能夠不使用,2者等同。
c、也能夠在使用者建立後直接使用grant方式來更新使用者密碼。


d、相應root密碼丟失或須要重設root密碼的情形,須要使用系統選項--skip-grant-tables啟動server後進行重設。
e、有關mysql許可權及使用者管理。建立使用者時指定密碼,請參考:MySQL 使用者與許可權管理

 

idkey=e61fdd22421b38cb5eef12db3c564ac0b5840f0f5cae4958e8b646740a79e680" target="_blank">

MySQL 改動使用者password及重設rootpassword

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.