HandlerSocket 是 MySQL 插件,繞過 SQL 層直接存取 InnoDB,提供 NoSQL 風格的高效能 Key-Value 存取。本文記錄 CentOS 6.3 + MySQL 5.5.20 環境下的完整編譯安裝、配置、PHP 擴展整合與實戰代碼範例。
/usr/local/webserver/php5318/usr/local/webserver/mysql5520(源碼編譯安裝)/usr/local/webserver/handlersocketyum install gcc gcc-c++ libtool make openssl-devel perl-DBI perl-DBD-MySQL
yum install rpm-build gperf readline-devel ncurses-devel time perl-Time-HiRes
git clone https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL.git
cd HandlerSocket-Plugin-for-MySQL
./autogen.sh
./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
make
libtool: link: only absolute run-paths are allowed
make[2]: *** [handlersocket.la] Error 1
修改 handlersocket/Makefile 第 301 行:
$(handlersocket_la_LINK) -rpath $(pkgplugindir) ...
→
$(handlersocket_la_LINK) -rpath /opt/HandlerSocket-Plugin-for-MySQL/handlersocket ...
再執行 make install。
# /etc/my.cnf
[mysqld]
plugin-load=handlersocket.so
loose_handlersocket_port = 9998 # 讀請求端口
loose_handlersocket_port_wr = 9999 # 寫請求端口
loose_handlersocket_threads = 16 # 讀執行緒數
loose_handlersocket_threads_wr = 1 # 寫執行緒數
open_files_limit = 65535 # 支援高併發連線
建議同步調大 innodb_buffer_pool_size 發揮最大效能。
mysql> INSTALL PLUGIN handlersocket SONAME 'handlersocket.so';
Query OK, 0 rows affected (0.00 sec)
# 驗證端口監聽
lsof -i -P | grep mysqld
# mysqld 26871 mysql 11u IPv4 72467 TCP *:9998 (LISTEN)
# mysqld 26871 mysql 29u IPv4 72469 TCP *:9999 (LISTEN)
wget https://php-handlersocket.googlecode.com/files/php-handlersocket-0.3.1.tar.gz
tar -zxvf php-handlersocket-0.3.1.tar.gz
cd handlersocket
/usr/local/webserver/php5318/bin/phpize
./configure --with-php-config=/usr/local/webserver/php5318/bin/php-config
# 若缺 hsclient headers:./configure --disable-handlersocket-hsclient ...
make && make install
編輯 php.ini 加入 extension=handlersocket.so,重啟 PHP-FPM。
$host = 'localhost';
$port = 9998; // 讀端口
$port_wr = 9999; // 寫端口
$dbname = 'hstestdb';
$table = 'hstesttbl';
$hs = new HandlerSocket($host, $port);
// 開啟索引(1=句柄,PRIMARY=主鍵,'k,v'=需讀取欄位)
$hs->openIndex(1, $dbname, $table, HandlerSocket::PRIMARY, 'k,v');
// 單條查詢:WHERE k='k1' LIMIT 1
$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);
// 寫操作需用寫端口
$hs_w = new HandlerSocket($host, $port_wr);
$hs_w->openIndex(2, $dbname, $table, '', 'v'); // 空 key 表示主鍵
$hs_w->executeUpdate(2, '=', array('k1'), array('V1'), 1, 0);
$hs_w->executeInsert(3, array('k2', 'v2'));
$hs_w->executeDelete(4, '=', array('k2'), 1, 0);
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 DEFAULT CHARSET=utf8;
AUTO_INCREMENT 自動取得新增 IDMySQL 5.6+ 內建 Memcached API(InnoDB Memcached Plugin),提供原生 KV 介面。但 HandlerSocket 支援非主鍵索引查詢,仍有生存空間。Percona Server 內建 HandlerSocket,生產環境建議直接使用 Percona。