Skip to content

Commit dabe284

Browse files
author
Chen Fei
committed
Merge pull request flike#121 from flike/feature-blacklist-sql
Feature blacklist sql
2 parents 4f3e1d0 + 8e32b09 commit dabe284

12 files changed

+1430
-17
lines changed

README_ZH.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ kingshard是一个由Go开发高性能MySQL Proxy项目,kingshard在满足基
3636

3737
[5.管理端命令介绍](./doc/KingDoc/admin_command_introduce.md)
3838

39-
[6.kingshard性能测试报告](./doc/KingDoc/kingshard_performance_test.md)
39+
[6.kingshard SQL黑名单功能介绍](./doc/KingDoc/sql_blacklist_introduce.md)
4040

4141
[7.kingshard的FAQ](./doc/KingDoc/function_FAQ.md)
4242

@@ -46,6 +46,7 @@ kingshard是一个由Go开发高性能MySQL Proxy项目,kingshard在满足基
4646

4747
[2.kingshard性能优化之网络篇](./doc/KingDoc/kingshard_performance_profiling.md)
4848

49+
[3.kingshard性能测试报告](./doc/KingDoc/kingshard_performance_test.md)
4950
## 鸣谢
5051
- 感谢[mixer](https://github.com/siddontang/mixer)作者siddontang, kingshard最初的版本正是基于mixer开发而来的。
5152
- 感谢[bigpyer](https://github.com/bigpyer),他对kingshard做了详细的性能测试,并撰写了一份非常详细的测试报告。

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Config struct {
3030
LogSql string `yaml:"log_sql"`
3131
SlowLogTime int `yaml:"slow_log_time"`
3232
AllowIps string `yaml:"allow_ips"`
33+
BlsFile string `yaml:"blacklist_sql_file"`
3334
Nodes []NodeConfig `yaml:"nodes"`
3435

3536
Schema SchemaConfig `yaml:"schema"`

doc/KingDoc/how_to_use_kingshard.md

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ log_sql : on
3636
slow_log_time : 100
3737
#日志文件路径,如果不配置则会输出到终端。
3838
log_path : /Users/flike/log
39+
# sql黑名单文件路径
40+
# 所有在该文件中的sql都会被kingshard拒绝转发
41+
#blacklist_sql_file: /Users/flike/blacklist
3942
# 只允许下面的IP列表连接kingshard,如果不配置则对连接kingshard的IP不做限制。
4043
allow_ips: 127.0.0.1
4144
@@ -422,3 +425,5 @@ kingshard的管理接口,目前还是命令行的方式。后续有时间打
422425

423426
## 7. 总结
424427
kingshard开源两个月以来,得到了很多开发者的关注。这足以证明,大家对数据库中间件是有需求的,希望出现一款简单好用的MySQL Proxy。kingshard经过这两个月的迭代开发,也比较稳定了。据了解,有几个公司正在对其进行尝试。后续作者的主要精力会放在优化kingshard的性能上,同时完善kingshard已有的功能。如果大家对kingshard有什么想法或建议,可以发邮件联系我(flikecn#126.com),非常乐意和大家交流。
428+
429+

doc/KingDoc/how_to_use_kingshard_EN.md

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ log_level : debug
3535
#log_sql: off
3636
#only log the query that take more than slow_log_time ms
3737
#slow_log_time : 100
38+
# the path of blacklist sql file
39+
# all these sqls in the file will been forbidden by kingshard
40+
#blacklist_sql_file: /Users/flike/blacklist
3841
# only allow this ip list ip to connect kingshard
3942
#allow_ips: 127.0.0.1
4043
@@ -461,3 +464,5 @@ mysql> admin server(opt,k,v) values('show','schema','config');
461464
## 6.Requirement and feedback
462465

463466
If You have new functional requirements about kingshard in the production environment, or find a bug in the process of using kingshard. Welcome to send a mail to `flikecn#126.com`, I will reply you as soon as possible.
467+
468+

doc/KingDoc/kingshard_install_document.md

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ log_sql : on
2727
slow_log_time : 100
2828
#日志文件路径,如果不配置则会输出到终端。
2929
log_path : /Users/flike/log
30+
# sql黑名单文件路径
31+
# 所有在该文件中的sql都会被kingshard拒绝转发
32+
#blacklist_sql_file: /Users/flike/blacklist
3033
# 只允许下面的IP列表连接kingshard,如果不配置则对连接kingshard的IP不做限制。
3134
allow_ips: 127.0.0.1
3235
@@ -107,3 +110,5 @@ schema :
107110
**2. kingshard采用的是yaml方式解析配置文件,需要注意的是yaml配置文件不允许出现tab键,且冒号后面需要跟一个空格。配置文件编写完成后,可以在[yaml lint](http://www.yamllint.com/)网站验证是否有格式错误。**
108111

109112
**3. windows下安装kingshard,参考[文档](https://github.com/flike/kingshard/wiki/%E5%9C%A8window%E4%B8%8B%E5%AE%89%E8%A3%85kingshard)**
113+
114+
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# kingshard SQL黑名单功能介绍
2+
3+
## 1. 应用场景介绍
4+
在kingshard开源之后,有用户多次提到能不能在kingshard中加入SQL黑名单机制,让kingshard能够根据特定的规则来拦截在黑名单中的SQL。有几个比较典型的应用场景:
5+
6+
1. DBA定义一些比较危险的SQL,放在SQL黑名单文件中。可以避免前端应用发过来的SQL对数据库造成危害。这种SQL有可能是开发者粗心编写的,也有可能是被SQL注入生成的SQL。例如:`delete from mytable`,这种不带where条件的SQL,会把整个表删除。
7+
2. 在kingshard项目上线后,通过log发现存在大量某条SQL给DB造成了很大的压力。这时候可以动态地将这条SQL加入黑名单,阻止该SQL的执行,从而使数据库压力降低。例如:`select count(*) from mytable where xxxx`,这类SQL如果没有优化得当,是很容易造成系统的IO过高的。
8+
9+
## 2. 功能介绍
10+
在kingshard如果想使用SQL黑名单功能,只需要在配置中添加:
11+
```
12+
blacklist_sql_file: /Users/flike/blacklist
13+
```
14+
然后我们在blacklist定义SQL黑名单,这样kingshard在转发的时候,就会阻止黑名单中SQL的转发。
15+
16+
黑名单SQL以正则表达式的形式定义。对于SQL中的值用`?``?+`代替。为保证黑名单有效,最好手动验证一下,kingshard是否正确拦截了黑名单中的SQL。定义规则(上一条是原SQL,对应的下一条是黑名单形式的SQL)可以参考下列例子:
17+
18+
```
19+
SELECT c FROM t WHERE id=1
20+
select c from t where id=?
21+
22+
SELECT * FROM prices.rt_5min where id=1
23+
select * from prices.rt_5min where id=?
24+
25+
select null, 5.001, 5001. from foo
26+
select ?, ?, ? from foo
27+
28+
select 'hello', '\nhello\n', \"hello\", '\\'' from foo
29+
select ?, ?, ?, ? from foo
30+
31+
select 'hello'\n
32+
select ?
33+
34+
select * from t where (base.nid IN ('1412', '1410', '1411'))
35+
select * from t where (base.nid in(?+))
36+
37+
select * from foo where a in (5) and b in (5, 8,9 ,9 , 10)
38+
select * from foo where a in(?+) and b in(?+)
39+
40+
select * from foo limit 5
41+
select * from foo limit ?
42+
43+
select * from foo limit 5, 10
44+
select * from foo limit ?, ?
45+
46+
select * from foo limit 5 offset 10
47+
select * from foo limit ? offset ?
48+
49+
INSERT INTO t (ts) VALUES (NOW())
50+
insert into t (ts) values(?+)
51+
52+
insert into foo(a, b, c) values(2, 4, 5)
53+
insert into foo(a, b, c) values(?+)
54+
55+
CALL foo(1, 2, 3)
56+
call foo
57+
58+
LOAD DATA INFILE '/tmp/foo.txt' INTO db.tbl
59+
load data infile ? into db.tbl
60+
61+
administrator command: Init DB
62+
administrator command: Init DB
63+
64+
use `foo`
65+
use ?
66+
67+
```
68+
69+
## 3.功能演示
70+
在blacklist加入如下SQL:
71+
72+
```
73+
select count(*) from test_shard_hash where id > ?
74+
select count(*) from test_shard_range
75+
SELECT * FROM WORLD
76+
DELETE FROM WORLD
77+
```
78+
79+
连接kingshard,执行SQL显示如下:
80+
81+
```
82+
mysql> select * from world;
83+
ERROR 1105 (HY000): sql in blacklist.
84+
mysql> select * from world where a > 0;
85+
+------+------+
86+
| a | b |
87+
+------+------+
88+
| 10 | 23 |
89+
| 45 | 565 |
90+
+------+------+
91+
2 rows in set (0.00 sec)
92+
93+
mysql> delete from world;
94+
ERROR 1105 (HY000): sql in blacklist.
95+
mysql> delete from world where a =10;
96+
Query OK, 1 row affected (0.00 sec)
97+
#注意在SQL黑名单中该SQL是大于后面有个空格,必须要严格匹配,否则#kingshard不会认为是黑名单SQL
98+
mysql> select count(*) from test_shard_hash where id >1;
99+
+----------+
100+
| count(*) |
101+
+----------+
102+
| 24 |
103+
+----------+
104+
1 row in set (0.02 sec)
105+
106+
mysql> select count(*) from test_shard_hash where id > 1;
107+
ERROR 1105 (HY000): sql in blacklist.
108+
```
109+
110+
用sysbench测试了一下存在blacklist时kingshad的性能,发现性能并没有明显下降,所以可以放心使用该功能。

etc/ks.yaml

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ addr : 0.0.0.0:9696
55
user : kingshard
66
password : kingshard
77

8-
#if set log_path, the sql log will write into log_path/sql.log,the system log
9-
#will write into log_path/sys.log
8+
# if set log_path, the sql log will write into log_path/sql.log,the system log
9+
# will write into log_path/sys.log
1010
#log_path : /Users/flike/log
1111

1212
# log level[debug|info|warn|error],default error
1313
log_level : debug
14-
#if set log_sql(on|off) off,the sql log will not output
15-
#log_sql: off
16-
#only log the query that take more than slow_log_time ms
14+
15+
# if set log_sql(on|off) off,the sql log will not output
16+
log_sql: off
17+
18+
# only log the query that take more than slow_log_time ms
1719
#slow_log_time : 100
20+
21+
# the path of blacklist sql file
22+
# all these sqls in the file will been forbidden by kingshard
23+
blacklist_sql_file: /Users/flike/blacklist
24+
1825
# only allow this ip list ip to connect kingshard
1926
#allow_ips: 127.0.0.1
2027

etc/unshard.yaml

+14-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ addr : 0.0.0.0:9696
55
user : kingshard
66
password : kingshard
77

8-
#if set log_path, the sql log will write into log_path/sql.log,the system log
9-
#will write into log_path/sys.log
8+
# if set log_path, the sql log will write into log_path/sql.log,the system log
9+
# will write into log_path/sys.log
1010
#log_path : /Users/flike/log
1111

1212
# log level[debug|info|warn|error],default error
1313
log_level : debug
14-
#if set log_sql(on|off) off,the sql log will not output
15-
log_sql: off
16-
#only log the query that take more than slow_log_time ms
14+
15+
# if set log_sql(on|off) off,the sql log will not output
16+
#log_sql: off
17+
18+
# only log the query that take more than slow_log_time ms
1719
#slow_log_time : 100
20+
21+
# blacklist sql file path
22+
# all these sqls in this file will been forbidden by kingshard
23+
#blacklist_sql_file: /Users/flike/blacklist
24+
1825
# only allow this ip list ip to connect kingshard
1926
#allow_ips: 127.0.0.1
2027

@@ -38,7 +45,8 @@ nodes :
3845
slave :
3946
down_after_noalive : 32
4047

41-
# schema defines which db can be used by client and this db's sql will be executed in which nodes, the db is also the default database
48+
# schema defines which db can be used by client and this db's sql will be executed in which nodes,
49+
# the db is also the default database
4250
schema :
4351
db : kingshard
4452
nodes: [node1]

0 commit comments

Comments
 (0)