为什么80%的码农都做不了架构师?>>>
# wgethttps://download.github.com/ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-71-g159ea6d.tar.gz
# tar zxvf ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-71-g159ea6d.tar.gz -C ../software/
# cd ../software/ahiguti-HandlerSocket-Plugin-for-MySQL-159ea6d/
# ./autogen.sh
# ./configure --prefix=/usr/local/handlersocket
--with-mysql-source=/usr/local/src/software/mysql-5.1.36//mysql源码目录
--with-mysql-bindir=/usr/local/mysql-5.1.36/bin //mysql安装目录可执行目录
--with-mysql-plugindir=/usr/local/mysql-5.1.36/lib/mysql/plugin //mysql插件目录
# make
# make install
2.修改配置文件 在my.cnf文件[mysqld]下增加以下配置:
loose_handlersocket_port = 9998//指定读请求端口号
loose_handlersocket_port_wr = 9999 //指定写请求端口号
loose_handlersocket_threads = 16 //指定读线程数目
loose_handlersocket_threads_wr = 1 //指定写线程数目
3.安装handlersocket.so:
> INSTALL PLUGIN handlersocket SONAME 'handlersocket.so';
4.查看进程状态
# netstat -ntpl
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31402/mysqld
tcp 0 0 0.0.0.0:9998 0.0.0.0:* LISTEN 31402/mysqld
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 31402/mysqld
5.常见问题 configure: error: MySQL source version does not match MySQL binary version 解决方法:在configure.in文件中首行添加一行[MySQL Server] 5.1.36。 五.HandlerSocket性能测试: 先来看看软件作者的测试结果: 环境:2.53GHZ,8核CPU,32G内存的Nehalem服务器 结果:
六.安装PHP扩展包php-handlersocket
# wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.0.7.tar.gz
# tar zxvf php-handlersocket-0.0.7.tar.gz -C ../software/
# /usr/local/php5.2.13/bin/phpize
# ./configure --prefix=/usr/local/php-handlersocket \
--with-php-config=/usr/local/php5.2.13/bin/php-config \
--with-handlersocket \
--with-handlersocket-includedir=/usr/local/handlersocket/include/handlersocket
# make
# make install
# vim /usr/local/Zend/etc/php.ini
添加下面一行
extension=/usr/local/php5.2.13/lib/php/extensions/no-debug-zts-20060613/handlersocket.so
查看是否加载成功: 常见问题: cc -shared.libs/handlersocket.o-lstdc++ -L/usr/local/handlersocket/include/handlersocket/lib -lhsclient-Wl,-rpath -Wl,/usr/local/handlersocket/include/handlersocket/lib -Wl,-soname -Wl,handlersocket.so -o .libs/handlersocket.so /usr/bin/ld: cannot find -lhsclient collect2: ld returned 1 exit status make: *** [handlersocket.la] Error 1 解决方法:# cd /usr/local/handlersocket
# cp -rf lib include/handlersocket/
七.测试: 1.建立测试表
mysql>create table `ttlsa_com` (
-> `id` int(10) auto_increment not null,
-> `k` varchar(50) default null,
-> `v` varchar(50) default null,
-> primary key (`id`),
-> key `index_k` (`k`)
-> )engine=innodb;
mysql> insert into ttlsa_com values ('','k1','v1');
mysql> insert into ttlsa_com values ('','k2','v2');
mysql> insert into ttlsa_com values ('','k3','v3');
2.PHP使用实例
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)))
{
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'))))
{
echo $hs->getError(), PHP_EOL;
}
if (!($hs->executeInsert(3, array('k3', 'v3'))))
{
echo $hs->getError(), PHP_EOL;
}
if (!($hs->executeInsert(3, array('k4', 'v4'))))
{
echo $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'))))
{
echo $hs->getError(), PHP_EOL;
die();
}
3.方法注释 openIndex(1, $dbname, $table, HandlerSocket::PRIMARY, 'k,v') 打开索引,第一个参数1用来在每个HandlerSocket对象中唯一标识一个表名;$dbname表示为数据库名;$table表示为表名;HandlerSocket::PRIMARY表示为索引名;'k,v'表示为要查询的列名。 executeSingle(1, '=', array('k1'), 1, 0) 执行查询,第一个参数要跟之前的openIndex方法中的第一个参数保持一致;第二个参数’=’表示检索的条件,支持’=’,’>=’,’<=’,’>’,’<’;第三个参数array(‘k1’)表示一个arrayref,指定检索的key,其长度必须小于或者等于对应索引的列数;第四个和第五个参数指定查询的limit和offset。 executeMulti 可在一次调用中执行多个操作。 如需转载请注明出处: http://www.ttlsa.com/html/3022.html