Mysql插件HandlerSocket的应用

一.安装HandlerSocket

1.下载

http://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL

获取 ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-71-g159ea6d.tar.gz

上传到LINUX目录/usr/local/src/ 下

2.安装

cd /usr/local/src/

tar zvfx ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-71-g159ea6d.tar.gz

cd ahiguti-HandlerSocket-Plugin-for-MySQL-159ea6d/

./autogen.sh

./configure --with-mysql-source=/usr/local/src/mysql-5.1.47 --with-mysql-binddir=/usr/local/app/mysql/bin/ --with-mysql-plugindir=/usr/local/app/mysql/lib/mysql/plugin/ --prefix=/usr/local/app/mysql

其中

--with-mysql-source MYSQL源码目录

--with-mysql-binddir MYSQL安装后的BIN目录

--with-mysql-plugindirMYSQL安装后PLUGIN的目录

(***************小插曲************************

笔者在configure的时候遇到的问题:

checking mysql source... yes: Using /usr/local/src/mysql-5.1.47, version 5.1.47
checking for mysql_config... /usr/bin/mysql_config
checking mysql binary... yes: Using /usr/bin/mysql_config, version 5.0.77
configure: error: MySQL source version does not match MySQL binary version

是默认的/usr/bin/mysql_config与mysql-5.1.47源码版本不一样,所以做一下补充:

cp /usr/local/app/mysql/bin/mysql_config /usr/bin/mysql_config

************************************************************)

编译安装:

make;make install

添加配置

vi /etc/my.cnf

增加如下选项:

[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.

然后使用root登录MYSQL

mysql -u root -h localhost -p

在MYSQL控制台中执行命令

mysql> install plugin handlersocket soname 'handlersocket.so';

如果执行成功,可以看到9998 9999端口已经启动

# netstat -ln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:997 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9998 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
udp 0 0 0.0.0.0:50187 0.0.0.0:*
udp 0 0 0.0.0.0:991 0.0.0.0:*
udp 0 0 0.0.0.0:994 0.0.0.0:*
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 :::43696 :::*
udp 0 0 :::5353 :::*

安装完毕。不需要修改mysql的源代码。
mysql需要5.1或者以后版本。

二、安装PHP的handlerSocket扩展

1.下载源码

http://code.google.com/p/php-handlersocket/downloads/detail?name=php-handlersocket-0.0.7.tar.gz

2.安装

cd /usr/local/src/

tar zvfx php-handlersocket-0.0.7.tar.gz

cd php-handlersocket

/usr/local/bin/phpize
./configure --with-handlersocket --with-php-config=/usr/local/bin/php-config --with-handlersocket-includedir=/usr/local/app/mysql/include/handlersocket/

make LDFLAGS='-L/usr/local/app/mysql/lib' ;

make install

三、使用PHP handlersocket的API

实例化:
  /*
  * 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 = 'discuz';

$table = 'cdb_members';

$hs = new HandlerSocket($host, $port);
if (!($hs->openIndex(0, $dbname, $table, "username", 'uid,username,password,gender'))) { echo $hs->getError(), PHP_EOL; die(); }

$retval = $hs->executeSingle(0, '=', array('吉祥二宝'), 10, 0); var_dump($retval);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值