为数据库用户修改密码是DBA比较常见的工作之一。对于MySQL用户账户的密码修改,有几种不同的方式,推荐的方式使用加密函数来修改密码。本文主要描述了通过几种不同的方式来修改用户密码以及mysql root账户密码丢失(重置root密码)的处理方法。
1、密码修改的几种方法
<span style="font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";">a、可以在创建用户的时候指定密码,以及直接使用grant创建用户的时候指定密码。<br/> 对于已经存在的用户直接使用grant方式也可以修改密码<br/>如下:<br/><br/>--演示版本<br/>root@localhost[(none)]> show variables like 'version%'; <br/>+-------------------------+------------------------------+ <br/>| Variable_name | Value | <br/>+-------------------------+------------------------------+ <br/>| version | 5.5.37 | <br/>| version_comment | MySQL Community Server (GPL) | <br/>| version_compile_machine | x86_64 | <br/>| version_compile_os | Linux | <br/>+-------------------------+------------------------------+ <br/><br/>--下面我们使用grant方式创建一个新帐户fred,并设定密码<br/>root@localhost[(none)]> grant usage on *.* to 'fred'@'localhost' identified by 'fred';<br/>Query OK, 0 rows affected (0.00 sec)<br/><br/>--查看刚刚创建的账户<br/>root@localhost[(none)]> select host,user,password from mysql.user where user='fred';<br/>+-----------+------+-------------------------------------------+<br/>| host | user | password |<br/>+-----------+------+-------------------------------------------+<br/>| localhost | fred | *6C69D17939B2C1D04E17A96F9B29B284832979B7 |<br/>+-----------+------+-------------------------------------------+<br/><br/>--下面可以成功登陆mysql<br/>SZDB:~ # mysql -ufred -pfred<br/><br/>fred@localhost[(none)]> <br/><br/>b、使用set password方式来修改账户密码<br/>--下面我们使用set password方式来设定密码<br/>root@localhost[(none)]> set password for 'fred'@'localhost'=password('passwd');<br/>Query OK, 0 rows affected (0.00 sec)<br/><br/>root@localhost[(none)]> flush privileges;<br/>Query OK, 0 rows affected (0.00 sec)<br/><br/>--再次登陆时,之前的密码已经失效,无法登陆<br/>SZDB:~ # mysql -ufred -pfred<br/>ERROR 1045 (28000): Access denied for user 'fred'@'localhost' (using password: YES)<br/><br/>--下面使用新密码登陆成功<br/>SZDB:~ # mysql -ufred -ppasswd<br/><br/>fred@localhost[(none)]> <br/><br/>--检索数据库是否存在jack用户,如下密码为null<br/>root@localhost[(none)]> select host,user,password from mysql.user where user='jack';<br/>+-----------+------+----------+<br/>| host | user | password |<br/>+-----------+------+----------+<br/>| localhost | jack | |<br/>+-----------+------+----------+<br/><br/>c、加密方式更新系统表user的password列<br/>--我们尝试直接更新密码列(不使用加密函数方式)<br/>root@localhost[(none)]> update mysql.user set password='jack' where user='jack';<br/>Query OK, 1 row affected (0.00 sec)<br/>Rows matched: 1 Changed: 1 Warnings: 0<br/><br/>--由于直接使用明文,因此系统表user列password显示为明文<br/>root@localhost[(none)]> select host,user,password from mysql.user where user='jack';<br/>+-----------+------+----------+<br/>| host | user | password |<br/>+-----------+------+----------+<br/>| localhost | jack | jack |<br/>+-----------+------+----------+<br/><br/>--Author : Leshami<br/>--Blog :http://blog.csdn.net/leshami<br/><br/>root@localhost[(none)]> flush privileges;<br/>Query OK, 0 rows affected (0.02 sec)<br/><br/>--此时无法登陆<br/>SZDB:~ # mysql -ujack -pjack -h localhost <br/>ERROR 1045 (28000): Access denied for user 'jack'@'localhost' (using password: YES)<br/><br/>--下面我们通过set方式来修改jack的密码,提示找不到jack用户<br/>root@localhost[(none)]> set password for 'jack'@'localhost'=password('jack');<br/>ERROR 1133 (42000): Can't find any matching row in the user table<br/><br/>--我们切换到mysql数据库下尝试,<br/>root@localhost[(none)]> use mysql <br/><br/>root@localhost[mysql]> set password for 'jack'@'localhost'=password('passwd'); <br/>--在mysql数据库下依旧无法更新用户jack的密码<br/>ERROR 1133 (42000): Can't find any matching row in the user table<br/><br/>--下面我们尝试用password函数方式来更新password列<br/>root@localhost[mysql]> update user set password=password('passwd') where user='jack'; --此方式更新成功<br/>Query OK, 1 row affected (0.04 sec)<br/>Rows matched: 1 Changed: 1 Warnings: 0<br/><br/>root@localhost[mysql]> select host,user,password from user where user='jack'; --可以看到密码已经变成了密文<br/>+-----------+------+-------------------------------------------+<br/>| host | user | password |<br/>+-----------+------+-------------------------------------------+<br/>| localhost | jack | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |<br/>+-----------+------+-------------------------------------------+<br/><br/>root@localhost[mysql]> flush privileges;<br/>Query OK, 0 rows affected (0.00 sec)<br/><br/>--此时登陆成功<br/>robin@SZDB:~> mysql -ujack -ppasswd <br/><br/>jack@localhost[(none)]> <br/></span>2、重置root帐户密码
<span style="font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";">--假定此时我们的root帐户忘记或遗失了密码,如下面的演示,我们给出的是xxx,不能登陆到mysql(真实的密码为mysql)<br/>SZDB:~ # mysql -uroot -pmysql<br/><br/>root@localhost[(none)]> <br/><br/>SZDB:~ # mysql -uroot -pxxx #忘记密码,此时无法正常登录 <br/>Enter password: <br/>ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)<br/><br/>--首先停止mysql服务器<br/>SZDB:~ # service mysql stop<br/>Shutting down MySQL.. done<br/><br/>--使用--skip-grant-tables选项跳过授权表验证,<br/>SZDB:~ # mysqld --help --verbose #获取mysqld帮助信息<br/><br/>--skip-grant-tables Start without grant tables. This gives all users FULL<br/> ACCESS to all tables.<br/><br/>--使用--skip-grant-tables启动mysql服务器<br/>SZDB:~ # mysqld --skip-grant-tables --user=mysql &<br/>[1] 10209<br/>SZDB:~ # ps -ef | grep mysql<br/>mysql 10209 14240 4 13:52 pts/0 00:00:00 mysqld --skip-grant-tables --user=mysql<br/>root 10229 14240 0 13:53 pts/0 00:00:00 grep mysql<br/>SZDB:~ # mysql <br/><br/>root@localhost[(none)]> select user,host,password from mysql.user where user='root';<br/>+-------+-----------+-------------------------------------------+<br/>| user | host | password |<br/>+-------+-----------+-------------------------------------------+<br/>| root | % | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |<br/>| root | 127.0.0.1 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |<br/>+-------+-----------+-------------------------------------------+<br/><br/>--更新mysql账户密码为NULL或设定为新密码,注设定为空密码时可以直接设置,无须使用加密函数,2者等同<br/>root@localhost[(none)]> update mysql.user set password='' where user='root';<br/>Query OK, 2 rows affected (0.00 sec)<br/>Rows matched: 2 Changed: 2 Warnings: 0<br/><br/>root@localhost[(none)]> select user,host,password from mysql.user where user='root';<br/>+------+-----------+----------+<br/>| user | host | password |<br/>+------+-----------+----------+<br/>| root | % | |<br/>| root | 127.0.0.1 | |<br/>+------+-----------+----------+<br/><br/>root@localhost[(none)]> exit<br/>Bye<br/><br/>#再次停止mysql数据库服务器<br/>SZDB:~ # service mysql stop<br/>Shutting down MySQL. done<br/>[1]+ Done mysqld --skip-grant-tables --user=mysql<br/>SZDB:~ # service mysql start<br/>Starting MySQL.. done<br/>SZDB:~ # mysql #重启后再次登陆,不再需要任何密码<br/><br/>root@localhost[(none)]> <br/><br/></span>
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启动服务器后进行重置。
e、有关mysql权限及用户管理,创建用户时指定密码,请参考:MySQL 用户与权限管理
【相关教程推荐】
WAP2.0企业手机网站主要特点: 系统管理:管理员管理,可以新增管理员及修改管理员密码。 产品管理:产品新增修改管理,支持UBB格式输入。 文章管理:文章新增修改管理,支持UBB格式输入。 新闻管理:新闻新增修改管理,支持UBB格式输入。 娱乐管理:新闻新增修改管理,支持UBB格式输入。 发信管理:可在线给客户发送WEB信件,注意配置信件的发送信息,如SMTP等! 访问统计:可以统计出用户访问的
0
1. mysql数据库图文教程
3. bootstrap教程
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号