MySQL安全问题(防范必知)
对于任何一种数据库来说,安全问题都是非常重要的。如果数据库出现安全漏洞,轻则数据被窃取,重则数据被破坏,这些后果对于一些重要的数据库都是非常严重的。下面来从操作系统和数据库两个层对MySQL的安全问题进行讨论。
操作系统相关的安全问题
1.严格控制操作系统账号和权限
- 锁定mysql用户
- 其他任何用户都采取独立的账号登录,管理员通过mysql专有用户管理MySQL,或者通过root su到mysql用户下进行管理。
- mysql用户目录下,除了数据文件目录,其他文件和目录属主都改为root
2.尽量避免以root权限运行MySQL
3.防止DNS欺骗
数据库相关的安全问题
1.删除匿名账号
2.给root账号设置口令
set password=password('newpassword');
3.设置安全密码
- 设置安全的密码,建议使用6位以上字母、数字、下划线和一些特殊字符组合的而成的字符串;
- 使用上的安全,使用密码期间尽量保证使用过程安全,不会被别人窃取。
mysql -uroot -p123
mysql -uroot -p
[client]
user=username
password=password
chomod +600 my.cnf
[root@iZ28dr6w0qvZ ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@iZ28dr6w0qvZ ~]# vim /etc/my.cnf
...
[client]
#password = your_password
user=cqh
password=123
[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';
mysql> select * from db where user='cqh'\G
*************************** 1. row ***************************
Host: localhost
Db: test
User: cqh
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Grant_priv: N
References_priv: Y
Index_priv: Y
Alter_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Execute_priv: Y
Event_priv: Y
Trigger_priv: Y
1 row in set (0.00 sec)
5.除root外,任何用户不应有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)
[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
[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权限授予管理员以外的账号
[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)
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
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
[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)
mysql> lock table user read;
Query OK, 0 rows affected (0.00 sec)
[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');
[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)
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
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)
[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)
7.LOAD DATA LOCAL带来的安全问题
8.DROP TABLE命令并不收回以前的相关访问权限
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)
[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)
[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)
[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)
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)
[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)
[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
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限制
11.REVOKE命令的漏洞
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)
mysql> revoke all privileges on *.* from 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, INSERT ON `test`.* TO 'cqh'@'localhost' |
+-------------------------------------------------------+
2 rows in set (0.00 sec)
[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安全问题(防范必知)的更多相关文章
- 使用MySQL正则表达式 __MySQL必知必会
正则表达式作用是匹配方本,将一个模式(正则表达式)与一个文本串进行比较. MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据. MySQL仅支持 ...
- MySQL必知必会1-20章读书笔记
MySQL备忘 目录 目录 使用MySQL 检索数据 排序检索数据 过滤数据 数据过滤 用通配符进行过滤 用正则表达式进行搜索 创建计算字段 使用数据处理函数 数值处理函数 汇总数据 分组数据 使用子 ...
- 《MySQL 必知必会》读书总结
这是 <MySQL 必知必会> 的读书总结.也是自己整理的常用操作的参考手册. 使用 MySQL 连接到 MySQL shell>mysql -u root -p Enter pas ...
- mysql学习--mysql必知必会1
例如以下为mysql必知必会第九章開始: 正則表達式用于匹配特殊的字符集合.mysql通过where子句对正則表達式提供初步的支持. keywordregexp用来表示后面跟的东西作为正則表達式 ...
- 《MySQL必知必会》[01] 基本查询
<MySQL必知必会>(点击查看详情) 1.写在前面的话 这本书是一本MySQL的经典入门书籍,小小的一本,也受到众多网友推荐.之前自己学习的时候是啃的清华大学出版社的计算机系列教材< ...
- mysql必知必会系列(一)
mysql必知必会系列是本人在读<mysql必知必会>中的笔记,方便自己以后查看. MySQL. Oracle以及Microsoft SQL Server等数据库是基于客户机-服务器的数据 ...
- 《mysql必知必会》读书笔记--存储过程的使用
以前对mysql的认识与应用只是停留在增删改查的阶段,最近正好在学习mysql相关内容,看了一本书叫做<MySQL必知必会>,看了之后对MySQL的高级用法有了一定的了解.以下内容只当读书 ...
- mysql必知必会
春节放假没事,找了本电子书mysql必知必会敲了下.用的工具是有道笔记的markdown文档类型. 下面是根据大纲已经敲完的章节,可复制到有道笔记的查看,更美观. # 第一章 了解SQL## 什么是S ...
- 《MySQL必知必会》整理
目录 第1章 了解数据库 1.1 数据库基础 1.1.1 什么是数据库 1.1.2 表 1.1.3 列和数据类型 1.1.4 行 1.1.5 主键 1.2 什么是SQL 第2章 MySQL简介 2.1 ...
随机推荐
- LPC43xx Dual-core or Multi-core configuration and JLink Debug
Test access port (TAP) JTAG defines a TAP (Test access port). The TAP is a general-purpose port that ...
- Dom4J解析xml文件动态转换为List<Bean>或者Map集合
大家在解析大量相似xml文件的时候是否会遇到这样一个问题:冗余的代码去set定义的实体对象Bean的值,基本都是一样的操作 而且毫无任何代码价值可言所以在这写了一个简单的例子,类封装了几个方法你只 ...
- 跨平台web调试代理工具---whistle
whistle是基于Node实现的跨平台web调试代理工具,支持windows.mac.linux等所有安装了Node的操作系统,可以部署在本地机器.虚拟机或远程服务器,并通过本地网页查看或修改HTT ...
- SQLServer公历转农历函数(1900年-2049年)
ALTER FUNCTION [dbo].[f_SysGetLunar]( @solarDay DATETIME) RETURNS varchar(20 ...
- mysql设置连接超时时间参数:wait_timeout
[root@ ~]# mysql -h 192.168.0.* -uroot -pEnter password: Welcome to the MySQL monitor. Commands end ...
- 对于程序开发者看书(指实在的书而不是PDF)的好处。(个人看法而已)
书是实在的东西.不同PDF.他能带你进入一种学习态度的环境 书上已经所列了知识点.看了.那些知识点就是你的. 第一次看,未必完全理解到里面的东西.说不定过几天,几周,几个月,甚至几年.再看.就有可能看 ...
- 聊聊IO多路复用之select、poll、epoll详解
本文转载自: http://mp.weixin.qq.com/s?__biz=MzAxODI5ODMwOA==&mid=2666538922&idx=1&sn=e6b436ef ...
- 纯CSS3实现3D特效的iPhone 6动画
iPhone 6发布不久,屌丝怎能买得起,不过作为程序员,今天看到一个用纯CSS3绘制的iPhone 6,由于CSS3特性的运用,带有点3D的动画特效,大家可以先来看看在线演示效果. 在线演示 ...
- ImageSource的使用
很多时候,我们会使用图片来装饰UI,比如作为控件背景等.而这些图片可以分为两种形式,即存在于本地文件系统中的图片和存在于内存中的图片对于这两种形式的图片,在WPF中,使用方法不同,下面主要说明针对这两 ...
- IOS Application Security Testing Cheat Sheet
IOS Application Security Testing Cheat Sheet [hide] 1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Int ...