国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

MySQL HandlerSocket 插件安裝配置細(xì)解

2024-07-24 12:35:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  HandlerSocket 是MySQL的一個(gè)插件,用于跳過(guò)MySQL的SQL層面,直接訪(fǎng)問(wèn)內(nèi)部的InnoDB存儲(chǔ)引擎,下面我們來(lái)看看MySQL HandlerSocket 插件安裝配置詳解.
 
  系統(tǒng)信息約定:
 
  系統(tǒng)版本:CentOS 6.3 X86
 
  php安裝目錄:/usr/local/webserver/php5318
 
  MySQL安裝目錄:/usr/local/webserver/mysql5520
 
  HandlerSocket安裝路徑:/usr/local/webserver/handlersocket
 
  安裝配置HandlerSocket
 
  安裝之前建議你先安裝相關(guān)支持及需要的組件包: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就簡(jiǎn)單了,因?yàn)樗呀?jīng)內(nèi)置了HandlerSocket支持,不過(guò)考慮到其內(nèi)置的版本不夠新,存在一些早已修復(fù)的BUG,所以最好采用源代碼編譯,HandlerSocket是基于MySQL數(shù)據(jù)庫(kù)的,因此在安裝HanderSocket前需要先按照常規(guī)方式部署MySQL服務(wù),同時(shí)需注意HandlerSocket時(shí)需要MySQL的源碼,因此還需要MySQL源碼編譯方式安裝.
 
  [root@iredmail opt]# git clone https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL.git  --phpfensi.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二進(jìn)制可執(zhí)行文件目錄
 
  --with-mysql-plugindir 指定MySQL插件的存儲(chǔ)路徑,如果不清楚這個(gè)目錄在哪,可以按如下方法查詢(xún):
 
  mysql> show variables like 'plugin%';
  +---------------+-------------------------------------------+
  | Variable_name | Value                                     |
  +---------------+-------------------------------------------+
  | plugin_dir    | /usr/local/webserver/mysql5520/lib/plugin |
  +---------------+-------------------------------------------+
  1 row in set (0.00 sec)  --phpfensi.com
  [root@iredmail HandlerSocket-Plugin-for-MySQL]# make
  常見(jiàn)錯(cuò)誤:
 
  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目錄下應(yīng)有handlersocket相關(guān)文件.
 
  配置MySQL,修改my.cnf配置文件:
 
  [root@iredmail HandlerSocket-Plugin-for-MySQL]# vi /etc/my.cnf
  [mysqld]
  plugin-load=handlersocket.so(plugin-load可略過(guò)不配)
  loose_handlersocket_port = 9998 # 指定讀請(qǐng)求端口號(hào)
  # the port number to bind to (for read requests)
  loose_handlersocket_port_wr = 9999 # 指定寫(xiě)請(qǐng)求端口號(hào)
  # the port number to bind to (for write requests)
  loose_handlersocket_threads = 16 # 指定讀線(xiàn)程數(shù)目
  # the number of worker threads (for read requests)
  loose_handlersocket_threads_wr = 1 # 指定寫(xiě)線(xiàn)程數(shù)目
  # 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等關(guān)系到緩存索引的選項(xiàng)盡可能設(shè)置大一些,這樣才能發(fā)揮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)
  說(shuō)明:這里提示沒(méi)有找到handlersocket.so擴(kuò)展文件,請(qǐng)查看擴(kuò)展文件是否存在.
 
  mysql> install plugin handlersocket soname 'handlersocket.so';
  Query OK, 0 rows affected (0.00 sec)
  mysql> quit;
  至此,HandlerSocket插件安裝完畢.
 
  重啟mysql服務(wù):
 
  [root@iredmail HandlerSocket-Plugin-for-MySQL]# service mysqld restart
 
  HandlerSocket狀態(tài)測(cè)試
 
  也可以通過(guò)查詢(xún)剛配置的端口是否已經(jīng)被MySQL占用來(lái)確認(rèn)是否安裝成功:
 
  [root@iredmail HandlerSocket-Plugin-for-MySQL]# lsof -i -P | grep mysqld  --phpfensi.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 擴(kuò)展模塊,安裝php-handlersocket擴(kuò)展:
 
  [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可加參數(shù):
  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擴(kuò)展安裝完成,放問(wèn)php.info頁(yè)面,我們可以看到已經(jīng)成功加載了handlersocket擴(kuò)展.
 
  php-handlersocket 使用示例
 
  <?php
  /*  
   * String  $host:MySQL ip;
   * String  $port:handlersocket插件的監(jiān)聽(tīng)端口,它有兩個(gè)端口可選:一個(gè)用于讀、一個(gè)用于寫(xiě)  
   */
  $hs = new HandlerSocket($host, $port);
  打開(kāi)一個(gè)數(shù)據(jù)表:
  /*
   * Int       $index:這個(gè)數(shù)字相當(dāng)于文件操作里的句柄,HandlerSocket的所有其他方法都會(huì)依據(jù)這個(gè)數(shù)字來(lái)操作由這個(gè)   openIndex打開(kāi)的表,
   * String  $dbname:庫(kù)名
   * String  $table:表名
   * String  $key:表的“主鍵”(HandlerSocket::PRIMARY)或“索引名”作為搜索關(guān)鍵字段,這就是說(shuō)表必須有主鍵或索引
   *                 個(gè)人理解:要被當(dāng)做where條件的key字段,這樣可以認(rèn)為handlersocket只有一個(gè)where條件
   * String  $column:'column1,column2' 所打開(kāi)表的字段(以逗號(hào)隔開(kāi)),就是說(shuō)$table表的其他字段不會(huì)被操作
   */
  $hs->openIndex($index, $dbname, $table, $key, $column);
  查詢(xún):
  /*
   * Int     $index: openIndex()所用的$index
   * String  $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '< =', '>',and '< ';可以理解為where條件
   * Array   $value
   * Int       $number(默認(rèn)是1):獲取結(jié)果的最大條數(shù);相當(dāng)于SQL中l(wèi)imit的第二個(gè)參數(shù)
   * Int     $skip(默認(rèn)是0):跳過(guò)去幾條;相當(dāng)于SQL中l(wèi)imit的第一個(gè)參數(shù)
   */
  $retval = $hs->executeSingle($index, $operation, $value, $number, $skip);
  插入(注意:此處的openIndex要用$port_wr,即讀寫(xiě)端口):
  /*
   * Int     $index: openIndex()所用的$index
   * Array   $arr:數(shù)字元素?cái)?shù)與openIndex的$column相同
   */
  $retval = $hs->executeInsert($index, $arr);
  刪除(注意:此處的openIndex要用$port_wr,即讀寫(xiě)端口):
  /*
   * Int     $index: openIndex()所用的$index
   * String  $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '< =', '>',and '< ';可以理解為where條件
   * Array   $value
   * Int     $number(默認(rèn)是1):獲取結(jié)果的最大條數(shù);相當(dāng)于SQL中l(wèi)imit的第二個(gè)參數(shù)
   * Int     $skip(默認(rèn)是0):跳過(guò)去幾條;相當(dāng)于SQL中l(wèi)imit的第一個(gè)參數(shù)
   */
  $retval = $hs->executeDelete($index, $operation, $value, $number, $skip);
  更新(注意:此處的openIndex要用$port_wr,即讀寫(xiě)端口):
  /*
   * Int     $index: openIndex()所用的$index
   * String  $operation:openIndex方法中指定的$key字段所用的操作符,目前支持'=', '>=', '< =', '>',and '< ';可以理解為where條件
   * Array   $value
   * Int       $number(默認(rèn)是1):獲取結(jié)果的最大條數(shù);相當(dāng)于SQL中l(wèi)imit的第二個(gè)參數(shù)
   * Int     $skip(默認(rèn)是0):跳過(guò)去幾條;相當(dāng)于SQL中l(wèi)imit的第一個(gè)參數(shù)
   */
  $retval = $hs->executeUpdate($index, $operation, $value, $number, $skip);
  Example:
  測(cè)試庫(kù) hstestdb,測(cè)試表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等各種引擎,不過(guò)推薦使用InnoDB。
 
  Tips:To avoid the insert error,Please remember set storage engine:InnoDB.
 
  Tips:對(duì)HandlerSocket一個(gè)常見(jiàn)的誤解是只能執(zhí)行PRIMARY類(lèi)型的KV查詢(xún),實(shí)際上只要支持索引,一般的簡(jiǎn)單查詢(xún)它都能勝任,這里就不多說(shuō)了,官方文檔里有介紹.
 
  HandlerSocket的缺陷:
 
  1)寫(xiě)操作并沒(méi)有淘汰查詢(xún)緩存——如果執(zhí)行了寫(xiě)操作通過(guò)HandlerSocket,由于沒(méi)有失效查詢(xún)緩存,那么你可能從MySQL讀到舊的數(shù)據(jù).
 
  2)不支持自動(dòng)遞增——插入時(shí)無(wú)法從自增列上自動(dòng)獲得增量值.
 
  鑒于以上問(wèn)題,揚(yáng)長(zhǎng)避短,使用其合并查詢(xún)操作,發(fā)揮其N(xiāo)oSQL性能獲取MySQL的InnoDB類(lèi)型表數(shù)據(jù).
 
  寫(xiě)在最后的:MySQL5.6提供原生的Memcached API,實(shí)際就是KV型NoSQL了,但HandlerSocket并不局限于KV形式,所以仍然有生存空間.
 
 

(編輯:武林網(wǎng))

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安康市| 松潘县| 宜都市| 达州市| 娄烦县| 驻马店市| 阳曲县| 怀柔区| 尼木县| 章丘市| 武冈市| 兴安盟| 德州市| 盘锦市| 朝阳市| 邹平县| 乐陵市| 安徽省| 文安县| 尼勒克县| 施秉县| 永春县| 治县。| 根河市| 东乡族自治县| 佛坪县| 武隆县| 达尔| 镇宁| 报价| 泰州市| 故城县| 鄂温| 衡山县| 南城县| 涿鹿县| 图们市| 平乐县| 静海县| 宁武县| 广水市|