Development, Analysis And Research


Installing memcached

Posted in General by Andrew Johnstone on the April 7th, 2007

Recently I had to install memcache on a number of servers, and I would always tend to end up with errors whilst memcache tries to locate libevent. I always seem to forgett LD_DEBUG, so I figured I would write up the process for installing memcache.

One of the dependencies of memcache is libevent, so firstly download the source files for Libevent.


tar -xvf libevent-1.3b.tar.gz
cd libevent-1.3b
./configure;make;make install;

Download the latest Memcached source code from danga.com


gunzip memcached-1.2.1.tar.gz
tar -xvf memcached-1.2.1.tar
cd memcached-1.2.1
./configure;make;make install;

Often libevent.so cannot be found when executing memcache. A useful command LD_DEBUG, is very helpful to determine where libraries are being loaded from.


LD_DEBUG=help memcached -v
 
LD_DEBUG=libs memcached -v 2>&1 > /dev/null | less
18990: find library=libevent-1.3b.so.1 [0]; searching
...
18990: trying file=/usr/lib/libevent-1.3b.so.1
18990:
memcached: error while loading shared libraries: libevent-1.3b.so.1: cannot open shared object file: No such file or directory

Simply place the library where memcached will find it and execute memcached.


ln -s /usr/local/lib/libevent-1.3b.so.1 /lib/libevent-1.3b.so.1
memcached -d -u nobody -m 512 127.0.0.1 -p 11211
 

The options for memcached are:

-l <ip_addr>  
Listen on <ip_addr>; default to INDRR_ANY. This is an important option to consider as there is no other way to secure the installation. Binding to an internal or firewalled network interface is suggested.
-d
Run memcached as a daemon.
-u <username>
Assume the identity of <username> (only when run as root).
-m <num>
Use <num> MB memory max to use for object storage; the default is 64 megabytes.
-M
Instead of throwing items from the cache when max memory is reached, throw an error
-c <num>
Use <num> max simultaneous connections; the default is 1024.
-k
Lock down all paged memory. This is a somewhat dangerous option with large caches, so consult the README and memcached homepage for configuration suggestions.
-p <num>
Listen on port <num>, the default is port 11211.
-r
Maximize core file limit
-M
Disable automatic removal of items from the cache when out of memory. Additions will not be possible until adequate space is freed up.
-r
Raise the core file size limit to the maximum allowable.
-h
Show the version of memcached and a summary of options.
-v
Be verbose during the event loop; print out errors and warnings.
-vv
Be even more verbose; same as -v but also print client commands and responses.
-i
Print memcached and libevent licenses.
-P <filename>
Print pidfile to <filename>, only used under -d option.

To install the pecl package for PHP

wget http://pecl.php.net/get/memcache-2.1.2.tgz
gzip -df memcache-2.1.2.tgz
tar -xvf memcache-2.1.2.tar
cd memcache-2.1.2
phpize
./configure;make;make install;

Add memcache.so to the php.ini file

extension=memcache.so

Then run

php -i | grep -i 'memcache'

memcache should be listed and then restart the web server.

For further information:
Distributed Caching with Memcached

10 Responses to 'Installing memcached'

Subscribe to comments with RSS or TrackBack to 'Installing memcached'.

  1. George_A said,

    on August 17th, 2007 at 5:17 am

    Very very well!!!

  2. pradeep kumar yadav said,

    on September 26th, 2007 at 9:26 am

    This is very good document ,very help full. I need to know how it used in application.

  3. Jonny Mac said,

    on October 24th, 2007 at 12:40 am

    thanks Andrew. Great Tutorial.


  4. on June 8th, 2008 at 9:56 am

    [...] další si můžete přečíst někde jinde. Tady půjde o jeho zprovoznění. Nebudu zde popisovat postup pro Linux, který je ale dost podobný. Na závěr se mrkneme i jak to celé rozchodit v PHP. V celé věci [...]

  5. James said,

    on September 26th, 2008 at 12:34 pm

    What about upgrading?

    Any specific process that I would need to go through to upgrade memcached?


  6. on January 24th, 2009 at 8:39 pm

    I was trying to get memcached to run on a shared host, here’s what I had to do:

    1. configure libevent with these options: –prefix=[path to your home lib folder]

    in my case, I just created a folder: ~/libs and set the prefix to that.

    2. configure memcached with these options: –with-libevent=[path to your home lib folder] –prefix=[your home apps folder]

    the [path to your home lib folder] is just where you previously installed libevent

    3. If you can’t touch ld.so.conf (like me), then simply type: LD_SEARCH_PATH=[path to your home lib folder]; export LD_SEARCH_PATH;

    You might need to include that into your terminal startup script.

    4. Running on OpenBSD will require you to use memcached with the option: -c 64 (due to how OBSD limits connections).

    I still haven’t gotten the PECL package to install on a shared host. Ugh. Almost there…


  7. on June 28th, 2009 at 8:45 pm

    On CentOS you can install it with yum as follows:

    1) Determine CentOS version by typing “cat /etc/*release*”.

    2) Install the DAG public key. If on CentOS version 4.x, type
    rpm -Uhv http://apt.sw.be/redhat/el4/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el4.rf.i386.rpm

    if on CentOS version 5.x, type
    rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

    3) Create a file at /etc/yum.repos.d/dag.repo with these contents:
    >>>
    [dag]
    name=Dag RPM Repository for Red Hat Enterprise Linux
    baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
    gpgcheck=1
    enabled=1
    >>>

    4) Then do “yum install memcached” and it should install smoothly.

  8. crzdot said,

    on June 29th, 2009 at 3:38 am

    regards:

    thanks for your resource.
    I now wanna to using memcache on my developing machine with windows XP operation system.

    Is there any guider to install memcache on windows?

    thanks.

  9. DiGennaro said,

    on October 18th, 2009 at 9:33 pm

    There is an easier way to address the libevent.so issue.In /etc/ld.so.conf.d simply add a file called local.conf and in it insert 1 line that says /usr/local/lib. Then run ldconfig and you are good to go.

  10. Carlos said,

    on November 5th, 2009 at 4:40 pm

    Hello crzdot.Take a look at this: http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/

Leave a Reply