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 ...
随机推荐
- 努力学习 HTML5 (4)—— 浏览器对语义元素的支持情况
经过上一节学习,我们已经建立一个结构良好的页面,如果在旧版的 IE 浏览器中浏览可能这些语义元素无法显示. 毕竟这些语义元素什么也不做,要支持它们,只要让浏览器把它们当做普通的 <div> ...
- 【LeetCode】338. Counting Bits (2 solutions)
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- HashSet HashTable HashMap的区别 及其Java集合介绍
(1)HashSet是set的一个实现类,hashMap是Map的一个实现类,同时hashMap是hashTable的替代品(为什么后面会讲到). (2)HashSet以对象作为元素,而HashMap ...
- ArcGIS Geodatabase版本控制机制的学习总结
本文是最近内部的一个学习的自我整理,只有关键信息,如果需要详细了解,请参阅ArcGIS帮助文档: http://resources.arcgis.com/zh-cn/help/main/10.1/in ...
- mac os x 安装mysql遇到 Access denied for user 'root'@'localhost' (using password: YES)的解决方法
最近把开发环境迁移到macbook上,装上mysql启动之后,连接,总是报这个错误.5.5的版本默认密码为空.其实这个错误是root未授权的问题.解决方法如下: 未给localhost root用户授 ...
- 关于Android真机调测Profiler
U3D中的Profile也是可以直接在链接安卓设备运行游戏下查看的,导出真机链接U3D的Profile看数据,这样能更好的测试具体原因. 大概看了下官方的做法,看了几张帖子顺带把做法记录下来. ...
- Oracle数据库入门——高水位线详解
一.什么是水线(High Water Mark)? 所有的oracle段(segments,在此,为了理解方便,建议把segment作为表的一个同义词) 都有一个在段内容纳数据的上限,我们把这个上限称 ...
- 使用grunt构建seajs项目
1.安装nodejs 2.安装grunt-cli npm install -g grunt-cli 3.进入到项目目录,同时准备好package.json和Gruntfile.js文件 //packa ...
- 优化IIS7.5支持10万个同时请求windows 2008 R2
通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请求数限制,TCPIP连接数等方面,从而使WEB服务器的性能得以提升,保证WEB访问的访问流畅. -
- CSS的sprite和单位
(1).关于css sprite技术 比方说: 有个论坛频道,其中有个一些论坛特有的小图标(火啊,顶啊之类),基于整站小图标大团结的思想,这些小图标也放在了那个icon背景图片上了.然而,数年下来,我 ...