close

下一站:HandlerSocket!

http://www.inspirr.com


loose_handlersocket_port = 9998
# the port number to bind to
loose_handlersocket_port_wr = 9999
# the port number to bind to
loose_handlersocket_threads = 16
# the number of worker threads
loose_handlersocket_threads_wr = 1
# the number of worker threads
open_files_limit = 65535
# to allow handlersocket accept many concurrent
# connections, make open_files_limit as large as
# possible.

此外,InnoDB的innodb_buffer_pool_size,或MyISAM的key_buffy_size等關系到緩存索引的選項盡可能設置大一些,這樣才能發揮HandlerSocket的潛力。

注:apt包管理下的配置文件一般是/etc/mysql/my.cnf,否則一般是/etc/my.cnf

最后登陸MySQL并激活HandlerSocket插件:

mysql> INSTALL PLUGIN handlersocket soname 'handlersocket.so';

如果沒有問題的話,就能在MySQL里看到HandlerSocket的線程了:

mysql> SHOW PROCESSLIST;

也可以通過查詢剛配置的端口是否已經被MySQL占用來確認是否安裝成功:

shell> lsof -i :9998
shell> lsof -i :9999

完活兒!現在你的MySQL已經具備NoSQL的能力了!

實戰

首先創建一個測試用的表:

CREATE TABLE IF NOT EXISTS `test`.`t` unsigned NOT NULL AUTO_INCREMENT,
  `a` varchar NOT NULL,
  `b` varchar NOT NULL,
  PRIMARY KEY ,
  KEY `a_b`
) ENGINE=InnoDB;

注:理論上HandlerSocket支持MyISAM,InnoDB等各種引擎,不過推薦使用InnoDB。

HandlerSocket的協議非常簡單,指令通過TAB分割,一行就是一個請求。

打開索引:P <索引標識> <數據庫> <表> <索引> <字段>

插入數據:<索引標識> ‘+’ <參數個數> <參數1> … <參數N>

讀取數據:<索引標識> <操作> <參數個數> <參數1> … <參數N> <條數> <偏移>

SQL原型:INSERT INTO test.t VALUES ,

shell> telnet localhost 9999
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
P       1       test    t       PRIMARY id,a,b
0       1
1       +       3       1       a1      b1
0       1       0
1       +       3       2       a2      b2
0       1       0

注:使用HandlerSocket時,因為沒有實際運行SQL,所以Binlog記錄的是Row格式。

SQL原型:SELECT id, a, b FROM test.t WHERE id = 1 LIMIT 1

shell> telnet localhost 9999
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
P       1       test    t       PRIMARY id,a,b
0       1
1       =       1       1       1       0
0       3       1       a1      b1

SQL原型:SELECT id, a, b FROM test.t WHERE id >=1 LIMIT 2

shell> telnet localhost 9999
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
P       1       test    t       PRIMARY id,a,b
0       1
1       >=      1       1       2       0
0       3       1       a1      b1      2       a2      b2

SQL原型:SELECT id, a, b FROM test.t WHERE a = ‘a1′ AND b = ‘b1′ LIMIT 1

shell> telnet localhost 9999
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
P       1       test    t       a_b     id,a,b
0       1
1       =       2       a1      b1      1       0
0       3       1       a1      b1

對HandlerSocket一個常見的誤解是只能執行PRIMARY類型的KV查詢,實際上只要支持索引,一般的簡單查詢它都能勝任,篇幅所限,這里就不多說了,如果你覺得直接操作telnet有些吃力,也可以使用自己熟悉的客戶端來測試,官方文檔里有介紹。

注:HandlerSocket作者寫了一個不錯的PPT可以參考:HandlerSocket plugin for MySQL

互聯網技術發展猶如一列高速運行的火車,下一站:HandlerSocket!請系好安全帶。


  Tag: 設計公司 | 網頁設計公司 | 廣告公司 | 網站設計 | 平面設計 | 互動媒體 | 網頁設計 | Web design | Website design | design house | 媒體公司 | Iphone app | 程式設計 | Flash 網頁 | Flash game | 動畫設計 | 後期製作 | 網上商店 | 網上宣傳 | 網頁服務 |

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 zhxalcy 的頭像
    zhxalcy

    陽光下的笑

    zhxalcy 發表在 痞客邦 留言(0) 人氣()