MySQL 修改用戶密碼及重置root密碼

一、密碼修改的幾種方法 mysql

  1. a、能夠在建立用戶的時候指定密碼,以及直接使用grant建立用戶的時候指定密碼。  
  2.    對於已經存在的用戶直接使用grant方式也能夠修改密碼  
  3. 以下:  
  4.   
  5. --演示版本  
  6. root@localhost[(none)]> show variables like 'version%';    
  7. +-------------------------+------------------------------+     
  8. | Variable_name           | Value                        |    
  9. +-------------------------+------------------------------+     
  10. | version                 | 5.5.37                       |    
  11. | version_comment         | MySQL Community Server (GPL) |    
  12. | version_compile_machine | x86_64                       |    
  13. | version_compile_os      | Linux                        |    
  14. +-------------------------+------------------------------+     
  15.   
  16. --下面咱們使用grant方式建立一個新賬戶fred,並設定密碼  
  17. root@localhost[(none)]> grant usage on *.* to 'fred'@'localhost' identified by 'fred';  
  18. Query OK, 0 rows affected (0.00 sec)  
  19.   
  20. --查看剛剛建立的帳戶  
  21. root@localhost[(none)]> select host,user,password from mysql.user where user='fred';  
  22. +-----------+------+-------------------------------------------+  
  23. | host      | user | password                                  |  
  24. +-----------+------+-------------------------------------------+  
  25. | localhost | fred | *6C69D17939B2C1D04E17A96F9B29B284832979B7 |  
  26. +-----------+------+-------------------------------------------+  
  27.   
  28. --下面能夠成功登錄mysql  
  29. SZDB:~ # mysql -ufred -pfred  
  30.   
  31. fred@localhost[(none)]>   
  32.   
  33. b、使用set password方式來修改帳戶密碼  
  34. --下面咱們使用set password方式來設定密碼  
  35. root@localhost[(none)]> set password for 'fred'@'localhost'=password('passwd');  
  36. Query OK, 0 rows affected (0.00 sec)  
  37.   
  38. root@localhost[(none)]> flush privileges;  
  39. Query OK, 0 rows affected (0.00 sec)  
  40.   
  41. --再次登錄時,以前的密碼已經失效,沒法登錄  
  42. SZDB:~ # mysql -ufred -pfred  
  43. ERROR 1045 (28000): Access denied for user 'fred'@'localhost' (using password: YES)  
  44.   
  45. --下面使用新密碼登錄成功  
  46. SZDB:~ # mysql -ufred -ppasswd  
  47.   
  48. fred@localhost[(none)]>   
  49.   
  50. --檢索數據庫是否存在jack用戶,以下密碼爲null  
  51. root@localhost[(none)]> select host,user,password from mysql.user where user='jack';  
  52. +-----------+------+----------+  
  53. | host      | user | password |  
  54. +-----------+------+----------+  
  55. | localhost | jack |          |  
  56. +-----------+------+----------+  
  57.   
  58. c、加密方式更新系統表userpassword列  
  59. --咱們嘗試直接更新密碼列(不使用加密函數方式)  
  60. root@localhost[(none)]> update mysql.user set password='jack' where user='jack';  
  61. Query OK, 1 row affected (0.00 sec)  
  62. Rows matched: 1  Changed: 1  Warnings: 0  
  63.   
  64. --因爲直接使用明文,所以系統表user列password顯示爲明文  
  65. root@localhost[(none)]> select host,user,password from mysql.user where user='jack';  
  66. +-----------+------+----------+  
  67. | host      | user | password |  
  68. +-----------+------+----------+  
  69. | localhost | jack | jack     |  
  70. +-----------+------+----------+  
  71.   
  72. --Author : Leshami  
  73. --Blog   :http://blog.csdn.net/leshami  
  74.   
  75. root@localhost[(none)]> flush privileges;  
  76. Query OK, 0 rows affected (0.02 sec)  
  77.   
  78. --此時沒法登錄  
  79. SZDB:~ # mysql -ujack -pjack -h localhost        
  80. ERROR 1045 (28000): Access denied for user 'jack'@'localhost' (using password: YES)  
  81.   
  82. --下面咱們經過set方式來修改jack的密碼,提示找不到jack用戶  
  83. root@localhost[(none)]> set password for 'jack'@'localhost'=password('jack');  
  84. ERROR 1133 (42000): Can't find any matching row in the user table  
  85.   
  86. --咱們切換到mysql數據庫下嘗試,  
  87. root@localhost[(none)]> use mysql     
  88.   
  89. root@localhost[mysql]> set password for 'jack'@'localhost'=password('passwd');  --在mysql數據庫下依舊沒法更新用戶jack的密碼  
  90. ERROR 1133 (42000): Can't find any matching row in the user table  
  91.   
  92. --下面咱們嘗試用password函數方式來更新password列  
  93. root@localhost[mysql]> update user set password=password('passwd'where user='jack'--此方式更新成功  
  94. Query OK, 1 row affected (0.04 sec)  
  95. Rows matched: 1  Changed: 1  Warnings: 0  
  96.   
  97. root@localhost[mysql]> select host,user,password from user where user='jack';    --能夠看到密碼已經變成了密文  
  98. +-----------+------+-------------------------------------------+  
  99. | host      | user | password                                  |  
  100. +-----------+------+-------------------------------------------+  
  101. | localhost | jack | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |  
  102. +-----------+------+-------------------------------------------+  
  103.   
  104. root@localhost[mysql]> flush privileges;  
  105. Query OK, 0 rows affected (0.00 sec)  
  106.   
  107. --此時登錄成功  
  108. robin@SZDB:~> mysql -ujack -ppasswd            
  109.   
  110. jack@localhost[(none)]>   

二、重置root賬戶密碼 sql

  1. --假定此時咱們的root賬戶忘記或遺失了密碼,以下面的演示,咱們給出的是xxx,不能登錄到mysql(真實的密碼爲mysql)  
  2. SZDB:~ # mysql -uroot -pmysql  
  3.   
  4. root@localhost[(none)]>   
  5.   
  6. SZDB:~ # mysql -uroot -pxxx       #忘記密碼,此時沒法正常登陸    
  7. Enter password:   
  8. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwordNO)  
  9.   
  10. --首先中止mysql服務器  
  11. SZDB:~ # service mysql stop  
  12. Shutting down MySQL..                               done  
  13.   
  14. --使用--skip-grant-tables選項跳過受權表驗證,  
  15. SZDB:~ # mysqld --help --verbose     #獲取mysqld幫助信息  
  16.   
  17. --skip-grant-tables Start without grant tables. This gives all users FULL  
  18.                       ACCESS to all tables.  
  19.   
  20. --使用--skip-grant-tables啓動mysql服務器  
  21. SZDB:~ # mysqld --skip-grant-tables --user=mysql &  
  22. [1] 10209  
  23. SZDB:~ # ps -ef | grep mysql  
  24. mysql    10209 14240  4 13:52 pts/0    00:00:00 mysqld --skip-grant-tables --user=mysql  
  25. root     10229 14240  0 13:53 pts/0    00:00:00 grep mysql  
  26. SZDB:~ # mysql     
  27.   
  28. root@localhost[(none)]> select user,host,password from mysql.user where user='root';  
  29. +-------+-----------+-------------------------------------------+  
  30. user  | host      | password                                  |  
  31. +-------+-----------+-------------------------------------------+  
  32. | root  | %         | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |  
  33. | root  | 127.0.0.1 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |  
  34. +-------+-----------+-------------------------------------------+  
  35.   
  36. --更新mysql帳戶密碼爲NULL或設定爲新密碼,注設定爲空密碼時能夠直接設置,無須使用加密函數,2者等同  
  37. root@localhost[(none)]> update mysql.user set password='' where user='root';  
  38. Query OK, 2 rows affected (0.00 sec)  
  39. Rows matched: 2  Changed: 2  Warnings: 0  
  40.   
  41. root@localhost[(none)]> select user,host,password from mysql.user where user='root';  
  42. +------+-----------+----------+  
  43. user | host      | password |  
  44. +------+-----------+----------+  
  45. | root | %         |          |  
  46. | root | 127.0.0.1 |          |  
  47. +------+-----------+----------+  
  48.   
  49. root@localhost[(none)]> exit  
  50. Bye  
  51.   
  52. #再次中止mysql數據庫服務器  
  53. SZDB:~ # service mysql stop  
  54. Shutting down MySQL.                                                  done  
  55. [1]+  Done                    mysqld --skip-grant-tables --user=mysql  
  56. SZDB:~ # service mysql start  
  57. Starting MySQL..                                                      done  
  58. SZDB:~ # mysql            #重啓後再次登錄,再也不須要任何密碼  
  59.   
  60. root@localhost[(none)]>     

三、小結
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啓動服務器後進行重置。 
數據庫

相關文章
相關標籤/搜索