HandlerSocket 是MySQL的一个插件,用于跳过MySQL的SQL层面,直接访问内部的InnoDB存储引擎,下面我们来看看MySQL HandlerSocket 插件安装配置详解.
系统信息约定:
系统版本:CentOS 6.3 X86
php安装目录:/usr/local/webserver/php5318
MySQL安装目录:/usr/local/webserver/mysql5520
HandlerSocket安装路径:/usr/local/webserver/handlersocket
安装配置HandlerSocket
安装之前建议你先安装相关支持及需要的组件包:yum install gcc gcc-c++ libtool make openssl-devel perl-DBI perl-DBD-MySQL(如果为64bit OS注意perl-DBD-MySQL.x86_64).
yum install rpm-build gperf readline-devel ncurses-devel time perl-Time-HiRes
安装:如果使用Percona Server版本的MySQL就简单了,因为它已经内置了HandlerSocket支持,不过考虑到其内置的版本不够新,存在一些早已修复的BUG,所以最好采用源代码编译,HandlerSocket是基于MySQL数据库的,因此在安装HanderSocket前需要先按照常规方式部署MySQL服务,同时需注意HandlerSocket时需要MySQL的源码,因此还需要MySQL源码编译方式安装.
- [root@iredmail opt]# git clone https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL.git --Vevb.com
- [root@iredmail opt]# cd HandlerSocket-Plugin-for-MySQL
- [root@iredmail HandlerSocket-Plugin-for-MySQL]# ./autogen.sh
- [root@iredmail HandlerSocket-Plugin-for-MySQL]#./configure --prefix=/usr/local/webserver/handlersocket --with-mysql-source=/opt/mysql-5.5.20 --with-mysql-bindir=/usr/local/webserver/mysql5520/bin --with-mysql-plugindir=/usr/local/webserver/mysql5520/lib/mysql/plugin
Tips:
--with-mysql-source 指定MySQL源码所在目录
--with-mysql-bindir 表示MySQL二进制可执行文件目录
--with-mysql-plugindir 指定MySQL插件的存储路径,如果不清楚这个目录在哪,可以按如下方法查询:
- mysql> show variables like 'plugin%';
- +---------------+-------------------------------------------+
- | Variable_name | Value |
- +---------------+-------------------------------------------+
- | plugin_dir | /usr/local/webserver/mysql5520/lib/plugin |
- +---------------+-------------------------------------------+
- 1 row in set (0.00 sec) --Vevb.com
- [root@iredmail HandlerSocket-Plugin-for-MySQL]# make
常见错误:
- libtool: link: only absolute run-paths are allowed
- make[2]: *** [handlersocket.la] Error 1
- make[2]: Leaving directory `/opt/HandlerSocket-Plugin-for-MySQL/handlersocket'
- make[1]: *** [all-recursive] Error 1
- make[1]: Leaving directory `/opt/HandlerSocket-Plugin-for-MySQL'
- make: *** [all] Error 2
解决方法:
- [root@iredmail HandlerSocket-Plugin-for-MySQL]# vi handlersocket/Makefile
- line 301:
- $(handlersocket_la_LINK) -rpath $(pkgplugindir) $(handlersocket_la_OBJECTS) $(handlersocket_la_LIBADD) $(LIBS)
- -->
- $(handlersocket_la_LINK) -rpath /opt/HandlerSocket-Plugin-for-MySQL/handlersocket $( handlersocket_la_OBJECTS) $(handlersocket_la_LIBADD) $(LIBS)
- [root@iredmail HandlerSocket-Plugin-for-MySQL]#make install
完成后,mysql-plugindir目录下应有handlersocket相关文件.
配置MySQL,修改my.cnf配置文件:
- [root@iredmail HandlerSocket-Plugin-for-MySQL]# vi /etc/my.cnf
- [mysqld]
- plugin-load=handlersocket.so(plugin-load可略过不配)
- 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 concurren connections, make open_files_limit as large as possible.
Tips:InnoDB的innodb_buffer_pool_size,或MyISAM的key_buffy_size等关系到缓存索引的选项尽可能设置大一些,这样才能发挥HandlerSocket的潜力.
登陆MySQL并激活HandlerSocket插件:
- [root@iredmail HandlerSocket-Plugin-for-MySQL]# mysql -uroot -p
- mysql> install plugin handlersocket soname 'handlersocket.so';
- ERROR 1126 (HY000): Can't open shared library '/usr/local/webserver/mysql5520/lib/plugin/handlersocket.so' (errno: 2 cannot open shared object file: No such file or directory)
说明:这里提示没有找到handlersocket.so扩展文件,请查看扩展文件是否存在.
- mysql> install plugin handlersocket soname 'handlersocket.so';
- Query OK, 0 rows affected (0.00 sec)
- mysql> quit;
至此,HandlerSocket插件安装完毕.
重启mysql服务:
[root@iredmail HandlerSocket-Plugin-for-MySQL]# service mysqld restart
HandlerSocket状态测试
也可以通过查询刚配置的端口是否已经被MySQL占用来确认是否安装成功:
- [root@iredmail HandlerSocket-Plugin-for-MySQL]# lsof -i -P | grep mysqld --Vevb.com
- mysqld 26871 mysql 11u IPv4 72467 0t0 TCP *:9998 (LISTEN)
- mysqld 26871 mysql 29u IPv4 72469 0t0 TCP *:9999 (LISTEN)
- mysqld 26871 mysql 31u IPv4 72474 0t0 TCP *:3306 (LISTEN)
- Tips:If ports 9998 and 9999 don’t show up. Make sure SELinux is not running.
安装配置 php-handlersocket 扩展模块,安装php-handlersocket扩展:
- [root@iredmail opt]# wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.3.1.tar.gz
- [root@iredmail opt]# tar -zxvf php-handlersocket-0.3.1.tar.gz
- [root@iredmail opt]# cd handlersocket/
- [root@iredmail handlersocket]# /usr/local/webserver/php5318/bin/phpize
- [root@iredmail handlersocket]# ./configure --with-php-config=/usr/local/webserver/php5318/bin/php-config
- ./configure可加参数:
- implemented configure options source file
- hsclient none (default) handlersocket.cc
- native --disable-handlersocket-hsclient handlersocet.c
- Tips:If you get an error:
- configure: error: Can't find hsclient headers,please install libhsclient first,Or ./configure--disable-handlersocket-hsclient --with-php-config=/usr/local/webserver/php5318/bin/php-config use native type.
- [root@iredmail handlersocket]#make && make install
- A successful install will have created handlersocket.so and put it into the PHP extensions directory. You'll need to and adjust php.ini and add an extension=handlersocket.so line before you can use the extension.
- [root@iredmail handlersocket]# vi /usr/local/webserver/php5318/etc/php.ini
- extension=handlersocket.so
至此php扩展安装完成,放问php.info页面,我们可以看到已经成功加载了handlersocket扩展.
php-handlersocket 使用示例
- <?php
- /*
- * 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);
- Example:
- 测试库 hstestdb,测试表hstesttbl:
- CREATE TABLE `hstesttbl` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `k` char(6) DEFAULT NULL,
- `v` char(6) DEFAULT NULL,
- PRIMARY KEY (`id`),
- KEY `idx_hstesttbl_k` (`k`)
- ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
- PHP test Code:
- $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();
- }
- ?>
Tips:理论上HandlerSocket支持MyISAM,InnoDB等各种引擎,不过推荐使用InnoDB。
Tips:To avoid the insert error,Please remember set storage engine:InnoDB.
Tips:对HandlerSocket一个常见的误解是只能执行PRIMARY类型的KV查询,实际上只要支持索引,一般的简单查询它都能胜任,这里就不多说了,官方文档里有介绍.
HandlerSocket的缺陷:
1)写操作并没有淘汰查询缓存——如果执行了写操作通过HandlerSocket,由于没有失效查询缓存,那么你可能从MySQL读到旧的数据.
2)不支持自动递增——插入时无法从自增列上自动获得增量值.
鉴于以上问题,扬长避短,使用其合并查询操作,发挥其NoSQL性能获取MySQL的InnoDB类型表数据.
写在最后的:MySQL5.6提供原生的Memcached API,实际就是KV型NoSQL了,但HandlerSocket并不局限于KV形式,所以仍然有生存空间.
新闻热点
疑难解答