MySQL的NoSQL插件HandlerSocket
FreeBSD安装
环境:FreeBSD 7.0-RELEASE + MySQL-5.1.33
编译步骤
cd ahiguti-HandlerSocket-Plugin-for-MySQL-635b930
./autogen.sh
./configure --with-mysql-source=/site/soft/mysql-5.1.33 --with-mysql-bindir=/data2/mysql5.1/bin --with-mysql-plugindir=/data2/mysql5.1/lib/mysql/plugin
make && make install
./autogen.sh
./configure --with-mysql-source=/site/soft/mysql-5.1.33 --with-mysql-bindir=/data2/mysql5.1/bin --with-mysql-plugindir=/data2/mysql5.1/lib/mysql/plugin
make && make install
注:--with-mysql-plugindir选项指定的插件库程序安装目录必须是mysql标准插件安装目录。
安装过程中遇到了2个问题:
1. 执行configure脚本的时候报错:
configure: error: MySQL source version does not match MySQL binary version
错误产生原因是:
configure 脚本中获取 MYSQL_SOURCE_VERSION 变量值的方式不完善,并不能根据 --with-mysql-source 选项指定源代码目录准确分析出版本号,获取代码片段为:
if test -f "$ac_mysql_source_dir/configure.in"; then
MYSQL_SOURCE_VERSION=`cat $ac_mysql_source_dir/configure.in | grep "\[MySQL Server\]" | sed -e "s|.*\([0-9]\+\.[0-9]\+\.[0-9]\+[0-9a-zA-Z\_\-]*\).*|\1|"`
else
{ { echo "$as_me:$LINENO: error: invalid MySQL source directory: $ac_mysql_source_dir" >&5
echo "$as_me: error: invalid MySQL source directory: $ac_mysql_source_dir" >&2;}
{ (exit 1); exit 1; }; }
fi
MYSQL_SOURCE_VERSION=`cat $ac_mysql_source_dir/configure.in | grep "\[MySQL Server\]" | sed -e "s|.*\([0-9]\+\.[0-9]\+\.[0-9]\+[0-9a-zA-Z\_\-]*\).*|\1|"`
else
{ { echo "$as_me:$LINENO: error: invalid MySQL source directory: $ac_mysql_source_dir" >&5
echo "$as_me: error: invalid MySQL source directory: $ac_mysql_source_dir" >&2;}
{ (exit 1); exit 1; }; }
fi
解决办法:
修改 configure 脚本,将检查源代码与二进制版本号是否一致的判断逻辑注释掉。
# if test a$MYSQL_SOURCE_VERSION != a$MYSQL_BIN_VERSION ; then
# { { echo "$as_me:$LINENO: error: MySQL source version does not match MySQL binary version" >&5
echo "$as_me: error: MySQL source version does not match MySQL binary version" >&2;}
# { (exit 1); exit 1; }; }
# fi
# { { echo "$as_me:$LINENO: error: MySQL source version does not match MySQL binary version" >&5
echo "$as_me: error: MySQL source version does not match MySQL binary version" >&2;}
# { (exit 1); exit 1; }; }
# fi
2. 执行make时报错:
libtool: link: only absolute run-paths are allowed
这个问题估计是 HandlerSocket 安装包自带的 libtool 有问题,复制系统的将其替换掉就好了。
插件安装
参考官方安装手册
安装PHP扩展
/data/php2/bin/phpize
./configure --with-php-config=/data/php2/bin/php-config
make
make install
./configure --with-php-config=/data/php2/bin/php-config
make
make install
在 php.ini 中添加扩展配置 extension=handlersocket.so
make的时候发生无法找到 hsclient 库程序的错误:
/usr/bin/ld: cannot find -lhsclient
解决办法:修改 Makefile 文件中的 HANDLERSOCKET_SHARED_LIBADD 变量值为:
HANDLERSOCKET_SHARED_LIBADD = -lstdc++ -R/usr/local/lib -L/usr/local/lib -lhsclient
PHP HandleSocket程序示例
<?php
$host = '192.168.0.x';
$port = 9998;
$port_wr = 9999;
$dbname = 'uni_fmworlds';
$table = 'user_equipment';
//GET 返回单条结果
$hs = new HandlerSocket($host, $port);
if (!($hs->openIndex(0, $dbname, $table, HandlerSocket::PRIMARY, 'id,uid,pid,type,equipment_type'))) {
echo $hs->getError(), PHP_EOL;
die();
}
$retval = $hs->executeSingle(0, '=', array(10000), 1, 0);
print_r($retval);
//GET 返回多条结果
if (!($hs->openIndex(1, $dbname, $table, 'uid_pid', 'id,uid,pid,type,equipment_type'))) {
echo $hs->getError(), PHP_EOL;
die();
}
$retval = $hs->executeSingle(1, '=', array(10459), 3, 0);
print_r($retval);
$retval = $hs->executeMulti(
array( array(1, '=', array(10007), 1, 0),
array(1, '=', array(10459), 1, 0))
);
print_r($retval);
unset($hs);
//UPDATE
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(2, $dbname, $table, HandlerSocket::PRIMARY, 'ctime'))) {
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeUpdate(2, '=', array(10000), array(date("Y-m-d H:i:s")), 1, 0) === false) {
echo $hs->getError(), PHP_EOL;
die();
}
unset($hs);
//INSERT
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(3, $dbname, $table, '', 'uid,pid,type'))) {
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeInsert(3, array(10001, 5, 'card')) === false) {
echo $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array(10001, 6, 'card')) === false) {
echo 'A', $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array(10001, 7, 'card')) === false) {
echo 'B', $hs->getError(), PHP_EOL;
}
unset($hs);
//DELETE
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(4, $dbname, $table, HandlerSocket::PRIMARY, 'id'))) {
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeDelete(4, '=', array(63679)) === false) {
echo $hs->getError(), PHP_EOL;
die();
}
?>
$host = '192.168.0.x';
$port = 9998;
$port_wr = 9999;
$dbname = 'uni_fmworlds';
$table = 'user_equipment';
//GET 返回单条结果
$hs = new HandlerSocket($host, $port);
if (!($hs->openIndex(0, $dbname, $table, HandlerSocket::PRIMARY, 'id,uid,pid,type,equipment_type'))) {
echo $hs->getError(), PHP_EOL;
die();
}
$retval = $hs->executeSingle(0, '=', array(10000), 1, 0);
print_r($retval);
//GET 返回多条结果
if (!($hs->openIndex(1, $dbname, $table, 'uid_pid', 'id,uid,pid,type,equipment_type'))) {
echo $hs->getError(), PHP_EOL;
die();
}
$retval = $hs->executeSingle(1, '=', array(10459), 3, 0);
print_r($retval);
$retval = $hs->executeMulti(
array( array(1, '=', array(10007), 1, 0),
array(1, '=', array(10459), 1, 0))
);
print_r($retval);
unset($hs);
//UPDATE
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(2, $dbname, $table, HandlerSocket::PRIMARY, 'ctime'))) {
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeUpdate(2, '=', array(10000), array(date("Y-m-d H:i:s")), 1, 0) === false) {
echo $hs->getError(), PHP_EOL;
die();
}
unset($hs);
//INSERT
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(3, $dbname, $table, '', 'uid,pid,type'))) {
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeInsert(3, array(10001, 5, 'card')) === false) {
echo $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array(10001, 6, 'card')) === false) {
echo 'A', $hs->getError(), PHP_EOL;
}
if ($hs->executeInsert(3, array(10001, 7, 'card')) === false) {
echo 'B', $hs->getError(), PHP_EOL;
}
unset($hs);
//DELETE
$hs = new HandlerSocket($host, $port_wr);
if (!($hs->openIndex(4, $dbname, $table, HandlerSocket::PRIMARY, 'id'))) {
echo $hs->getError(), PHP_EOL;
die();
}
if ($hs->executeDelete(4, '=', array(63679)) === false) {
echo $hs->getError(), PHP_EOL;
die();
}
?>
延伸阅读:
1. HandlerSocket-Plugin-for-MySQL
2. [人自明] Mysql的HandlerSocket插件
3. HandlerSocket系列(三):性能及其性能优化
4. MySQL HandlerSocket in Action