密码恢复及设置

密码恢复及设置

问题

本案例要求熟悉MySQL管理密码的控制,完成以下任务操作:
1)练习重置MySQL管理密码的操作
2)通过正常途径设置MySQL数据库的管理密码

步骤

实现此案例需要按照如下步骤进行。
步骤一:重置MySQL管理密码
1)首先停止已运行的MySQL服务程序

[root@dbsvr1 ~]# service mysql stop  						//停止服务
Shutting down MySQL...  							[确定]
[root@dbsvr1 ~]# service mysql status  						//确认状态
MySQL is not running  							[失败]

2)然后跳过授权表启动MySQL服务程序
这一步主要利用mysqld的 --skip-grant-tables选项,具体操作时可选择不同方式,但本质上是一样的。以下提供三种方式,任选其中一种都可以。
方式1(推荐),执行mysql脚本起服务,末尾加 --skip-grant-tables 参数:

[root@dbsvr1 ~]# service mysql start --skip-grant-tables
Starting MySQL............  								[确定]
[root@dbsvr1 ~]# service mysql status
MySQL running (45640)  									[确定]

方式2,执行mysqld_safe进程,添加 --skip-grant-tables 参数:

[root@dbsvr1 ~]# mysqld_safe --user=mysql --skip-grant-tables &
[1] 46076
140110 09:58:39 mysqld_safe Logging to '/var/lib/mysql/dbsvr1.tarena.com.err'.
140110 09:58:39 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

方式3,修改my.cnf配置,添加 skip_grant_tables=1启动设置:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
skip_grant_tables=1
.. ..
[root@dbsvr1 ~]# service mysql start
Starting MySQL  											[确定]
[root@dbsvr1 ~]# service mysql status
MySQL running (46580) 									[确定]

为了避免冲突,上述三种方式不要同时使用。若要分别测试不同方式,同样要先停用其他方式启动的MySQL服务程序(直接service mysql stop即可)。
3)使用mysql命令连接到MySQL服务,重设root的密码
由于前一步启动的MySQL服务跳过了授权表,所以可以root从本机直接登录,不需要验证密码:

[root@dbsvr1 ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

进入 mysql> 环境后,通过修改mysql库中user表的相关记录,重设root用户从本机登录的密码:

mysql> UPDATE mysql.user SET password=PASSWORD('1234567')
    -> WHERE user='root' AND host='localhost';  			//重设root的密码
Query OK, 0 rows affected (0.17 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> FLUSH PRIVILEGES;  								//刷新授权表
Query OK, 0 rows affected (0.00 sec)
mysql> exit  											//退出mysql> 环境
Bye

通过执行“FLUSH PRIVILEGES;”可使授权表立即生效,对于正常运行的MySQL服务,也可以用上述方法来修改密码,不用重启服务。本例中因为是恢复密码,最好重启MySQL服务程序,所以上述“FLUSH PRIVILEGES;”操作可跳过。
4)重新以正常方式启动MySQL服务程序,验证新密码
如果前面是修改/etc/my.cnf配置的方法来跳过授权表,则重置root密码后,应去除相应的设置以恢复正常:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
#skip_grant_tables=1  							//注释掉或删除此行
.. ..

按正常方式,通过mysql脚本重启服务即可:

[root@dbsvr1 ~]# service mysql restart
Shutting down MySQL..                                      [确定]
Starting MySQL.                                            [确定]

验证无密码登录时,将会被拒绝:

[root@dbsvr1 ~]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

只有提供重置后的新密码,才能成功登入:

[root@dbsvr1 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

步骤二:正常设置MySQL管理密码
正常的前提是:已知当前MySQL管理用户(root)的密码。
1)方法1,在Shell命令行下设置
使用mysqladmin管理工具,需要验证旧的密码。比如,以下操作将会把root的密码设置为 1234567:

[root@dbsvr1 ~]# mysqladmin -u root -p password '1234567'
Enter password:  								//验证原来的密码

2)方法2,以root登入mysql> 后,使用SET PASSWORD指令设置
这个与新安装MySQL-server后首次修改密码时要求的方式相同,平时也可以用:

mysql> SET PASSWORD FOR root@localhost=PASSWORD('1234567');
Query OK, 0 rows affected (0.16 sec)

3)方法3,以root登入mysql> 后,使用GRANT授权工具设置
这个是最常见的用户授权方式(下一节会做更多授权的练习):

mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';
Query OK, 0 rows affected (0.00 sec)

4)方法4,以root登入mysql> 后,使用UPDATE更新相应的表记录
这种方法与恢复密码时的操作相同:

mysql> UPDATE mysql.user SET password=PASSWORD('1234567')
    -> WHERE user='root' AND host='localhost';  		//重设root的密码
Query OK, 0 rows affected (0.17 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> FLUSH PRIVILEGES;  								//刷新授权表
Query OK, 0 rows affected (0.00 sec)

在上述方法中,需要特别注意:当MySQL服务程序以 --skip-grant-tables 选项启动时,如果未执行“FLUSH PRIVILEGES;”操作,是无法通过SET PASSWORD或者GRANT方式来设置密码的。比如,验证这两种方式时,都会看到ERROR 1290的出错提示:

mysql> SET PASSWORD FOR root@localhost=PASSWORD('1234567');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值