{"id":316,"date":"2008-12-10T09:56:30","date_gmt":"2008-12-10T07:56:30","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=316"},"modified":"2008-12-13T06:30:24","modified_gmt":"2008-12-13T04:30:24","slug":"installing-memcached-functions-for-mysql","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/installing-memcached-functions-for-mysql","title":{"rendered":"Installing memcached functions for MySQL"},"content":{"rendered":"<p><a href=\"http:\/\/tangent.org\/\">tangent.org<\/a> 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.<\/p>\n<p>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.<!--more--><\/p>\n<p>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&#8217;s the machine on which I experiment&#8230; The setup I&#8217;ve tested is a single machine setup, with a single memcached instance.<\/p>\n<h4>memcached<\/h4>\n<p>To start with, you need to have the <a href=\"http:\/\/www.danga.com\/memcached\/\">memcached<\/a> daemon installed. Easy enough:<\/p>\n<blockquote>\n<pre>sudo apt-get install memcached<\/pre>\n<\/blockquote>\n<p>That should install memcached and start the daemon, on the default 11211 port (see \/etc\/memcached.conf)<\/p>\n<h4>libmemcached<\/h4>\n<p>Next, memcached functions for MySQL depend on <a href=\"http:\/\/tangent.org\/552\/libmemcached.html\">libmemcached<\/a>. This one comes with RPM and SRPM builds, but I&#8217;m running on Ubuntu\/Debian, which invites trouble: I&#8217;ve tried installing the RPM, but got into dependency hell. I thought I may as well just compile the sources. And so I&#8217;ve downloaded libmemcached-0.25.tar.gz, and went throught the usualy steps:<\/p>\n<blockquote>\n<pre>tar xzfv libmemcached-0.25.tar.gz\r\ncd libmemcached-0.25\/\r\n.\/configure\r\nmake\r\nsudo make install<\/pre>\n<\/blockquote>\n<p>The configure script did give me some trouble, claiming something about invalid struct padding. Running configure with<\/p>\n<blockquote>\n<pre>bash -x configure<\/pre>\n<\/blockquote>\n<p>has shown that I was simply missing the g++ compiler. Once installed, all went well.<\/p>\n<h4>MySQL<\/h4>\n<p>We do need to have MySQL up and running, of course. Required version is 5.0 and above. But we also need to have <strong>mysql_config<\/strong>. This tool does not come with the standard apt-get package for debian\/ubuntu. It is available in the develop package, though:<\/p>\n<blockquote>\n<pre>sudo apt-get install libmysqlclient-dev<\/pre>\n<\/blockquote>\n<p>RedHat and derived users can use the <strong>mysql-devel<\/strong> RPM. I have MySQL installed from binary tarball, so <strong>mysql_config<\/strong> is already there.<\/p>\n<h4>More dependencies<\/h4>\n<p>The README states you need to have the latest <strong>autoconf<\/strong> tools. <strong>pkg-config<\/strong> was required.<\/p>\n<h4>memcached funtions for MySQL<\/h4>\n<p>Finally, we get to business. Download sources for <a href=\"http:\/\/tangent.org\/586\/Memcached_Functions_for_MySQL.html\">memcached functions to MySQL<\/a>.<\/p>\n<blockquote>\n<pre>tar xzfv memcached_functions_mysql-0.7.tar.gz\r\ncd memcached_functions_mysql-0.7\/\r\n.\/configure --with-mysql=\/usr\/local\/mysql\/bin\/mysql_config --libdir=\/usr\/local\/mysql\/lib\/mysql\r\nmake\r\nsudo make install<\/pre>\n<\/blockquote>\n<p>The parameters to <strong>configure<\/strong> are the location of <strong>mysql_config<\/strong>, and the destination into which the libraries are written. Since I&#8217;ve installed my MySQL tarball on <strong>\/usr\/local\/mysql<\/strong>, my destination is <strong>\/usr\/local\/mysql\/lib\/mysql<\/strong>. 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&#8217;re looking for <strong>libmemcached_functions_mysql.so<\/strong>.<\/p>\n<p>These libraries need to be found in the library search path. One way of doing so is to add the path to <strong>\/etc\/ld.so.conf<\/strong>:<\/p>\n<blockquote>\n<pre>sudo echo \/usr\/local\/mysql\/lib\/mysql\/ &gt;&gt; \/etc\/ld.so.conf<\/pre>\n<\/blockquote>\n<p>And then update the search path<\/p>\n<blockquote>\n<pre>sudo ldconfig<\/pre>\n<\/blockquote>\n<p>Once this is done, we can install the functions in MySQL. Go to the <strong>memcached_functions_mysql-0.7\/<\/strong> path, and execute:<\/p>\n<blockquote>\n<pre>mysql -u root -p &lt; sql\/install_functions.sql<\/pre>\n<\/blockquote>\n<p>This file simply contains the <strong>CREATE FUNCTION<\/strong> statements for all supplied memcached API.<\/p>\n<h4>Testing<\/h4>\n<p>To put our installation to the test, let&#8217;s try setting a value to memcached, then getting it back:<\/p>\n<blockquote>\n<pre>mysql&gt; SELECT memc_set('mykey', 'Getting this with SELECT means all works well');\r\n+--------------------------------------------------------------------+\r\n| memc_set('mykey', 'Getting this with SELECT means all works well') |\r\n+--------------------------------------------------------------------+\r\n|\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 |\r\n+--------------------------------------------------------------------+\r\n1 row in set (0.03 sec)\r\n\r\nmysql&gt; SELECT memc_get('mykey');\r\n+-----------------------------------------------+\r\n| memc_get('mykey')\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\r\n+-----------------------------------------------+\r\n| Getting this with SELECT means all works well |\r\n+-----------------------------------------------+\r\n1 row in set (0.00 sec)<\/pre>\n<\/blockquote>\n<p>The README file contains examples for all supplied functions. Take a look at the <a title=\"MySQL docs\" href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/ha-memcached-interfaces-mysqludf.html\">MySQL docs<\/a>, as well.<\/p>\n<h4>Conclusion<\/h4>\n<p>It is also nice to see that Java or Python clients are also able to read the value stored with the &#8220;mykey&#8221; key. Well, that&#8217;s the nice thing about memcached: its diversity and compatibility of clients.<\/p>\n<p>In a future post, I will write about if, why and how I think memcahed functions for MySQL should be used.<\/p>\n<p>Please share below any insights about installing on other Linux flavours, BSD, Solaris or other operating systems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[5],"tags":[28],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-memcached"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-56","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=316"}],"version-history":[{"count":26,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/316\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}