原文:http://yoshinorimatsunobu.blogspot.com/2010/10/using-mysql-as-nosql-story-for.html
相关的日文PPT:http://www.slideshare.net/akirahiguchi/handlersocket-plugin-for-mysql-4664154
相关新能测试请参考:http://www.cnblogs.com/inrie/archive/2011/02/22/1961415.html
下载地址:https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL
安装方式:
下载 ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-73-g0e63366.tar.gz https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL/tarball/master
[root@niutian365 tmp]# tar -zxvf ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-73-g0e63366.tar.gz
[root@niutian365 tmp]# cd ahiguti-HandlerSocket-Plugin-for-MySQL-0e63366
[root@niutian365 tmp]# ./autogen.sh
##这里 可能会遇到libtool 未安装 yum install libtool 安装下
[root@niutian365 tmp]# ./configure --with-mysql-source=/niutian365/softs/mysql-5.1.54 --with-mysql-bindir=/usr/local/mysql/bin --with-mysql-plugindir=/usr/local/mysql/lib/mysql/plugin
##注意这里我的mysql版本是mysql-5.1.54 已经安装过 不详述mysql安装 源码目录是/niutian365/softs/mysql-5.1.54,这个地方我经常搞错,配置到mysql安装目录下.
[root@niutian365 tmp]# make
[root@niutian365 tmp]# make install
[root@niutian365 tmp]# vi /etc/my.cnf
[mysqld]
......
#下面添加
loose_handlersocket_port = 9998
loose_handlersocket_port_wr = 9999
loose_handlersocket_threads = 16
loose_handlersocket_threads_wr = 1
open_files_limit = 65535
保存退出
重启mysql
[root@niutian365 tmp]# mysql restart
登录mysql
[root@niutian365 tmp]# mysql -uroot -pniutian
mysql> install plugin handlersocket soname 'handlersocket.so';
mysql> show processlist;
mysql> show plugins;
[root@niutian365 tmp]# mysql restart
安装说明请参考 ahiguti-HandlerSocket-Plugin-for-MySQL-0e63366/docs-ja目录下面的说明文件
安装php模块HandlerSocket文档中提供了三种方式安装:
http://openpear.org/package/Net_HandlerSocket
http://github.com/tz-lom/HSPHP
http://code.google.com/p/php-handlersocket/
这里php模块支持选择http://code.google.com/p/php-handlersocket/
[root@niutian365 tmp]# wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.0.7.tar.gz
[root@niutian365 tmp]# tar -zxvf php-handlersocket-0.0.7.tar.gz
[root@niutian365 tmp]# cd php-handlersocket
[root@niutian365 tmp]# /usr/local/php/bin/phpize
[root@niutian365 tmp]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@niutian365 tmp]# make
[root@niutian365 tmp]# make install
出现
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
Installing header files: /usr/local/php/include/php/
[root@niutian365 tmp]# vi /usr/local/php/etc/php.ini
添加extension=handlersocket.so
保存退出
重启apache或者php-fpm
2011-06-07升级安装 0.2.0
个别包依赖包括re2c等
[root@niutian365 tmp]# yum -y install gcc gcc-c++ autoconf glibc glibc-devel glib2 glib2-devel
[root@niutian365 tmp]# wget http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el5.rf.i386.rpm
[root@niutian365 tmp]# rpm -ivh re2c-0.13.5-1.el5.rf.i386.rpm
[root@niutian365 tmp]# wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.2.0.tar.gz
安装方法同上
使用PHP handlersocket的API
/* Constants */
const HandlerSocket : :PRIMARY ;
/* Methods */
__construct ( string $host , string $port [ , array $options ] )
public bool openIndex ( int $id , string $db , string $table , string $index , string $fields )
public mixed executeSingle ( int $id , string $op , array $fields [ , int $limit , int $skip , strint $modop , array $values , array $filters , int $invalues_key , array $invalues ] )
public mixed executeMulti ( array $requests )
public mixed executeUpdate ( int $id , string $op , array $fields , array $values [ , int $limit , int $skip , array $filters , int $invalues_key , array $invalues ] ] )
public mixed executeDelete ( int $id , string $op , array $fields [ , int $limit , int $skip , array $filters , int $invalues_key , array $invalues ] ] )
public mixed executeInsert ( int $id , array $values )
public string getError ( void )
}
中文说明:
/*
* String $host:MySQL ip;
* String $port:handlersocket插件的监听端口,它有两个端口可选:一个用于读、一个用于写
*/
$hs = new HandlerSocket ($host , $port ) ;
打开一个数据表:
/*
* Int $index:这个数字相当于文件操作里的句柄,HandlerSocket的所有其他方法都会依据这个数字来操作由这个 openIndex打开的表,
* String $dbname:库名
* String $table:表名
* String $key:表的“主键”(HandlerSocket::PRIMARY)或“索引名”作为搜索关键字段,这就是说表必须有主键或索引
* 个人理解:要被当做where条件的key字段,这样可以认为handlersocket只有一个where条件
* String $column:'column1,column2' 所打开表的字段(以逗号隔开),就是说$table表的其他字段不会被操作
*/
$hs - >openIndex ($index , $dbname , $table , $key , $column ) ;
查询:
/*
* Int $index: openIndex()所用的$index
* String $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '<=', '>',and '<';可以理解为where条件
* Array $value
* Int $number(默认是1):获取结果的最大条数;相当于SQL中limit的第二个参数
* Int $skip(默认是0):跳过去几条;相当于SQL中limit的第一个参数
*/
$retval = $hs - >executeSingle ($index , $operation , $value , $number , $skip ) ;
插入(注意:此处的openIndex要用$port_wr,即读写端口):
/*
* Int $index: openIndex()所用的$index
* Array $arr:数字元素数与openIndex的$column相同
*/
$retval = $hs - >executeInsert ($index , $arr ) ;
删除(注意:此处的openIndex要用$port_wr,即读写端口):
/*
* Int $index: openIndex()所用的$index
* String $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '<=', '>',and '<';可以理解为where条件
* Array $value
* Int $number(默认是1):获取结果的最大条数;相当于SQL中limit的第二个参数
* Int $skip(默认是0):跳过去几条;相当于SQL中limit的第一个参数
*/
$retval = $hs - >executeDelete ($index , $operation , $value , $number , $skip ) ;
更新(注意:此处的openIndex要用$port_wr,即读写端口):
/*
* Int $index: openIndex()所用的$index
* String $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '<=', '>',and '<';可以理解为where条件
* Array $value
* Int $number(默认是1):获取结果的最大条数;相当于SQL中limit的第二个参数
* Int $skip(默认是0):跳过去几条;相当于SQL中limit的第一个参数
*/
$retval = $hs - >executeUpdate ($index , $operation , $value , $number , $skip ) ;
实例:
< ?php
$host = 'localhost' ;
$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 ( ) ;
}