Table of Contents
安装
配置
限制
实战
Home Database Mysql Tutorial 初窥InnoDB的Memcached插件

初窥InnoDB的Memcached插件

Jun 07, 2016 pm 04:32 PM
innodb memcached plug-in

前些年,HandlerSocket的横空出世让人们眼前一亮,当时我还写了一篇文章介绍了其用法梗概,时至今日,由于种种原因,HandlerSocket并没有真正流行起来,不过庆幸的是MySQL官方受其启发,研发了基于InnoDB的Memcached插件,总算是在MySQL中延续了NoSQL的香火

前些年,HandlerSocket的横空出世让人们眼前一亮,当时我还写了一篇文章介绍了其用法梗概,时至今日,由于种种原因,HandlerSocket并没有真正流行起来,不过庆幸的是MySQL官方受其启发,研发了基于InnoDB的Memcached插件,总算是在MySQL中延续了NoSQL的香火,以前单独架设Memcached服务器不仅浪费了内存,而且还必须自己维护数据的不一致问题,有了Memcached插件,这些问题都不存在了,而且借助MySQL本身的复制功能,我们可以说是变相的实现了Memcached的复制,这更是意外之喜。

安装

为了让文章更具完整性,我们选择从源代码安装MySQL,需要注意的是早期的版本有内存泄漏,所以推荐安装最新的稳定版,截至本文发稿时为止,最新的稳定版是5.6.13,我们就以此为例来说明,过程很简单,只要激活了WITH_INNODB_MEMCACHED即可:

shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> tar zxvf mysql-5.6.13.tar.gz
shell> cd mysql-5.6.13
shell> cmake . -DWITH_INNODB_MEMCACHED=ON
shell> make
shell> make install
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
shell> cp support-files/mysql.server /etc/init.d/mysql.server
Copy after login

MySQL安装完毕后,需要导入Memcached插件所需要的表结构:

mysql> SOURCE /usr/local/mysql/share/innodb_memcached_config.sql
Copy after login

一切就绪后就可以激活Memcached插件了(当然也可以禁止):

mysql> INSTALL PLUGIN daemon_memcached soname "libmemcached.so";
mysql> UNINSTALL PLUGIN daemon_memcached;
Copy after login

说明:以后一旦修改了原本已存在的Memcached插件的配置信息,均需要使用如上命令来重启Memcached插件(先uninstall,再install),以便让修改生效。

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                |
+----------------------------------+------------------+
Copy after login

注意:daemon_memcached_r_batch_size和daemon_memcached_w_batch_size,这两个选项对性能影响较大,简单点说就是控制事务提交的频率,MySQL的缺省值均为1,也就是说每次都提交,这主要是从安全性考虑的,大家可以依照自己的情况来调整。

差不多了,此时Memcached端口应该准备就绪了,你可以试试看:

shell> echo "stats" | nc localhost 11211
Copy after login

换句话说,MySQL已经兼容Memcached协议,可以直接使用Memcached命令。

配置

在安装步骤里,我们导入了一个名为innodb_memcached_config.sql的脚本,它创建了一库(innodb_memcache)三表(cache_policies, config_options, containers):

mysql> USE innodb_memcache
mysql> SHOW TABLES;
+---------------------------+
| Tables_in_innodb_memcache |
+---------------------------+
| cache_policies            |
| config_options            |
| containers                |
+---------------------------+
Copy after login

cache_policies定义了缓存策略,包含如下选择:

  • innodb_only:只使用InnoDB作为数据存储。

  • cache-only:只使用传统的Memcached引擎作为后端存储。

  • caching:二者皆使用,如果在Memcached里找不到,就查询InnoDB。

  • config_options定义了分隔符号:

  • separator:Memcached只识别单值,使用此分隔符(|)来连接多个字段的值。

  • table_map_delimiter:通过此分隔符(.)来确认表和键,如:@@table.key。

  • 如果我们想通过Memcached协议来访问一个表,需要先在containers中配置它:

mysql> SELECT * FROM containers\G
*************************** 1. row ***************************
                  name: aaa
             db_schema: test
              db_table: demo_test
           key_columns: c1
         value_columns: c2
                 flags: c3
            cas_column: c4
    expire_time_column: c5
unique_idx_name_on_key: PRIMARY
Copy after login

如上已经有了test数据库的demo_test表,通过c1查询c2的值,表结构如下所示:

mysql> DESC test.demo_test;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| c1    | varchar(32)         | NO   | PRI |         |       |
| c2    | varchar(1024)       | YES  |     | NULL    |       |
| c3    | int(11)             | YES  |     | NULL    |       |
| c4    | bigint(20) unsigned | YES  |     | NULL    |       |
| c5    | int(11)             | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
Copy after login

缺省情况下有一行数据:

mysql> SELECT * FROM test.demo_test;
+----+--------------+------+------+------+
| c1 | c2           | c3   | c4   | c5   |
+----+--------------+------+------+------+
| AA | HELLO, HELLO |    8 |    0 |    0 |
+----+--------------+------+------+------+
Copy after login

让我们用Memcached协议来访问看看:

shell> echo "get @@aaa.AA" | nc localhost 11211
VALUE @@aaa.AA 8 12
HELLO, HELLO
END
Copy after login

我们还可以先设定缺省访问的表,然后后续的查询就只写键名就可以了:

shell> (echo "get @@aaa"; echo "get AA") | nc localhost 11211
VALUE @@aaa 0 14
test/demo_test
END
VALUE AA 8 12
HELLO, HELLO
END
Copy after login

虽然我的例子都是通过命令行执行的,但是大家很容易就更改写成PHP之类的方法。

限制

Memcached插件用起来非常简单,不过并不是一切都很完美,比如说:当我们配置表的时候,containers表的字段,除了key_columns和value_columns以外,其它的字段,如:flags,cas_column,expire_time_column等也必须设定,可是很多时候,我们在原表中找不到贴切的字段,此时就只能对应新建三个字段,味道很恶心。

此外,containers表还有如下限制:

  • key_columns字段的类型必须是CHAR或VARCHAR,且最大长度是250个字符。

  • value_columns字段的类型必须是CHAR或VARCHAR或BLOB,长度不限。

  • cas_column字段的类型必须是BIGINT。

  • expiration_time_column字段的类型必须是INT。

  • flags字段的类型必须是INT。

  • 说明:随着MySQL版本的更新,这些限制可能会发生变化,请大家以实际情况为准。

    实战

    让我们以一个用户登录的例子来检验一下学习成果:

    首先在测试数据库创建一个用户表:

    USE `test`
    CREATE TABLE `users` (
        `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        `username` VARCHAR(15) NOT NULL,
        `password` VARCHAR(32) NOT NULL,
        `email` text NOT NULL,
        `flags` INT(10) UNSIGNED DEFAULT '0',
        `cas_column` BIGINT(20) UNSIGNED DEFAULT '0',
        `expire_time_column` INT(10) UNSIGNED DEFAULT '0',
        PRIMARY KEY (`id`),
        UNIQUE KEY `username` (`username`)
    ) ENGINE=InnoDB;
    Copy after login

    然后添加几行测试数据:

    INSERT INTO `users` (`username`, `password`, `email`)
    VALUES
    ('foo', 'ffffffffffffffffffffffffffffffff', 'foo@domain.com'),
    ('bar', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'bar@domain.com');
    Copy after login

    接着在containers里配置这个表:

    INSERT INTO innodb_memcache.containers (
        name, db_schema, db_table, key_columns, value_columns,
        flags, cas_column, expire_time_column, unique_idx_name_on_key
    ) VALUES (
        'default', 'test', 'users', 'username', 'password|email',
        'flags', 'cas_column', 'expire_time_column', 'username'
    );
    Copy after login

    这里我们定义了多个字段(password和email)作为value_columns,并且使用竖线作为分隔符,实际上使用空格,逗号之类分隔符也可以,在innodb_config.c文件的源代码中能查到如下关于分隔符的定义,文档里并没有涵盖这些信息:

    static const char* sep = " ;,|\n";
    Copy after login

    最后使用Memcached协议来访问一下,这里我们换个花样,执行一个MGET操作:

    shell> echo "get foo bar" | nc localhost 11211
    VALUE foo 0 47
    ffffffffffffffffffffffffffffffff|foo@domain.com
    VALUE bar 0 47
    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb|bar@domain.com
    END
    Copy after login

    既然我定义value_columns的时候设置了多个字段,那么返回数据的时候自然也返回多个字段的数据,并且它们依照innodb_memcache.config_options表中的separator字段来分隔,缺省情况下是一个竖线,如果你的字段内容里包含了竖线,那么就会和缺省值发生冲突,此时你可以更新separator的定义,比如改成三个竖线等等,需要提醒的是,修改后别忘了重启Memcached插件。

    说明:因为们在配置的时候把表命名为default,所以在请求的时候不用传递表名。

    本文在使用Memcached插件的时候,所有例子均使用的是读操作,实际上写操作也是支持的,不过在实际使用时,我更倾向于写操作都通过SQL来执行,而Memcached插件仅处理KV形式的读操作,实际压力测试的结果显示,Memcached插件比SQL的方式,性能提升了百分之一百左右,虽然仍不及独立的Memcached,但考虑到其它的优势,这个结果已经很让人欣喜了,下一站,InnoDB的Memcached插件!大家做好准备吧。

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! Feb 25, 2024 pm 11:57 PM

    PyCharm is a powerful and popular Python integrated development environment (IDE) that provides a wealth of functions and tools so that developers can write code more efficiently. The plug-in mechanism of PyCharm is a powerful tool for extending its functions. By installing different plug-ins, various functions and customized features can be added to PyCharm. Therefore, it is crucial for newbies to PyCharm to understand and be proficient in installing plug-ins. This article will give you a detailed introduction to the complete installation of PyCharm plug-in.

    Error loading plugin in Illustrator [Fixed] Error loading plugin in Illustrator [Fixed] Feb 19, 2024 pm 12:00 PM

    When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

    What is the Chrome plug-in extension installation directory? What is the Chrome plug-in extension installation directory? Mar 08, 2024 am 08:55 AM

    What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

    Share three solutions to why Edge browser does not support this plug-in Share three solutions to why Edge browser does not support this plug-in Mar 13, 2024 pm 04:34 PM

    When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

    Does PyCharm Community Edition support enough plugins? Does PyCharm Community Edition support enough plugins? Feb 20, 2024 pm 04:42 PM

    Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

    How to use WordPress plug-in to implement video playback function How to use WordPress plug-in to implement video playback function Sep 05, 2023 pm 12:55 PM

    How to use WordPress plug-in to implement video playback function 1. Introduction The application of video on websites and blogs is becoming more and more common. In order to provide a high-quality user experience, we can use WordPress plug-ins to implement video playback functions. This article will introduce how to use WordPress plugins to implement video playback functions and provide code examples. 2. Choose plug-ins WordPress has many video playback plug-ins to choose from. When choosing a plug-in, we need to consider the following aspects: Compatibility: Make sure the plug-in

    How to add online ordering functionality to WordPress plugin How to add online ordering functionality to WordPress plugin Sep 05, 2023 pm 03:42 PM

    How to Add Online Ordering Function to WordPress Plugin In today’s digital era, many restaurants and coffee shops have chosen to move the ordering process online to meet the needs of customers. WordPress is a widely used content management system (CMS), and many businesses are using WordPress to build their websites. This article will introduce how to add online ordering functionality to WordPress plug-in and provide corresponding code examples. Step 1: Choose the Right Plugin First, we need to create a custom plugin in WordPress

    How to add WeChat mini program functionality to WordPress plug-in How to add WeChat mini program functionality to WordPress plug-in Sep 06, 2023 am 09:03 AM

    How to Add WeChat Mini Program Functions to WordPress Plugins With the popularity and popularity of WeChat mini programs, more and more websites and applications are beginning to consider integrating them with WeChat mini programs. For websites that use WordPress as their content management system, adding the WeChat applet function can provide users with a more convenient access experience and more functional choices. This article will introduce how to add WeChat mini program functionality to WordPress plug-in. Step 1: Register a WeChat mini program account. First, you need to open the WeChat app

    See all articles