Installing memcached functions for MySQL

tangent.org provide a memcached client for MySQL, in the form of UDFs. The provided set of functions allow for connecting to a memcached server, putting values in the cache, getting values, invalidating, utilizing increments etc.

The code is not (yet?) available in binary format, so the libraries need to be compiled and installed manually. Following is a quick installation HOWTO for Linux users.

I have installed on Ubuntu 8.04, running on an old IBM ThinkPad (600X) which has ~490MB and 10GB of disk space, with a 500MHz who-knows-which Intel processor. Well, that’s the machine on which I experiment… The setup I’ve tested is a single machine setup, with a single memcached instance.

memcached

To start with, you need to have the memcached daemon installed. Easy enough:

sudo apt-get install memcached

That should install memcached and start the daemon, on the default 11211 port (see /etc/memcached.conf)

libmemcached

Next, memcached functions for MySQL depend on libmemcached. This one comes with RPM and SRPM builds, but I’m running on Ubuntu/Debian, which invites trouble: I’ve tried installing the RPM, but got into dependency hell. I thought I may as well just compile the sources. And so I’ve downloaded libmemcached-0.25.tar.gz, and went throught the usualy steps:

tar xzfv libmemcached-0.25.tar.gz
cd libmemcached-0.25/
./configure
make
sudo make install

The configure script did give me some trouble, claiming something about invalid struct padding. Running configure with

bash -x configure

has shown that I was simply missing the g++ compiler. Once installed, all went well.

MySQL

We do need to have MySQL up and running, of course. Required version is 5.0 and above. But we also need to have mysql_config. This tool does not come with the standard apt-get package for debian/ubuntu. It is available in the develop package, though:

sudo apt-get install libmysqlclient-dev

RedHat and derived users can use the mysql-devel RPM. I have MySQL installed from binary tarball, so mysql_config is already there.

More dependencies

The README states you need to have the latest autoconf tools. pkg-config was required.

memcached funtions for MySQL

Finally, we get to business. Download sources for memcached functions to MySQL.

tar xzfv memcached_functions_mysql-0.7.tar.gz
cd memcached_functions_mysql-0.7/
./configure --with-mysql=/usr/local/mysql/bin/mysql_config --libdir=/usr/local/mysql/lib/mysql
make
sudo make install

The parameters to configure are the location of mysql_config, and the destination into which the libraries are written. Since I’ve installed my MySQL tarball on /usr/local/mysql, my destination is /usr/local/mysql/lib/mysql. You may wish to set this one up differently. Once the process is done, see that the libraries have indeed been created there. In particular, you’re looking for libmemcached_functions_mysql.so.

These libraries need to be found in the library search path. One way of doing so is to add the path to /etc/ld.so.conf:

sudo echo /usr/local/mysql/lib/mysql/ >> /etc/ld.so.conf

And then update the search path

sudo ldconfig

Once this is done, we can install the functions in MySQL. Go to the memcached_functions_mysql-0.7/ path, and execute:

mysql -u root -p < sql/install_functions.sql

This file simply contains the CREATE FUNCTION statements for all supplied memcached API.

Testing

To put our installation to the test, let’s try setting a value to memcached, then getting it back:

mysql> SELECT memc_set('mykey', 'Getting this with SELECT means all works well');
+--------------------------------------------------------------------+
| memc_set('mykey', 'Getting this with SELECT means all works well') |
+--------------------------------------------------------------------+
|                                                                  0 |
+--------------------------------------------------------------------+
1 row in set (0.03 sec)

mysql> SELECT memc_get('mykey');
+-----------------------------------------------+
| memc_get('mykey')                             |
+-----------------------------------------------+
| Getting this with SELECT means all works well |
+-----------------------------------------------+
1 row in set (0.00 sec)

The README file contains examples for all supplied functions. Take a look at the MySQL docs, as well.

Conclusion

It is also nice to see that Java or Python clients are also able to read the value stored with the “mykey” key. Well, that’s the nice thing about memcached: its diversity and compatibility of clients.

In a future post, I will write about if, why and how I think memcahed functions for MySQL should be used.

Please share below any insights about installing on other Linux flavours, BSD, Solaris or other operating systems.

13 thoughts on “Installing memcached functions for MySQL

  1. @Martin,
    Joy of open source: a *lot* of compilations and complications. I’ll be happy if you post another comments once you find the solution.

    Thanks

  2. Mysql is looking for the .so file in it’s specific plugin_dir, instead of relying on ld to be able to find the correct .so file.

    In a mysql cli, run the command
    show variables like ‘plugin_dir’

    Use the dir in the query result as your –libdir

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.