Install and configure the MySQLHandlerSocket plug-in
I. What is HandlerSocket?
HandlerSocket is a MySQL plug-in written by akira higuchi. Provides NoSQL-like network services in the form of MySQL Daemon Plugin. with this plug-in, you can directly interact with the MySQL backend storage engine in key-value format, saves CPU overhead such as SQL interpretation at the upper layer of MySQL, opening and closing tables, and creating query plans.
Currently, most websites using MySQL use Memcache as the key-value cache at the same time. Although such an architecture is extremely popular and there are many successful cases, it relies too much on Memcache to virtually make Memcache the root cause of failure:
Memcache data consistency problem: when MySQL data changes, if the expired data cannot be cleared in a timely and effective manner, it will cause data inconsistency. This is not desirable in the instant Web era.
The avalanche effect after the Memcache crash: once the Memcache as a cache crashes, MySQL may suffer high load and crash in a short time.
Note: to clear expired data, you can find a solution in the program architecture. if there is a uniform DAO encapsulation for data operations, you can use the Observer mode to clear expired data, non-subject content, information self-check.
In the face of the above problems, the HandlerSocket project is a good solution. It provides MySQL with a complete NoSQL function through plug-ins. in principle, it skips MySQL's most time-consuming syntax parsing, query plan and other steps to directly read data. if the memory is large enough, the query efficiency of MySQL can be improved several times if indexes can be loaded!
HandlerSocket: https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL
Php-handlersocket: http://code.google.com/p/php-handlersocket/
System Information conventions:
System version: CentOS 6.3X86
PHP installation directory:/usr/local/webserver/php5318
MySQL installation directory:/usr/local/webserver/mysql5520
HandlerSocket installation path:/usr/local/webserver/handlersocket
II. install and configure HandlerSocket
Before installation, we recommend that you install the related support and required package:
Yum install gcc-c ++ libtool make openssl-devel perl-DBI perl-DBD-MySQL
Yum install rpm-build gperf readline-devel ncurses-devel time perl-Time-HiRes
1. install
If you use MySQL of Percona Server version, it is simple because it has built-in HandlerSocket support. However, considering that its built-in version is not new enough, there are some bugs that have already been fixed, so it is best to use source code compilation. HandlerSocket is based on the MySQL database. Therefore, you need to deploy the MySQL service in the conventional way before installing HanderSocket. you also need to pay attention to the MySQL source code when installing HandlerSocket. Therefore, you also need to install the MySQL source code compilation method.
[Root @ iredmail opt] # git clone https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL.git
[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 specifies the directory where the MySQL source code is located
-- With-mysql-bindir indicates the directory of MySQL binary executable files
-- With-mysql-plugindir specifies the storage path of the MySQL plug-in. if you do not know where the directory is, you can query it as follows:
Mysql> show variables like 'In in % ';
+ --------------- + --------------------------------------------- +
| Variable_name | Value |
+ --------------- + --------------------------------------------- +
| Plugin_dir |/usr/local/webserver/mysql5520/lib/plugin |
+ --------------- + --------------------------------------------- +
1 row in set (0.00 sec)
[Root @ iredmail HandlerSocket-Plugin-for-MySQL] # make
Common errors:
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
Solution:
[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
After completion, the directory of mysql-plugindir should contain handlersocket-related files.
2. configure MySQL:
Modify the my. cnf configuration file:
[Root @ iredmail HandlerSocket-Plugin-for-MySQL] # vi/etc/my. cnf
[Mysqld]
Plugin-load = handlersocket. so (plugin-load can be skipped)
Loose_handlersocket_port = 9998 # specify the read request port number
# The port number to bind to (for read requests)
Loose_handlersocket_port_wr = 9999 # specify the write request port number
# The port number to bind to (for write requests)
Loose_handlersocket_threads = 16 # specify the number of read threads
# The number of worker threads (for read requests)
Loose_handlersocket_threads_wr = 1 # specify the number of write threads
# The number of worker threads (for write requests)
Open_files_limit = 65535
# To allow handlersocket accept implements concurren connections, make open_files_limit as large as possible.
Tips:Innodb_buffer_pool_size of InnoDB, key_buffy_size of MyISAM, and other options related to cache indexes should be set as much as possible, so as to realize the potential of HandlerSocket.
Log on to MySQL and activate the HandlerSocket plug-in:
[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)
Note: The "handlersocket. so" extension file is not found. check whether the extension file exists.
Mysql> install plugin handlersocket soname 'handlersocket. so ';
Query OK, 0 rows affected (0.00 sec)
Mysql> quit;
Now, the HandlerSocket plug-in has been installed.
Restart the mysql service:
[Root @ iredmail HandlerSocket-Plugin-for-MySQL] # service mysqld restart
3. HandlerSocket status test:
You can also check whether the configured port has been occupied by MySQL to check whether the installation is successful:
[Root @ iredmail HandlerSocket-Plugin-for-MySQL] # lsof-I-P | grep mysqld
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.
3. install and configure the php-handlersocket extension module:
1. install php-handlersocket extension
[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 can be added with the following parameters:
Tips: If you get 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
Now the php extension is installed. on the php.info page, we can see that the handlersocket extension has been successfully loaded.
2. example of using php-handlersocket:
/*
* String $ host: MySQL ip address;
* String $ port: The listening port of the handlersocket plug-in. it has two optional ports: one for reading and the other for writing.
*/
$ Hs = new HandlerSocket ($ host, $ port );
Open a data table:
/*
* Int $ index: This number is equivalent to the handle in the file operation. All other HandlerSocket methods operate the table opened by this openIndex based on this number,
* String $ dbname: database name
* String $ table: table name
* String $ key: the "PRIMARY key" (HandlerSocket: PRIMARY) or "index name" of the table as the search keyword segment. This means that the table must have a PRIMARY key or index.
* Personal understanding: the key field to be used as the where condition. in this way, handlersocket has only one where condition.
* String $ column: fields in the table opened by 'column1, column2 '(separated by commas). That is to say, other fields in the $ table will not be operated.
*/
$ Hs-> openIndex ($ index, $ dbname, $ table, $ key, $ column );
Query:
/*
* Int $ index: $ index used by openIndex ()
* String $ operation: operator used for the $ key field specified in the openIndex method. Currently, '=', '> =', '<=', '> ', and '<'; can be understood as the where condition
* Array $ value
* Int $ number (1 by default): the maximum number of results obtained. it is equivalent to the second parameter of limit in SQL.
* Int $ skip (the default value is 0): The number of hops in the past. it is equivalent to the first parameter of limit in SQL.
*/
$ Retval = $ hs-> executeSingle ($ index, $ operation, $ value, $ number, $ skip );
Insert (note: $ port_wr is used for the openIndex here, that is, the read/write port ):
/*
* Int $ index: $ index used by openIndex ()
* Array $ arr: the number of numeric elements is the same as that of $ column in openIndex.
*/
$ Retval = $ hs-> executeInsert ($ index, $ arr );
Delete (note: $ port_wr is used for the openIndex here, that is, the read/write port ):
/*
* Int $ index: $ index used by openIndex ()
* String $ operation: operator used for the $ key field specified in the openIndex method. Currently, '=', '> =', '<=', '> ', and '<'; can be understood as the where condition
* Array $ value
* Int $ number (1 by default): the maximum number of results obtained. it is equivalent to the second parameter of limit in SQL.
* Int $ skip (the default value is 0): The number of hops in the past. it is equivalent to the first parameter of limit in SQL.
*/
$ Retval = $ hs-> executeDelete ($ index, $ operation, $ value, $ number, $ skip );
Update (note: $ port_wr is used for the openIndex here, that is, the read/write port ):
/*
* Int $ index: $ index used by openIndex ()
* String $ operation: operator used for the $ key field specified in the openIndex method. Currently, '=', '> =', '<=', '> ', and '<'; can be understood as the where condition
* Array $ value
* Int $ number (1 by default): the maximum number of results obtained. it is equivalent to the second parameter of limit in SQL.
* Int $ skip (the default value is 0): The number of hops in the past. it is equivalent to the first parameter of limit in SQL.
*/
$ Retval = $ hs-> executeUpdate ($ index, $ operation, $ value, $ number, $ skip );
Example:
Test database hstestdb, Test table 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:In theory, HandlerSocket supports various engines such as MyISAM and InnoDB, but InnoDB is recommended.
Tips:To avoid the insert error, Please remember set storage engine: InnoDB.
Tips:A common misunderstanding of HandlerSocket is that only KV queries of the PRIMARY type can be executed. In fact, as long as indexes are supported, simple queries can be competent. I will not talk about it here, as described in the official document.
HandlerSocket defects:
(1) the write operation has not eliminated the query cache-if the write operation is passed through HandlerSocket and the query cache is not invalid, you may read the old data from MySQL;
(2) auto increment is not supported-the auto increment value cannot be automatically obtained from the auto increment column during insertion.
In view of the above problems, we can use its combined query operations to obtain MySQL InnoDB table data using its NoSQL performance. the specific operations are as follows:
// Obtain data through handlersocket
$ Hs = new HandlerSocket (HS_HOST, HS_PORT );
If (! ($ Hs-> openIndex (1, 'dbname', 'table', HandlerSocket: PRIMARY, 'id, content, create_uid, create_user, created, state '))){
Echo $ hs-> getError (), PHP_EOL;
Die ();
}
$ DataList = array ();
Foreach ($ ids as $ id ){
$ DataList [] = array (1, "=", array ($ id ));
}
$ Data = $ hs-> executeMulti ($ dataList );
Written at the end:
MySQL5.6 provides native Memcached APIs, which are actually KV NoSQL, but HandlerSocket is not limited to the KV format, so there is still room for survival.