2、重置root帐户密码
--假定此时我们的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)]>
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 用户与权限管理 见 http://www.linuxidc.com/Linux/2014-10/107663.htm
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/107662.htm
