PhpHandlerSocket usage _ PHP Tutorial

Source: Internet
Author: User
Tags percona server
Use of phpHandlerSocket. 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. In the instant Web era,
  • 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. It is said that Sina Weibo encountered such a problem some time ago.

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 these 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!

Performance Test: Using MySQL as a NoSQL-A story for exceeding 750,000 qps (F * ck GFW)

Because HandlerSocket performance is good enough, there is no need to use Memcache, which can save a lot of hardware resources, rather low carbon! In addition, HandlerSocket operates the index of MySQL in the memory without any additional cache, so there is no data consistency problem.

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.

Note: for some questions about HandlerSocket in the old version, see: What's up with HandlerSocket?

The official website already has a simple installation document, but I encountered some other unspecified problems during actual installation, so I will write the corresponding installation process again here.

First, make sure that the version of MySQL5.1 and later has been installed. I use the Ubuntu operating system. I have installed MySQL5.1.37 with apt in advance, and also need the corresponding mysql_config. if it is Ubuntu, yes:

shell> aptitude install libmysqld-dev

Note: If the MySQL you are using is a binary version compiled from the source code or officially provided, you can skip this step.

Download the MySQL source code and HandlerSocket source code that are consistent with the MySQL version of the system:

  • Mysql-5.1.37.tar.gz
  • Ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-76-gf5f7443.tar.gz
shell> tar zxf mysql-5.1.37.tar.gzshell> tar zxf ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-76-gf5f7443.tar.gzshell> cd ahiguti-HandlerSocket-Plugin-for-MySQL-f5f7443shell> ./autogen.shshell> ./configure --with-mysql-source=../mysql-5.1.37 \          --with-mysql-bindir=/usr/bin \          --with-mysql-plugindir=/usr/lib/mysql/plugin

The parameter meanings are as follows: with-mysql-source indicates the MySQL source code directory, and with-mysql-bindir indicates the MySQL binary executable file directory (that is, the directory where mysql_config is located ), with-mysql-plugindir indicates the MySQL plug-in Directory. if you do not know where the directory is, you can query it as follows:

mysql> SHOW VARIABLES LIKE 'plugin%';+---------------+-----------------------+| Variable_name | Value         |+---------------+-----------------------+| plugin_dir  | /usr/lib/mysql/plugin |+---------------+-----------------------+

After running the command, if MySQL5.1.37 is used, the following error message is displayed:

MySQL source version does not match MySQL binary version

It is clear that both the MySQL source code version and the binary version are 5.1.37. Why does this error still occur? By querying the HandlerSocket compilation script, we found that it would retrieve the VERSION file in the MySQL source code directory, but the file was not found in the source code directory of MySQL5.1.37, so an error was reported, now that we know the reason, let's create a VERSION file and put it in the MySQL source code directory. the content is as follows:

MYSQL_VERSION_MAJOR=5MYSQL_VERSION_MINOR=1MYSQL_VERSION_PATCH=37MYSQL_VERSION_EXTRA=

Run the configure script again, and the remaining steps should be completed:

shell> makeshell> make install

Configure HandlerSocket, edit the MySQL configuration file, and add the following content:

[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.

In addition, 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.

Note: the configuration file under apt package management is usually/etc/mysql/my. cnf, otherwise it is usually/etc/my. cnf

Finally, log on to MySQL and activate the HandlerSocket plug-in:

mysql> INSTALL PLUGIN handlersocket soname 'handlersocket.so';

Restart the MySQL service. if there is no problem, you can see the HandlerSocket thread in MySQL:

mysql> SHOW PROCESSLIST;

You can also check whether the configured port has been occupied by MySQL to check whether the installation is successful:

shell> lsof -i :9998shell> lsof -i :9999

Finished! Now your MySQL has the NoSQL capability!

Practice

First, create a test table:

CREATE TABLE IF NOT EXISTS `test`.`t` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `a` varchar(10) NOT NULL, `b` varchar(10) NOT NULL, PRIMARY KEY (`id`), KEY `a_b` (`a`,`b`)) ENGINE=InnoDB;

Note: In theory, HandlerSocket supports various engines such as MyISAM and InnoDB, but InnoDB is recommended.

The HandlerSocket protocol is very simple. commands are separated by tabs, and one row is a request. This article uses:

  • Enable index: P <索引标识> <数据库> <表> <索引> <字段>
  • Insert data: <索引标识> '+' <参数个数> <参数1> ... <参数n>
  • Read data: <索引标识> <操作> <参数个数> <参数1> ... <参数n> <条数> <偏移>

SQL prototype: insert into test. t (id, a, B) VALUES (1, 'A1', 'B1'), (2, 'A2 ', 'B2 ′)

shell> telnet localhost 9999Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.P    1    test  t    PRIMARY id,a,b0    11    +    3    1    a1   b10    1    01    +    3    2    a2   b20    1    0

Note: When HandlerSocket is used, because no SQL is actually run, the Binlog records the Row format.

SQL prototype: SELECT id, a, B FROM test. t WHERE id = 1 LIMIT 1

shell> telnet localhost 9999Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.P    1    test  t    PRIMARY id,a,b0    11    =    1    1    1    00    3    1    a1   b1

SQL prototype: SELECT id, a, B FROM test. t WHERE id> = 1 LIMIT 2

shell> telnet localhost 9999Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.P    1    test  t    PRIMARY id,a,b0    11    >=   1    1    2    00    3    1    a1   b1   2    a2   b2

SQL prototype: SELECT id, a, B FROM test. t WHERE a = 'A1' AND B = 'b1 'LIMIT 1

shell> telnet localhost 9999Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.P    1    test  t    a_b   id,a,b0    11    =    2    a1   b1   1    00    3    1    a1   b1

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 are generally competent and limited in length. I will not say much here, if you think it is difficult to directly operate telnet, you can also use a client that you are familiar with to test it. the official documentation provides an introduction.

Note: The author of HandlerSocket wrote a good PPT. for details, refer to HandlerSocket plugin for MySQL.

Note: 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.

The development of Internet technology is like a high-speed train. Next stop: HandlerSocket! Make preparations.

Bytes. This is in the era of instant ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.