喜歡Mysql,不僅僅是因為他簡單的使用,更深一層次的是因為他的開源、他的插入式引擎及更優秀的plugin!從隊列式儲存引擎Q4M(http://q4m.github.com/)到memcache 的UDF(http://hi.baidu.com/ytjwt/blog/item/5fc8303f226c542f71cf6c3c.html)
到本文要說到的NoSQL for
MySQL,去年的某一天,一朋友跟我說,nosql效能有多好多好時,我說,如果提取像k/v形式的資料,假設:用主鍵查詢一個資料,你覺得效能會怎樣
呢?其實當時我也知道,肯定是比不過memcache之類的cache,因為,mysql有一系列的認證,一系列的文法、詞法分析!
前段一個偶然的時間裡,下載percona-server突然發現一個名叫HandlerSocket的東東,一時好奇,google一把,發現這不就是我一直想要的在mysql裡實現nosql的東西嗎?興奮之餘,也動手安裝及使用了一把,下面把我的步驟列出來!
主要參考:http://whitesock.javaeye.com/blog/811339
1:安裝
HandlerSocket老簡單了,只需要5.1版本以上的source 安裝版本跟二進位版本,因為5.5GA了,所以我選擇了5.5.8版本安裝
安裝HandlerSocket
$ ./autogen.sh
$ ./configure
--with-mysql-source=../mysql-5.5.8/
--with-mysql-bindir=/usr/local/mysql55/bin/
--with-mysql-plugindir=/usr/local/mysql55/lib/plugin/
$ make
$ make install
2:配置
在正式使用前,我們必須在mysql設定檔添加如下配置
[mysqld]
loose_handlersocket_port = 9998
# the port number to bind to (for read requests)
loose_handlersocket_port_wr = 9999
# the port number to bind to (for write requests)
loose_handlersocket_threads = 16
# the number of worker threads (for read requests)
loose_handlersocket_threads_wr = 1
# the number of worker threads (for write requests)
open_files_limit = 65535
# to allow handlersocket accept many concurrent connections, make open_files_limit as large as possible.
重啟mysql後,登入mysql,執行
install plugin handlersocket soname 'handlersocket.so';
這個時候,我們就能看到兩個新連接埠了
netstat -lnp|grep 999
tcp 0 0 0.0.0.0:9998 0.0.0.0:* LISTEN 32010/mysqld
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 32010/mysqld
mysql> show plugins;
| handlersocket | ACTIVE | DAEMON | handlersocket.so | BSD |
+-----------------------+----------+--------------------+------------------+---------+
21 rows in set (0.00 sec)
mysql> show processlist;
+----+-------------+-----------------+---------------+---------+------+-------------------------------------------+------------------+
|
Id | User | Host | db | Command | Time |
State | Info |
+----+-------------+-----------------+---------------+---------+------+-------------------------------------------+------------------+
|
2 | system user | connecting host | NULL | Connect | NULL |
handlersocket: mode=rd, 0 conns, 0 active | NULL |
| 3 | system user | connecting host | NULL | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL
這樣就說明已經正常使用了!
3:使用
我使用perl對handlersocket進行測試使用,表也用網上的表(^-^方便)
CREATE TABLE `user` (
`user_id` int(10) unsigned NOT NULL,
`user_name` varchar(50) DEFAULT NULL,
`user_email` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`user_id`),
KEY `INDEX_01` (`user_name`)
) ENGINE=InnoDB;
手動insert 幾條語句insert into user values(4, "yangting", "D@test.com", CURRENT_TIMESTAMP);
1)SELECT
如下我通過handlersocket對錶進行查詢
#!/usr/bin/perl
use strict;
use warnings;
use Net::HandlerSocket;
#1. establishing a connection
my $args = { host => 'localhost', port => 9998 };
my $hs = new Net::HandlerSocket($args);
#2. initializing an index so that we can use in main logics.
# MySQL tables will be opened here (if not opened)
my $res = $hs->open_index(0, 'test', 'user', 'PRIMARY', 'user_name,user_email,created');
#這個為查詢的列數,這裡為三列,分別為:user_name,user_email,created
die $hs->get_error() if $res != 0;
#3. main logic
#fetching rows by id
#execute_single (index id, cond, cond value, max rows, offset)
# 下面這個語句的意思是:查詢主鍵大於或等於1,總行數為2行的資料
$res = $hs->execute_single(0, '>=', [ 1 ], 2, 0);
# 這裡我們要注意:execute_single方法的傳回值類型為arrayref,其第一個元素為error code:如果為0,則為正常,否則不正常,數組從第二元素開始即為返回的值,儲存格式為 後面一行緊跟前面一行!
die $hs->get_error() if $res->[0] != 0;
shift(@$res);
#下面為分行列印這個數組的所有值
for (my $row = 0; $row <8 ; ++$row) {
my $user_name= $res->[$row*3 + 0];
my $user_email= $res->[$row*3 + 1];
my $created= $res->[$row*3 + 2];
if ($user_name || $user_email || $created)
{
print "$user_name\t$user_email\t$created\n";
}
else
{ last;
}
}
#4. closing the connection
$hs->close();
2)INSERT
my $args = { host => 'localhost', port => 9999 };
my $hs = new Net::HandlerSocket($args);
my $res = $hs->open_index(3, 'test', 'user', 'PRIMARY', 'user_id,user_name,user_email,created');
die $hs->get_error() if $res != 0;
#INSERT
$res = $hs->execute_single(3,'+', [5, 'zhongguo', 'zhogonguo@email.com','2011-01-08 13:51:33' ],1,0);
die $hs->get_error() if $res->[0] != 0;
$hs->close();
3)UPDATE
my $args = { host => 'localhost', port => 9999 };
my $hs = new Net::HandlerSocket($args);
my $res = $hs->open_index(3, 'test', 'user', 'PRIMARY', 'user_name');
die $hs->get_error() if $res != 0;
#當user_id=5,更新'user_name'為woaini
$res = $hs->execute_single(3,'=', [5],1,0,'U',['woaini']);
die $hs->get_error() if $res->[0] != 0;
$hs->close();
4) DELETE
my $args = { host => 'localhost', port => 9999 };
my $hs = new Net::HandlerSocket($args);
my $res = $hs->open_index(3, 'test', 'user', 'PRIMARY', 'user_name');
die $hs->get_error() if $res != 0;
#DELETE user_id=4 的資料
$res = $hs->execute_single(3,'=', [4],1,0,'D');
print $res;
die $hs->get_error() if $res != 0;
$hs->close();
這裡我只關注了使用,對於效能,有時間,還是需要測試一把,不過,HandlerSocket作者自己就測試了
參照:http://yoshinorimatsunobu.blogspot.com/2010/10/using-mysql-as-nosql-story-for.html