MySQL 修改用户密码及重置root密码

    为数据库用户修改密码是DBA比较常见的工做之一。对于MySQL用户帐户的密码修改,有几种不一样的方式,推荐的方式使用加密函数来修改密码。本文主要描述了经过几种不一样的方式来修改用户密码以及mysql root帐户密码丢失(重置root密码)的处理方法。mysql

 

一、密码修改的几种方法sql

[sql] view plaincopyprint?在CODE上查看代码片派生到个人代码片数据库

  1. a、能够在建立用户的时候指定密码,以及直接使用grant建立用户的时候指定密码。  服务器

  2.    对于已经存在的用户直接使用grant方式也能够修改密码  ide

  3. 以下:  函数

  4.   

  5. --演示版本  加密

  6. root@localhost[(none)]> show variables like 'version%';    spa

  7. +-------------------------+------------------------------+     .net

  8. | Variable_name           | Value                        |    code

  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] view plaincopyprint?在CODE上查看代码片派生到个人代码片

  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启动服务器后进行重置。
e、有关mysql权限及用户管理,建立用户时指定密码,请参考:MySQL 用户与权限管理

相关文章
相关标签/搜索