MySQL 修改使用者密碼及重設root密碼

來源:互聯網
上載者:User

標籤:database   dba   mysql   密碼   加密   

    為資料庫使用者修改密碼是DBA比較常見的工作之一。對於MySQL使用者賬戶的密碼修改,有幾種不同的方式,推薦的方式使用加密函數來修改密碼。本文主要描述了通過幾種不同的方式來修改使用者密碼以及mysql root賬戶密碼丟失(重設root密碼)的處理方法。

 

1、密碼修改的幾種方法

a、可以在建立使用者的時候指定密碼,以及直接使用grant建立使用者的時候指定密碼。   對於已經存在的使用者直接使用grant方式也可以修改密碼如下:--示範版本[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,並設定密碼[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)--再次登陸時,之前的密碼已經失效,無法登陸SZDB:~ # mysql -ufred -pfredERROR 1045 (28000): Access denied for user ‘fred‘@‘localhost‘ (using password: YES)--下面使用新密碼登陸成功SZDB:~ # mysql -ufred -ppasswd[email protected][(none)]> --檢索資料庫是否存在jack使用者,如下密碼為null[email protected][(none)]> select host,user,password from mysql.user where user=‘jack‘;+-----------+------+----------+| host      | user | password |+-----------+------+----------+| localhost | jack |          |+-----------+------+----------+c、加密方式更新系統資料表user的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的密碼,提示找不到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帳戶密碼

--假定此時我們的root帳戶忘記或遺失了密碼,如下面的示範,我們給出的是xxx,不能登陸到mysql(真實的密碼為mysql)SZDB:~ # mysql -uroot -pmysql[email protected][(none)]> SZDB:~ # mysql -uroot -pxxx       #忘記密碼,此時無法正常登入  Enter password: ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)--首先停止mysql伺服器SZDB:~ # 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啟動mysql伺服器SZDB:~ # 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賬戶密碼為NULL或設定為新密碼,注設定為空白密碼時可以直接設定,無須使用加密函數,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資料庫伺服器SZDB:~ # service mysql stopShutting down MySQL.                                                  done[1]+  Done                    mysqld --skip-grant-tables --user=mysqlSZDB:~ # service mysql startStarting MySQL..                                                      doneSZDB:~ # mysql            #重啟後再次登陸,不再需要任何密碼[email protected][(none)]>   

3、小結
a、可以使用set password for ‘user_name‘@‘host_name‘ password=password(‘new_pwd‘)方式來修改密碼
b、可以使用update系統資料表方式,update user set password=password(‘passwd‘) where user=‘user_name‘
       註: 對於user表password類,如果不用password函數的話,導致更新後無法登陸。
            但如果將賬戶更新為空白密碼,可以使用加密函數,也可以不使用,2者等同。
c、也可以在使用者建立後直接使用grant方式來更新使用者密碼。
d、對應root密碼丟失或需要重設root密碼的情形,需要使用系統選項--skip-grant-tables啟動伺服器後進行重設。
e、有關mysql許可權及使用者管理,建立使用者時指定密碼,請參考:MySQL 使用者與許可權管理

 

MySQL 修改使用者密碼及重設root密碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.