前些年,HandlerSocket的横空出世让人们眼前一亮,当时我还写了一篇文章介绍了其用法梗概,时至今日,由于种种原因,HandlerSocket并没有真正流行起来,不过庆幸的是MySQL官方受其启发,研发了基于InnoDB的Memcached插件,总算是在MySQL中延续了NoSQL的香火,以前单独架设Memcached服务器不仅浪费了内存,而且还必须自己维护数据的不一致问题,有了Memcached插件,这些问题都不存在了,而且借助MySQL本身的复制功能,我们可以说是变相的实现了Memcached的复制,这更是意外之喜。
安装
为了让文章更具完整性,我们选择从源代码安装MySQL,需要注意的是早期的版本有内存泄漏,所以推荐安装最新的稳定版,截至本文发稿时为止,最新的稳定版是5.6.13,我们就以此为例来说明,过程很简单,只要激活了WITH_INNODB_MEMCACHED即可:
shell> groupadd mysqlshell> useradd -r -g mysql mysqlshell> tar zxvf mysql-5.6.13.tar.gzshell> cd mysql-5.6.13shell> cmake . -DWITH_INNODB_MEMCACHED=ONshell> makeshell> make installshell> cd /usr/local/mysqlshell> chown -R mysql .shell> chgrp -R mysql .shell> scripts/mysql_install_db --user=mysqlshell> chown -R root .shell> chown -R mysql datashell> bin/mysqld_safe --user=mysql &shell> cp support-files/mysql.server /etc/init.d/mysql.server |
MySQL安装完毕后,在插件目录我们能看到innodb_engine.so和libmemcached.so:
mysql> SELECT @@plugin_dir;+------------------------------+| @@plugin_dir |+------------------------------+| /usr/local/mysql/lib/plugin/ |+------------------------------+ |
mysql> SOURCE /usr/local/mysql/share/innodb_memcached_config.sql |
mysql> INSTALL PLUGIN daemon_memcached soname "libmemcached.so";mysql> UNINSTALL PLUGIN daemon_memcached; |
Memcached插件相关的配置信息如下,具体介绍可以参考官方文档:
mysql> SHOW VARIABLES LIKE '%memcached%';+----------------------------------+------------------+| Variable_name | Value |+----------------------------------+------------------+| daemon_memcached_enable_binlog | OFF || daemon_memcached_engine_lib_name | innodb_engine.so || daemon_memcached_engine_lib_path | || daemon_memcached_option | || daemon_memcached_r_batch_size | 1 || daemon_memcached_w_batch_size | 1 |+----------------------------------+------------------+ |