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.
@Pete
Try running
ldconfig –verbose
to see the current search path. Either you put the libraries in one of the listed paths, or you create a sym link, or just add you path to /etc/ld.so.conf
Shlomi
Nice post. I ran through all the install steps using YUM on my Fedora 10 machine and everything went smoothly, including the loading of UDFs.
I can
SELECT mem_setc(‘mykey’, ‘Some data here’);
…however…when I try to retrieve it with:
SELECT mem_getc(‘mysql’);
it finds the key, but null instead of ‘Some data here’.
Setup:
Fedora 10 – kernel 2.6.27.37-170.2.104.fc10.i686
MySQL 5.0.84
Did I miss something?
@SailorCTO
Hi,
Thanks for posting. I’m unable at this moment to check this on my current setup, sorry 🙁
Best if you could ping the appropriate forums, or even poke Patrick Galbraith 🙂
Regards
That’s pretty slick. I like it. I made a UDF too that enables JavaScript to evaluate on MySQL. It could be helpful if you are into UDFs: http://bit.ly/cKA8np
Thx for this great tutorial, I managed finally to compile and install everything on a debian lenny system.
Just the last step doesn’t work, when I try
mysql -u root -p < sql/install_functions.sql
mysql replies that the database is missing and when I add a –database=xxx the mysql replies 'ERROR 1126 (HY000) at line 1: Can't open shared library 'libmemcached_functions_mysql.so' (errno: 22 /usr/lib/mysql/libmemcached_functions_mysql.so: undefined symbol: memcached_string_append)'
Maybe I will find a way, already checked the rights etc.
Some day it will work 🙂