一、密码修改的几种方法 mysql
- a、能够在建立用户的时候指定密码,以及直接使用grant建立用户的时候指定密码。
- 对于已经存在的用户直接使用grant方式也能够修改密码
- 以下:
-
- --演示版本
- root@localhost[(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,并设定密码
- root@localhost[(none)]> grant usage on *.* to 'fred'@'localhost' identified by 'fred';
- Query OK, 0 rows affected (0.00 sec)
-
- --查看刚刚建立的帐户
- root@localhost[(none)]> select host,user,password from mysql.user where user='fred';
- +-----------+------+-------------------------------------------+
- | host | user | password |
- +-----------+------+-------------------------------------------+
- | localhost | fred | *6C69D17939B2C1D04E17A96F9B29B284832979B7 |
- +-----------+------+-------------------------------------------+
-
- --下面能够成功登录mysql
- SZDB:~ # mysql -ufred -pfred
-
- fred@localhost[(none)]>
-
- b、使用set password方式来修改帐户密码
- --下面咱们使用set password方式来设定密码
- root@localhost[(none)]> set password for 'fred'@'localhost'=password('passwd');
- Query OK, 0 rows affected (0.00 sec)
-
- root@localhost[(none)]> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
-
- --再次登录时,以前的密码已经失效,没法登录
- SZDB:~ # mysql -ufred -pfred
- ERROR 1045 (28000): Access denied for user 'fred'@'localhost' (using password: YES)
-
- --下面使用新密码登录成功
- SZDB:~ # mysql -ufred -ppasswd
-
- fred@localhost[(none)]>
-
- --检索数据库是否存在jack用户,以下密码为null
- root@localhost[(none)]> select host,user,password from mysql.user where user='jack';
- +-----------+------+----------+
- | host | user | password |
- +-----------+------+----------+
- | localhost | jack | |
- +-----------+------+----------+
-
- c、加密方式更新系统表user的password列
- --咱们尝试直接更新密码列(不使用加密函数方式)
- root@localhost[(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显示为明文
- root@localhost[(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
-
- root@localhost[(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用户
- root@localhost[(none)]> set password for 'jack'@'localhost'=password('jack');
- ERROR 1133 (42000): Can't find any matching row in the user table
-
- --咱们切换到mysql数据库下尝试,
- root@localhost[(none)]> use mysql
-
- root@localhost[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列
- root@localhost[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
-
- root@localhost[mysql]> select host,user,password from user where user='jack'; --能够看到密码已经变成了密文
- +-----------+------+-------------------------------------------+
- | host | user | password |
- +-----------+------+-------------------------------------------+
- | localhost | jack | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
- +-----------+------+-------------------------------------------+
-
- root@localhost[mysql]> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
-
- --此时登录成功
- robin@SZDB:~> mysql -ujack -ppasswd
-
- jack@localhost[(none)]>
二、重置root账户密码 sql
- --假定此时咱们的root账户忘记或遗失了密码,以下面的演示,咱们给出的是xxx,不能登录到mysql(真实的密码为mysql)
- SZDB:~ # mysql -uroot -pmysql
-
- root@localhost[(none)]>
-
- SZDB:~ # mysql -uroot -pxxx #忘记密码,此时没法正常登陆
- Enter password:
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
-
- --首先中止mysql服务器
- SZDB:~ # service mysql stop
- Shutting 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] 10209
- SZDB:~ # ps -ef | grep mysql
- mysql 10209 14240 4 13:52 pts/0 00:00:00 mysqld --skip-grant-tables --user=mysql
- root 10229 14240 0 13:53 pts/0 00:00:00 grep mysql
- SZDB:~ # mysql
-
- root@localhost[(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者等同
- root@localhost[(none)]> update mysql.user set password='' where user='root';
- Query OK, 2 rows affected (0.00 sec)
- Rows matched: 2 Changed: 2 Warnings: 0
-
- root@localhost[(none)]> select user,host,password from mysql.user where user='root';
- +------+-----------+----------+
- | user | host | password |
- +------+-----------+----------+
- | root | % | |
- | root | 127.0.0.1 | |
- +------+-----------+----------+
-
- root@localhost[(none)]> exit
- Bye
-
- #再次中止mysql数据库服务器
- SZDB:~ # service mysql stop
- Shutting down MySQL. done
- [1]+ Done mysqld --skip-grant-tables --user=mysql
- SZDB:~ # service mysql start
- Starting MySQL.. done
- SZDB:~ # mysql #重启后再次登录,再也不须要任何密码
-
- 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启动服务器后进行重置。 数据库