HandlerSocket简介
HandlerSocket是针对Mysql的一个NoSQL插件,它作为一个守护进程工作在mysqld进程里面,接收tcp连接,并处理来自客户端的请求。HandlerSocket不支持SQL查询,作为替代,它支持表的简单的CRUD操作。
由于下面的原因,在某些情况下HandlerSocket比mysqld/libmysql对儿更快速:
-HandlerSocket 处理数据不需要解析SQL,由于这个原因使得其占用少量CPU资源。
-HandlerSocket 从客户端批量读取多个请求并处理他们,这使得其占用更少的CPU和磁盘使用率。
-HandlerSocket 客户端/服务器协议比mysql/libmysql对儿更简洁,这使得其占用更少的网络使用率。
Mysql版本需要5.1及以上,mysql的安装就不写了
一、服务器端安装、配置、及测试
1、下载
[root@vm3 ~]# git clone http://github.com/DeNADev/HandlerSocket-Plugin-for-MySQL.git或
[root@vm3 ~]# wget -O HandlerSocket-Plugin-for-MySQL.zip --no-check-certificate https://github.com/DeNADev/HandlerSocket-Plugin-for-MySQL/archive/master.zip2、安装
[root@vm3 ~]# cd HandlerSocket-Plugin-for-MySQL-master/
[root@vm3 HandlerSocket-Plugin-for-MySQL-master]# ./autogen.sh
[root@vm3 HandlerSocket-Plugin-for-MySQL-master]# ./configure --with-mysql-source=../mysql-5.1.62 --with-mysql-bindir=/usr/local/mysql/bin --with-mysql-plugindir=/usr/local/mysql/lib/mysql/plugin/
[root@vm3 HandlerSocket-Plugin-for-MySQL-master]# make
[root@vm3 HandlerSocket-Plugin-for-MySQL-master]# make install
3、配置
以root身份登录到mysql,用下面命令安装handlersocket插件
mysql> install plugin handlersocket soname 'handlersocket.so';
Query OK, 0 rows affected (0.00 sec)
mysql> show plugins;
| handlersocket | ACTIVE | DAEMON | handlersocket.so | BSD |编辑my.cnf文件,在[mysqld]段下加入下列配置项
loose_handlersocket_port = 9998
loose_handlersocket_port_wr = 9999
loose_handlersocket_threads = 4
loose_handlersocket_threads_wr = 1
loose_handlersocket_address = 192.168.18.240重启mysql服务
[root@vm3 ~]# service mysqld restart
4、验证部署
查看handlersocket线程
mysql> show processlist;
|3| system user|connecting host|NULL|Connect|NULL|handlersocket:mode=rd,0 conns,0 active|NULL|
|4| system user|connecting host|NULL|Connect|NULL|handlersocket:mode=rd,0 conns,0 active|NULL|
|5| system user|connecting host|NULL|Connect|NULL|handlersocket:mode=rd,0 conns,0 active|NULL|
|6| system user|connecting host|handlersocket|Connect|NULL|handlersocket:mode=wr,0 conns,0 active|NULL|
|7| system user|connecting host|NULL|Connect|NULL|handlersocket:mode=rd,0 conns,0 active|NULL|查看监听端口
[root@vm3 ~]# netstat -ntupl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local AddressForeign AddressStatePID/Program name
tcp000.0.0.0:33060.0.0.0:*LISTEN 5522/mysqld
tcp00192.168.18.240:99980.0.0.0:*LISTEN 5522/mysqld
tcp00192.168.18.240:99990.0.0.0:*LISTEN 5522/mysqld
至此服务器端部署完毕,下面部署客户端,客户端使用PHP程序语言
二、客户端安装、配置、测试
1、下载
[root@vm4 ~]# wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.2.0.tar.gz
这里使用的不是最新的0.3.1版本,而是老的0.2.0,版本,新版本安装时报错,如有兄弟成功安装,请告知,3ks
2、安装
[root@vm4 ~]# tar zxvf php-handlersocket-0.2.0.tar.gz
[root@vm4 ~]# cd php-handlersocket/
[root@vm4 php-handlersocket]# phpize
[root@vm4 php-handlersocket]# ./configure --with-php-config=/usr/bin/php-config
[root@vm4 php-handlersocket]# make
[root@vm4 php-handlersocket]# make install
3、配置
本例使用的是rpm安装的php,编辑/etc/php.ini,添加或修改如下行
extension_dir = "/usr/lib64/php/modules"
extension=handlersocket.so
4、验证
重启httpd服务
[root@vm4 handlersocket]# service httpd restart编写phpinfo.php
[root@vm4 html]# vim /var/www/html/phpinfo.php
phpinfo();访问页面,看看是否加载了handlersocket扩展
ok,扩展已经启用了
5、测试
使用下面代码测试
$host = '192.168.18.240';
$port = 9998;
$port_wr = 9999;
$dbname = 'hstestdb';
$table = 'hstesttbl';
//GET
$hs = new HandlerSocket($host, $port);
if (!($hs->openIndex(1, $dbname, $table, HandlerSocket::PRIMARY, 'k,v')))
{
echo $hs->getError(), PHP_EOL;
die();
}
$retval = $hs->executeSingle(1, '=', array('k1'), 1, 0);
var_dump($retval);
$retval = $hs->executeMulti(
array(array(1, '=', array('k1'), 1, 0),
array(1, '=', array('k2'), 1, 0)));
var_dump($retval);
unset($hs);
//UPDATE
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(2, $dbname, $table, '', 'v')))
{
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeUpdate(2, '=', array('k1'), array('V1'), 1, 0) === false)
{
echo $hs->getError(), PHP_EOL;
die();
}
unset($hs);
//INSERT
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(3, $dbname, $table, '', 'k,v')))
{
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeInsert(3, array('k2', 'v2')) === false)
{
echo $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array('k3', 'v3')) === false)
{
echo 'A', $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array('k4', 'v4')) === false)
{
echo 'B', $hs->getError(), PHP_EOL;
}
unset($hs);
//DELETE
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(4, $dbname, $table, '', '')))
{
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeDelete(4, '=', array('k2')) === false)
{
echo $hs->getError(), PHP_EOL;
die();
}至此客户端也部署完毕了,性能和具体应用有待测试!