kingshard SQL黑名單功能介紹

mvwan 8年前發布 | 31K 次閱讀 SQL

來自: http://segmentfault.com/a/1190000004359414

kingshard SQL黑名單功能介紹

1. 應用場景介紹

在kingshard開源之后,有用戶多次提到能不能在kingshard中加入SQL黑名單機制,讓kingshard能夠根據特定的規則來攔截在黑名單中的SQL。有幾個比較典型的應用場景:

delete from mytable
select count(*) from mytable where xxxx

2. 功能介紹

在kingshard如果想使用SQL黑名單功能,只需要在配置中添加:

blacklist_sql_file: /Users/flike/blacklist

然后我們在blacklist定義SQL黑名單,這樣kingshard在轉發的時候,就會阻止黑名單中SQL的轉發。

黑名單SQL以正則表達式的形式定義。對于SQL中的值用 ? 或 ?+ 代替。為保證黑名單有效,最好手動驗證一下,kingshard是否正確攔截了黑名單中的SQL。定義規則(上一條是原SQL,對應的下一條是黑名單形式的SQL)可以參考下列例子:

SELECT c FROM t WHERE id=1
select c from t where id=?

SELECT FROM prices.rt_5min where id=1 select from prices.rt_5min where id=?

select null, 5.001, 5001. from foo select ?, ?, ? from foo

select 'hello', '\nhello\n', \"hello\", '\'' from foo select ?, ?, ?, ? from foo

select 'hello'\n select ?

select from t where (base.nid IN ('1412', '1410', '1411')) select from t where (base.nid in(?+))

select from foo where a in (5) and b in (5, 8,9 ,9 , 10) select from foo where a in(?+) and b in(?+)

select from foo limit 5 select from foo limit ?

select from foo limit 5, 10 select from foo limit ?, ?

select from foo limit 5 offset 10 select from foo limit ? offset ?

INSERT INTO t (ts) VALUES (NOW()) insert into t (ts) values(?+)

insert into foo(a, b, c) values(2, 4, 5) insert into foo(a, b, c) values(?+)

CALL foo(1, 2, 3) call foo

LOAD DATA INFILE '/tmp/foo.txt' INTO db.tbl load data infile ? into db.tbl

administrator command: Init DB administrator command: Init DB

use foo use ?</pre>

3.功能演示

在blacklist加入如下SQL:

select count(*) from test_shard_hash where id > ?
select count(*) from test_shard_range
SELECT * FROM WORLD
DELETE FROM WORLD

連接kingshard,執行SQL顯示如下:

mysql> select  from world;
ERROR 1105 (HY000): sql in blacklist.
mysql> select  from world where a > 0;
+------+------+
| a    | b    |
+------+------+
|   10 |   23 |
|   45 |  565 |
+------+------+
2 rows in set (0.00 sec)

mysql> delete from world; ERROR 1105 (HY000): sql in blacklist. mysql> delete from world where a =10; Query OK, 1 row affected (0.00 sec)

注意在SQL黑名單中該SQL是大于后面有個空格,必須要嚴格匹配,否則#kingshard不會認為是黑名單SQL

mysql> select count() from test_shard_hash where id >1; +----------+ | count() | +----------+ | 24 | +----------+ 1 row in set (0.02 sec)

mysql> select count(*) from test_shard_hash where id > 1; ERROR 1105 (HY000): sql in blacklist.</pre>

用sysbench測試了一下存在blacklist時kingshad的性能,發現性能并沒有明顯下降,所以可以放心使用該功能。

</div>

 本文由用戶 mvwan 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!