密码恢复及设置 用户受权及撤销 MySQL管理工具
1 密码恢复及设置
1.1 问题php
本案例要求熟悉MySQL管理密码的控制,完成如下任务操做:html
练习重置MySQL管理密码的操做 经过正常途径设置MySQL数据库的管理密码
1.2 步骤node
实现此案例须要按照以下步骤进行。mysql
步骤一:重置MySQL管理密码web
1)首先中止已运行的MySQL服务程序sql
[root@dbsvr1 ~]# systemctl stop mysqld.service //中止服务 [root@dbsvr1 ~]# systemctl status mysqld.service //确认状态 mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: inactive (dead) since 五 2017-04-07 23:01:38 CST; 21s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 20260 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 20238 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 20262 (code=exited, status=0/SUCCESS)
2)而后跳过受权表启动MySQL服务程序数据库
这一步主要利用mysqld的 --skip-grant-tables选项vim
修改my.cnf配置,添加 skip_grant_tables=1启动设置:浏览器
[root@dbsvr1 ~]# vim /etc/my.cnf [mysqld] skip_grant_tables=1 .. .. [root@dbsvr1 ~]# systemctl restart mysqld.service [root@dbsvr1 ~]# service mysql status mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled) Active: active (running) since 五 2017-04-07 23:40:20 CST; 40s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 11698 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 11676 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 11701 (mysqld) CGroup: /system.slice/mysqld.service └─11701 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.p...
3)使用mysql命令链接到MySQL服务,重设root的密码安全
因为前一步启动的MySQL服务跳过了受权表,因此能够root从本机直接登陆
[root@dbsvr1 ~]# mysql -u root Enter password: //直接回车便可 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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 authentication_string=PASSWORD('1234567') -> WHERE user='root' AND host='localhost'; //重设root的密码 Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> FLUSH PRIVILEGES; //刷新受权表 Query OK, 0 rows affected (0.01 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 ~]# systemctl restart mysqld.service
验证无密码登陆时,将会被拒绝:
[root@dbsvr1 ~]# mysql -u root Enter password: //没有跳过受权表回车会报错 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 4 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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: //验证原来的密码 mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. //提示明文修改不安全,并非报错
2)方法2,以root登入mysql> 后,使用SET PASSWORD指令设置
这个与新安装MySQL-server后首次修改密码时要求的方式相同,平时也能够用:
mysql> SET PASSWORD FOR root@localhost=PASSWORD('1234567'); Query OK, 0 rows affected, 1 warning (0.00 sec)
3)方法3,以root登入mysql> 后,使用GRANT受权工具设置
这个是最多见的用户受权方式(下一节会作更多受权的练习):
mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567'; Query OK, 0 rows affected, 1 warning (0.00 sec)
4)方法4,以root登入mysql> 后,使用UPDATE更新相应的表记录
这种方法与恢复密码时的操做相同:
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('1234567') -> WHERE user='root' AND host='localhost'; //重设root的密码 Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 1 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
2 用户受权及撤销
2.1 问题
容许root从192.168.4.0/24网段 访问,对全部库/表有彻底权限,密码为tarena 添加一个管理帐号dba007,彻底控制及受权 撤销root从本机访问的权限,而后恢复 容许webuser从任意客户机登陆,只对webdb库有彻底权限,密码为 888888 撤销webuser的彻底权限,改成查询权限
2.2 方案
使用2台RHEL 7虚拟机,如图-1所示。其中192.168.4.10是MySQL服务器,受权及撤销操做均在此服务器上执行;而192.168.4.120做为测试客户机,须要安装好MySQL-client软件包,以便提供mysql命令。
图-1
同时,MySQL服务器自己(192.168.4.10)也能够做为测试客户机。
2.3 步骤
实现此案例须要按照以下步骤进行。
步骤一:用户受权及撤销
1)容许root从192.168.4.0/24访问,对全部库表有彻底权限,密码为tarena。
受权以前,从192.168.4.0/24网段的客户机访问时,将会被拒绝:
[root@host120 ~]# mysql -u root -p -h 192.168.4.10 Enter password: //输入正确的密码 ERROR 2003 (HY000): Host '192.168.4.120' is not allowed to connect to this MySQL server
受权操做,此处可设置与从localhost访问时不一样的密码:
mysql> GRANT all ON *.* TO root@'192.168.4.%' IDENTIFIED BY 'tarena'; Query OK, 0 rows affected (0.00 sec)
再次从192.168.4.0/24网段的客户机访问时,输入正确的密码后可登入:
[root@host120 ~]# mysql -u root -p -h 192.168.4.10 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 20 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> CREATE DATABASE rootdb; //建立新库rootdb Query OK, 1 row affected (0.06 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | home | | mysql | | performance_schema | | rootdb | //新建的rootdb库 | sys | | userdb | +--------------------+ 7 rows in set (0.01 sec)
2)在Mysql服务器上创建一个管理帐号dba007,对全部库彻底控制,并赋予其受权的权限
新建帐号并受权:
mysql> GRANT all ON *.* TO dba007@localhost -> IDENTIFIED BY '1234567' -> WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec)
查看dba007的权限:
mysql> SHOW GRANTS FOR dba007@localhost; +-----------------------------------------------------------------------+ | Grants for dba007@localhost | +-----------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'dba007'@'localhost' WITH GRANT OPTION | +-----------------------------------------------------------------------+ 1 row in set (0.00 sec)
3)撤销root从本机访问的权限,而后恢复
注意:若是没有事先创建其余管理帐号,请不要轻易撤销root用户的本地访问权限,不然恢复起来会比较困难,甚至不得不重装数据库。
撤销root对数据库的操做权限:
mysql> REVOKE all ON *.* FROM root@localhost; Query OK, 0 rows affected (0.00 sec) mysql> SHOW GRANTS FOR root@localhost; +--------------------------------------------------------------+ | Grants for root@localhost | +--------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'root'@'localhost' WITH GRANT OPTION | | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION | +--------------------------------------------------------------+ 2 rows in set (0.00 sec)
验证撤销后的权限效果:
mysql> exit //退出当前MySQL链接 Bye [root@dbsvr1 ~]# mysql -u root -p //从新以root从本地登入 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 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> CREATE DATABASE newdb2014; //尝试新建库失败 ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'newdb2014' mysql> DROP DATABASE rootdb; //尝试删除库失败 ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'rootdb'
尝试以当前的root用户恢复权限,也会失败(无权更新受权表):
mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567'; ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
怎么办呢?
退出当前MySQL链接,以上一步添加的管理帐号dba007登入:
mysql> exit //退出当前MySQL链接 Bye [root@dbsvr1 ~]# mysql -u dba007 -p //以另外一个管理帐号登入 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 24 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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.
由管理帐号dba007从新为root添加本地访问权限:
mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY '1234567'; Query OK, 0 rows affected (0.00 sec) mysql> SHOW GRANTS FOR root@localhost; //查看恢复结果 +---------------------------------------------------------------------+ | Grants for root@localhost | +---------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION | +---------------------------------------------------------------------+ 2 rows in set (0.00 sec)
退出,再从新以root登入,测试一下看看,权限又恢复了吧:
mysql> exit //退出当前MySQL链接 Bye [root@dbsvr1 ~]# mysql -u root -p //从新以root登入 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 25 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> CREATE DATABASE newdb2014; //成功建立新库 Query OK, 1 row affected (0.00 sec)
4)容许webuser从任意客户机登陆,只对webdb库有彻底权限,密码为 888888
添加受权:
mysql> GRANT all ON webdb.* TO webuser@'%' IDENTIFIED BY '888888'; Query OK, 0 rows affected (0.00 sec)
查看受权结果:
mysql> SHOW GRANTS FOR webuser@'%'; +----------------------------------------------------+ | Grants for webuser@% | +----------------------------------------------------+ | GRANT USAGE ON *.* TO 'webuser'@'%' | | GRANT ALL PRIVILEGES ON `webdb`.* TO 'webuser'@'%' | +----------------------------------------------------+ 2 rows in set (0.00 sec)
5)撤销webuser的彻底权限,改成查询权限
撤销全部权限:
mysql> REVOKE all ON webdb.* FROM webuser@'%'; Query OK, 0 rows affected (0.00 sec)
只赋予查询权限:
mysql> GRANT select ON webdb.* TO webuser@'%'; Query OK, 0 rows affected (0.00 sec)
确认受权更改结果:
mysql> SHOW GRANTS FOR webuser@'%'; +--------------------------------------------+ | Grants for webuser@% | +--------------------------------------------+ | GRANT USAGE ON *.* TO 'webuser'@'%' | | GRANT SELECT ON `webdb`.* TO 'webuser'@'%' | +--------------------------------------------+ 2 rows in set (0.00 sec)
3 MySQL管理工具
3.1 问题
本案例要求基于LAMP平台部署一套phpMyAdmin应用系统,实现对MySQL服务器的Web方式管理。
3.2 方案
使用2台RHEL6虚拟机 + 1台Windows 7真机,如图-2所示。其中192.168.4.10是MySQL服务器,受权操做在此服务器上执行;另外一台Linux服务器192.168.4.6上部署phpMyAdmin管理平台,实现从浏览器访问的Web管理方式。
图-2
3.3 步骤
实现此案例须要按照以下步骤进行。
步骤一:在MySQL服务器上配置用户访问受权
为了实验方便起见,直接以root用户为例,容许其从192.168.4.0/24网段访问,密码设置为1234567。
mysql> GRANT all ON *.* TO root@'192.168.4.%' IDENTIFIED BY '1234567'; Query OK, 0 rows affected, 1 warning (0.00 sec)
步骤二:搭建phpMyAdmin管理平台(192.168.4.6)
phpMyAdmin是以PHP语言开发的一套用来管理MySQL数据库的网页程序,所以须要有支持PHP的网站服务器才能正常使用phpMyAdmin平台。
1)LAMP平台的简易部署
直接以yum方式安装httpd、mysql、php、php-mysql软件包,本例中只须要MySQl客户端程序,无需安装mysql-server:
[root@dbsvr ~]# yum -y install httpd mariadb php php-mysql .. ..
因为RHEL 7未提供php-mbsring包,而phpMyAdmin套件须要相关库文件,所以须要额外下载适用的RPM包(由教员提供),安装时忽略依赖关系便可:
[root@dbsvr ~]# rpm -ivh php-mbstring-5.3.3-26.el6.x86_64.rpm -nodeps .. ..
完成安装之后,对httpd服务配置稍做调整,启动httpd网站服务:
[root@dbsvr ~]# vim /etc/httpd/conf/httpd.conf ServerName localhost.localdomain .. .. DirectoryIndex index.php index.html .. .. [root@dbsvr ~]# systemctl restart httpd
2)下载、部署phpMyAdmin套件
访问http://www.phpmyadmin.net/,下载支持多语言的源码程序包phpMyAdmin-4.1.2-all-languages.zip。
将下载回来的源码包解压,并部署到网站目录:
[root@dbsvr ~]# unzip phpMyAdmin-4.1.2-all-languages.zip [root@dbsvr ~]# mv phpMyAdmin-4.1.2-all-languages /var/www/html/pma
切换到部署后的pma程序目录,拷贝配置文件,并修改配置以正确指定MySQL服务器的IP地址。
[root@dbsvr ~]# cd /var/www/html/pma/ [root@dbsvr pma]# cp config.sample.inc.php config.inc.php [root@dbsvr pma]# vim config.inc.php <?php .. .. $cfg[‘blowfish_secret’]=’tarena’; //在单引号里随意添加字符,若是不修改这项,会报错【配置文件如今须要绝密的短语密码(blowfish_secret)】。 .. .. $cfg['Servers'][$i]['host'] = '192.168.4.10'; .. .. ?>
3)从浏览器访问phpMyAdmin系统
在Windows 7客户机中,打开IE网页浏览器,访问部署了phpMyAdmin系统的网站http://192.168.4.6/pma/index.php,便可打开phpMyAdmin管理平台。如图-3所示,输入正确的数据库用户名(如root)及密码登入便可。
图-3
登入成功后,如图-4所示,便可在受权范围内对MySQL数据库进行管理。
图-4