How to use NoSQL in MySQL?
Louis Hust
1 Introduction
MySQL5.6 has already supported memcached, so we can say MySQL is a mixture of RDBMS and NoSQL. But there is few materials about how to install memcached in MySQL and how to use it, and that is the reason i write this article. In this article, i will show you how to install memcached plugin with MySQL source code and how to use it with InnoDB engine. After that, you will have a general comprehension about it. So let's begin.
2 Architecture
At first, i have to mention the architecture in Figure 1which is used all the time when you search the keywords 'Nosql' && 'MySQL'.
3 Get MySQL source code
MySQL with memcached was just in MySQL Labs before, now it's contained in trunk. I just get the trunk tree with bazzar as below:
bzr branch http://bazaar.launchpad.net/~mysql/mysql-server/trunk/ trunk
This will take much time, so you can read left and get ready for install.
4 Build server
As you know, MySQL use cmake to compile the code, which is a cross-platform build system. Many people are used to use cmake at the source code directory. I'd like to make a new directory for cmake. Have a glance at my source directory.
Shell>pwd /home/loushuai/src/mysql-server/trunk Shell>ls bld cscope.out libmysqld sql-bench bld_memcached cscope.po.out libservices sql-common BUILD dbug man storage BUILD-CMAKE Docs mysql-test strings client Doxyfile-perfschema mysys support-files cmake extra packaging tags CMakeLists.txt files plugin tests cmd-line-utils include README unittest config.h.cmake INSTALL-SOURCE regex VERSION configure.cmake INSTALL-WIN-SOURCE scripts vio COPYING libevent source_downloads win cscope.in.out libmysql sql zlib
As you see, i have two directories: bld && bld_memcached, bld was used for general purpose and bld_memcached is used for memcached. Just follow my code:
Shell>pwd /home/loushuai/src/mysql-server/trunk Shell>mkdir bld_memcached Shell>cd bld_memcached Shell>cmake .. -DWITH_DEBUG=1 -DCMAKE_INSTALL_PREFIX=./mysql \ -DMYSQL_DATADIR=./mysql/data -DWITH_INNODB_MEMCACHED=ON -DENABLE_DOWNLOADS=1 Shell>make Shell>make install
Now let's check if the memcached plugin is built.
Shell>pwd /home/loushuai/src/mysql-server/trunk/bld_memcached/mysql/lib/plugin Shell>ls adt_null.so daemon_example.ini mypluglib.so semisync_master.so auth.so innodb_engine.so qa_auth_client.so semisync_slave.so auth_socket.so libdaemon_example.so qa_auth_interface.so auth_test_plugin.so libmemcached.so qa_auth_server.so
As seen above, what we need is two dynamic link library files:emphlibmemcached.so && emphinnodb_engine.so.
5 Start server
Before we start sever, we should following things:
- Initialize database Cause I install MySQL from source, so we need initialize the database manually.
Shell>pwd /home/loushuai/src/mysql-server/trunk/bld_memcached/mysql/scripts Shell>./mysql_install_db --datadir=../data --basedir=../ --user=loushuai
Cause I install MySQL with user 'loushuai', so i need to add -user, the same to you. - Config my.cnf
[mysqld] gdb datadir=../data socket=../data/mysql.sock autocommit=1 #back_log=200 log-bin=mysql-bin pid-file=../data/a.pid #wait_timeout=31536000 #interactive_timeout=31536000 server-id=1 log_slave_updates binlog-format=STATEMENT general-log
- Start MySQL server
Shell>pwd /home/loushuai/src/mysql-server/trunk/bld_memcached/mysql/bin Shell>./mysqld --defaults-file=my.cnf
- Create configure table for memcached Next we create the configure table and a demo table by running innodb_memcached_config.sql:
Shell>pwd /home/loushuai/src/mysql-server/trunk/bld_memcached/mysql/bin Shell>./mysql -uroot -h127.0.0.1 < ../scripts/innodb_memcached_config.sql
Check tables are created:mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | innodb_memcache | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> use innodb_memcache Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------------------+ | Tables_in_innodb_memcache | +---------------------------+ | cache_policies | | config_options | | containers | +---------------------------+ 3 rows in set (0.00 sec)
This configure script installs 3 tables needed by the InnoDB Memcached. These tables are created in a dedicated database ïnnodb_memcache". We will go over these three tables in a bit more detail: - Install memcached plugin At last we need install the memcached daemon plugin:
mysql> install plugin daemon_memcached soname "libmemcached.so"; Query OK, 0 rows affected (0.01 sec)
6 How to use?
We can set and get value just telnet the memcached port:
Shell>telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. set all 0 0 9 123456789 STORED get all VALUE all 0 9 123456789 END get AA VALUE AA 8 12 HELLO, HELLO END quit Connection closed by foreign host.
So we can check the value we just set into table with SQL:
mysql> select * from test.demo_test; +-----+--------------+------+------+------+ | c1 | c2 | c3 | c4 | c5 | +-----+--------------+------+------+------+ | AA | HELLO, HELLO | 8 | 0 | 0 | | all | 123456789 | 0 | 1 | 0 | +-----+--------------+------+------+------+ 2 rows in set (0.00 sec)
As you see, we set the value '123456789' with the key 'all' which are stored in test.demo_test. And the mapping relation is define in the table innodb_memcache.containers.
References
File translated from
TEX
by
TTH,
version 4.03.
On 21 Nov 2012, 15:29.