与MongoDB媲美:Mysql插件HandlerSocket的应用

    技术2022-05-20  60

    一.安装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-plugindir MYSQL安装后PLUGIN的目录

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

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

    checking mysql source... yes: Using /usr/local/src/mysql-5.1.47, version 5.1.47checking for mysql_config... /usr/bin/mysql_configchecking mysql binary... yes: Using /usr/bin/mysql_config, version 5.0.77configure: 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 -lnActive 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);

    ?>

     


    最新回复(0)