热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MySQL安全问题

对于任何一种数据库来说,安全问题都是非常重要的。如果数据库出现安全漏洞,轻则数据被窃取,重则数据被破坏,这些后果对于一些重要

对于任何一种数据库来说,安全问题都是非常重要的。如果数据库出现安全漏洞,轻则数据被窃取,重则数据被破坏,这些后果对于一些重要的数据库都是非常严重的。下面来从操作系统数据库两个层对MySQL 的安全问题进行讨论。

操作系统相关的安全问题

常见的操作系统安全问题主要出现在MySQL 的安装和启动过程中.

1、严格控制操作系统账号和权限

在数据库服务器上要严格控制操作系统的账号和权限,比如:

  • 锁定mysql用户

  • 其他任何用户都采取独立的账号登录,管理员通过mysql专有用户管理MySQL,或者通过root su到mysql用户下进行管理

  • mysql用户目录下,除了数据文件目录,其他文件和目录属主都改为root


2、尽量避免以root 权限运行MySQL

MySQL 安装完毕后,一般会将数据目录属主设置为mysql 用户,而将MySQL 软件目录的属主设置为root,这样做的目的是当使用mysql 启动数据库时,可以防止任何具有FILE 权限的用户能够用root 创建文件。而如果使用root 用户启动数据库,则任何具有FILE 权限的用户都可以读写root 用户的文件,这样会给系统造成严重的安全隐患。

3、防止DNS欺骗

创建用户时,host 可以指定域名或者IP 地址。但是,如果指定域名,就可能带来如下安全隐患:  如果域名对应的IP 地址被恶意修改,则数据库就会被恶意的IP 地址进行访问,导致安全隐患。

数据库相关的安全问题

常见的数据库问题大多数是由于账号的管理不当造成的。应该加强对账号管理的安全意识。

1、删除匿名账号

在某些版本的中,安装完毕MySQL 后,会自动安装一个空账号,此账号具有对test 数据库的全部权限,普通用户只需要执行mysql 命令即可登录MySQL 数据库,这个时候默认使用了空用户,可以在test 数据库里面做各种操作,比如可以创建一个大表,占用大量磁盘空间,这样给系统造成了安全隐患。

2、给root账号设置口令

MySQL 安装完毕后,root 默认口令为空,需要马上修改口令

3、设置安全密码

密码的安全体现在以下两个方面:

  • 设置安全的密码,建议使用6位以上字母、数字、下划线和一些特殊字符组合的而成的字符串

  • 使用上的安全,使用密码期间尽量保证使用过程安全,不会被别人窃取

第一点就不用说了,越长越复杂越没有规律的密码越安全。

对于第二点,可以总结一下,在日常工作中,使用密码一般是采用以下几种方式。

(1)直接将密码写在命令行中

mysql -uroot -p123

(2)交互式方式输入密码

mysql -uroot -p

(3)将用户名和密码写在配置文件里面,连接的时候自动读取,比如应用连接数据库或者执行一些批处理脚本。对于这种方式,MySQL 供了一种方法,在my.cnf 里面写入连接信息

[client]
user=username
password=password

然后对配置文件进行严格的权限限制,例如:

chomod +600 my.cnf

以上是3种常见的密码使用方式。很显然,第1种最不安全,因为它将密码写成为明文;第2种比较安全,但是只能使用在交互的界面下;第3种比较方便,但是需要将配置文件设置严格的存取权限,而且任何只要可以登录操作系统的用户都可能自动登录,存在一定的安全隐患。

第3种方法通常使用不多,下面举一个例子

(1)输入mysql 无法登录

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

(2)修改配置文件,加入连接信息

[root@iZ28dr6w0qvZ ~]# vim /etc/my.cnf
...
[client]
#password = your_password
user=cqh
password=123

(3)重启数据库后,输入mysql

[root@iZ28dr6w0qvZ ~]# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.. SUCCESS!
[root@iZ28dr6w0qvZ ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> select current_user();
+----------------+
| current_user() |
+----------------+
| cqh@localhost |
+----------------+
1 row in set (0.02 sec)

4、只授予账号必须的权限

只需要赋予普通用户必须的权限,比如:

grant select,insert,update,delete on tablename to 'username'@'hostname';

在很多情况下,DBA 由于图方便,而经常赋予用户all privileges 权限,这个all privileges 到底具体包含哪些权限呢?来看下面的例子:

mysql> select * from db where user='cqh'\G
*************************** 1. row ***************************Host: localhostDb: testUser: cqhSelect_priv: YInsert_priv: YUpdate_priv: YDelete_priv: YCreate_priv: YDrop_priv: YGrant_priv: NReferences_priv: YIndex_priv: YAlter_priv: Y
Create_tmp_table_priv: YLock_tables_priv: YCreate_view_priv: YShow_view_priv: YCreate_routine_priv: YAlter_routine_priv: YExecute_priv: YEvent_priv: YTrigger_priv: Y
1 row in set (0.00 sec)

all privileges 里面的权限,远远超过了我们一般应用所需要的权限。而且,有些权限如果误操作,将会产生非常严重的后果,比如drop_priv 等。因此,用户权限的时候越具体,则对数据库越安全。

5、除root 外,任何用户不应有mysql 库user 表的存取权限

由于MySQL 中可以通过更改mysql 数据库的user 表进行权限的增加、删除、变更等操作,因此,除了root 以外,任何用户都不应该拥有对user 表的存取权限(SELECT、UPDATE、INSERT、DELETE等),造成系统的安全隐患。下例对普通用户cqh 授予user 表的存取权限,看看会对系统产生了怎么样的安全隐患。

(1)创建普通用户chenqionghe,拥有对mysql数据库中的user表的各种权限

[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 103
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> grant select,update,insert,delete on mysql.user to chenqionghe@localhost;
Query OK, 0 rows affected (0.00 sec)

(2)用chenqionghe 来更新root 权限

[root@iZ28dr6w0qvZ ~]# mysql -uchenqionghe
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 106
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use mysql;
Database changed
mysql>
mysql> update user set password=password('abcd') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

(3)当数据库重启或者root 刷新权限表后,root 登录时密码已经被更改

[root@iZ28dr6w0qvZ ~]# mysql -uroot -pabcd
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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.

6、不要把FILE、PROCESS 或SUPER 权限授予管理员以外的账号

FILE权限主要以下作用:

将数据库的信息通过SELECT ...INTO OUTFILE...写到服务器上有写权限的目录下,作为文本格式存放。具有权限的目录也就是启动MySQL 时的用户权限目录。

可以将有读权限的文本文件通过LOAD DATA INFILE...命令写入数据表,如果这些表中存放了很重要的信息,将对系统造成很大的安全隐患。

在例中详细描述了FILE 权限可能造成的隐患

(1)连接数据库并创建测试表t

[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use test;
Database changed
mysql> create table t (name varchar(500));
Query OK, 0 rows affected (0.02 sec)

(2)将/etc/password文件加载到表t中

mysql> load data infile '/etc/passwd' into table t;
Query OK, 23 rows affected (0.01 sec)
Records: 23 Deleted: 0 Skipped: 0 Warnings: 0

(3)查看t的内容

mysql> select * from t;
+----------------------------------------------------------------------+
| name |
+----------------------------------------------------------------------+
| root:x:0:0:root:/root:/bin/bash |
| bin:x:1:1:bin:/bin:/sbin/nologin |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin |
| adm:x:3:4:adm:/var/adm:/sbin/nologin |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin |
| sync:x:5:0:sync:/sbin:/bin/sync |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown |
| halt:x:7:0:halt:/sbin:/sbin/halt |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin |
| uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin |
| operator:x:11:0:operator:/root:/sbin/nologin |
| games:x:12:100:games:/usr/games:/sbin/nologin |
| gopher:x:13:30:gopher:/var/gopher:/sbin/nologin |
| ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin |
| nobody:x:99:99:Nobody:/:/sbin/nologin |
| vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin |
| ntp:x:38:38::/etc/ntp:/sbin/nologin |
| saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin |
| postfix:x:89:89::/var/spool/postfix:/sbin/nologin |
| sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin |
| nscd:x:28:28:NSCD Daemon:/:/sbin/nologin |
| www:x:500:500::/alidata/www:/sbin/nologin |
| mysql:x:501:501::/home/mysql:/sbin/nologin

这样,重要的用户信息/etc/passwd内容将被写入表t中,造成安全隐患。

PROCESS权限能被用来执行“show processlist”命令,查看当前所有用户执行的查询的明文文本,包括设定或改变密码的查询。在默认情况下,每个用户都可以执行“show processlist”命令,但是只能查询本用户的进程。因此,对PROCESS权限管理不当,有可能会使得普通用户能够看到管理员执行的命令。

下例中对普通用户赋予了PROCESS权限,来看看会造成什么安全隐患。

(1)将PROCESS权限授予给普通用户

[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 2 | root | localhost | NULL | Sleep | 53 | | NULL |
| 26 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
2 rows in set (0.00 sec)
mysql> grant process on *.* to 'cqh'@'localhost';
Query OK, 0 rows affected (0.00 sec)

(2)锁定表user,可以让进程阻塞,以方便用户看到进程内容

mysql> lock table user read;
Query OK, 0 rows affected (0.00 sec)

(3)打开另外一个session,用root执行修改密码操作,此时因为user表被锁定,此进程被阻塞挂起

[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> set password=password('123');

(4)打开第3个session,用cqh登录,执行show processlist语句

[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> show processlist;
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| 26 | root | localhost | mysql | Sleep | 20 | | NULL |
| 27 | root | localhost | NULL | Query | 15 | Waiting for table level lock | set password=password('123') |
| 31 | cqh | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
3 rows in set (0.00 sec)

可以发现,cqh显示的进程中清楚地看到了root的修改密码操作,并看到了明文的密码,这将对系统造成严重的安全隐患。

SUPER权限能够执行kill命令,终止其他用户进程。下面例子中,普通用户拥有了SUPER 权限后,便可以任意kill 任何用户的进程。

(1)cqh 登录后想kill 掉root 修改密码进程(进程号27)

mysql> show processlist;
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| 26 | root | localhost | mysql | Sleep | 20 | | NULL |
| 27 | root | localhost | NULL | Query | 15 | Waiting for table level lock | set password=password('123') |
| 31 | cqh | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
3 rows in set (0.00 sec)
mysql> kill 27;
ERROR 1095 (HY000): You are not owner of thread 27

(2)kill失败后,root将super权限赋予cqh

mysql> grant super on *.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cqh@localhost;
+--------------------------------------------------+
| Grants for cqh@localhost |
+--------------------------------------------------+
| GRANT PROCESS, SUPER ON *.* TO 'cqh'@'localhost' |
+--------------------------------------------------+
1 row in set (0.00 sec)

(3)重新kill root的进程成功

[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> show processlist;
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| 26 | root | localhost | mysql | Sleep | 20 | | NULL |
| 27 | root | localhost | NULL | Query | 15 | Waiting for table level lock | set password=password('123') |
| 31 | cqh | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
3 rows in set (0.00 sec)
mysql> kill 27;
Query OK, 0 rows affected (0.00 sec)

从上面的例子中,可以看到FILE、PROCESS、SUPER三个管理权限可能会带来的安全隐患,因此除了管理员外,不要把这些权限赋予给普通用户。

7、LOAD DATA LOCAL带来的安全问题

LOAD DATA 默认读的是服务器上的文件,但是加上LOCAL 参数后,就可以将本地具有访问权限的文件加载到数据库中。这在在带来方便的同时,可带来了以下安全问题。 

可以任意加载本地文件到数据库。

在Web环境中,客户从Web服务器连接,用户可以使用LOAD DATA LOCAL语句来读取Web服务器进程在读访问权限的任何文件(假定用户可以运行SQL服务器的任何命令)。在这种环境中,MySQL服务器的客户实际上的是Web服务器,而不是连接Web服务器的用户运行的程序。

解决的方法是,可以用--local-infile=0 选项启动mysqld 从服务器禁用所有LOAD DATA LOCAL命令。

对于mysql 命令行客户端,可以通过指定--local-infile[=1] 选项启用LOAD DATA LOCAL,或通过--local-infile=0 选项禁用。类似地,对于mysqlimport,--local or -L 选项启用本地文件装载。在任何情况下,成功进行本地装载需要服务器启用相关选项。

8、DROP TABLE命令并不收回以前的相关访问权限

DROP表的时候,其他用户对此表的权限并没有被收回,这样导致重新创建同名的表时,以前其他用户对此表的权限会自动自动赋予,进而产生权限外流。因此,在删除表时,要同时取消其他用户在此表上的相应权限。

下面的例子说明了不收回相关访问授权的隐患。

(1)用root 创建用户cqh,授权test下所有表的select权限

mysql> grant select on test.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cqh@localhost;
+-----------------------------------------------+
| Grants for cqh@localhost |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost' |
| GRANT SELECT ON `test`.* TO 'cqh'@'localhost' |
+-----------------------------------------------+
2 rows in set (0.00 sec)

(2)cqh登录,测试权限

[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 287
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu |
| salary |
| t |
| t1 |
| t12 |
| t2 |
+----------------+
6 rows in set (0.00 sec)

(3)root登录,删除表t12

[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 288
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use test;
Database changed
mysql> drop table t12;
Query OK, 0 rows affected (0.00 sec)

(4)cqh登录,再次测试权限

[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 290
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu |
| salary |
| t |
| t1 |
| t2 |
+----------------+
5 rows in set (0.00 sec)

(5)此时t12表已经看不到了

mysql> show grants for cqh@localhost;
+-----------------------------------------------+
| Grants for cqh@localhost |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost' |
| GRANT SELECT ON `test`.* TO 'cqh'@'localhost' |
+-----------------------------------------------+
2 rows in set (0.00 sec)

权限仍然显示对test下所有表的有SELECT权限(安全漏洞)

(6)root再次登录,创建表t12

[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 292
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use test;
Database changed
mysql> create table t12(id int);
Query OK, 0 rows affected (0.03 sec)

(7)cqh登录,对t1权限依旧存在

[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 293
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu |
| salary |
| t |
| t1 |
| t12 |
| t2 |
+----------------+
6 rows in set (0.00 sec)

因此,对表做删除后,其他用户对此表的权限不会自动收回,一定要记住手工收回。

9、使用SSL

SSL(Secure Socket Layer,安全套接字层)是一种安全传输的协议,最初Netscape 公司所开发,用以保障在Internet 上数据传输之安全,利用 数据加密(Encryption)技术,可确保数据在网络上传输过程中不会被截取及窃听。

SSL协议提供的服务主要有:

(1)认证用户和服务器,确保数据发送到正确的客户机和服务器

(2)加密数据以防止数据中途被窃取

(3)维护数据的完整性,确保数据在传输过程中不被改变

在MySQL中,要想使用SSL进行安全传输,需要在命令行中或选项文件中设置“--ssl”选项。

对于服务器,“ssl”选项规定该服务器允许SSL 连接。对于客户端端程序,它允许客户使用SSL 连接。对于客户端程序,它允许客户端用SSL 连接服务器。单单该选项不足以使用SSL 连接。还必须指定--ssl-ca、--ssl-cert和--ssl-key选项。如果不想启用SSL,可以将选项指定为--skip-ssl 或--ssl=0 。

请注意,如果编译的服务器或客户端不支持SSL,则使用普通的示加密的连接。

确保使用SSL 连接的安全方式是,使用含 REQUIRE SSL子句的GRANT语句在服务器上创建一账户,然后使用该账户来连接服务器,服务器和客户端均应启用SSL支持。下面例子创建了一个含REQUIRE SSL子句的账号:

mysql> grant select on *.* to cqh identified by '123' REQUIRE ssl;
Query OK, 0 rows affected (0.00 sec)

  • --ssl-ca=file_name    含可信的SSL CA的清单的文件的路径

  • --ssl-cert=file_name    SSL证书文件名,用于建立安全连接

  • --ssl-key=file_name    SSL密钥文件名,用于建立 安全连接


10、如果可能,给所有用户加上访问IP 限制

对数据库来说,我们希望客户端过来的连接都是安全的,因此,就很有必要在创建用户的时候指定可以进行连接的服务器IP 或者HOSTNAME,只有符合授权的IP 或者HOSTNAME 才可以进行数据库的访问。

11、REVOKE 命令的漏洞

当用户多次赋予权限后,由于各种原因,需要将此用户的权限全部取消,此时,REVOKE 命令可能并不会按照我们的意愿执行,来看看下面的例子。

(1)连续赋予用户两次权限,其中,第2次是对所有数据库的所有权限

mysql> grant select,insert on test.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cqh@localhost;
+-------------------------------------------------------+
| Grants for cqh@localhost |
+-------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'cqh'@'localhost' |
| GRANT SELECT, INSERT ON `test`.* TO 'cqh'@'localhost' |
+-------------------------------------------------------+
2 rows in set (0.00 sec)

(2)此时,需要取消用户的所有权限

mysql> revoke all privileges on *.* from cqh@localhost;
Query OK, 0 rows affected (0.00 sec)

(3)我们很可能以为,此时用户已经没有任何权限了,而不会再去查看他的权限表。而实际上,此时的用户依然拥有test上的SELECT和INSERT权限

mysql> show grants for cqh@localhost;
+-------------------------------------------------------+
| Grants for cqh@localhost |
+-------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost' |
| GRANT SELECT, INSERT ON `test`.* TO 'cqh'@'localhost' |
+-------------------------------------------------------+
2 rows in set (0.00 sec)

(4)此时,再次用cqh登录,测试一下是否能对test数据库做操作

[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 395
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu |
| salary |
| t |
| t1 |
| t12 |
| t2 |
+----------------+
6 rows in set (0.00 sec)
mysql> insert into t1 values (1);
Query OK, 1 row affected (0.01 sec)

这个是MySQL 权限机制造成的隐患,在一个数据库上多次赋予权限,权限会自动合并;但是在多个数据库上多次赋予权限,每个数据库上都会认为是单独的一组权限,必须在此数据库上用REVOKE 命令来单进行权限收回,而 REVOKE ALL PRIVILEGES ON *.*  并不会替用户自动完成这个情况。


来源:雪上飞猪

www.cnblogs.com/chenqionghe/p/4873665.html


推荐阅读
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
author-avatar
沉佩颖岳玲
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有