Category Archives: Caching

Memcached: How do you install memcached? (CentOS 64 bit, Linux, Redhat, Fedora)

[updated 5/16/2010]

Memcached is a very popular open source object caching server. It was developed to speed up livejournal.com by Danga Interactive.  We use memcached for a lot of our sites.  We use it for different purposes but one main purpose is to cache query results so we don’t have to keep hitting database.  As most of the people who work with databases know it is costly to keep hitting database for same information over and over.

When you run the Memcached daemon, it runs and listens on a specific port. One of the things Memcached does lack is security. Memcached will let anybody who can make a connection to its port have full access to all objects. So you would have to run a firewall to block unauthorized access. It is usually wise to do put firewall on it even if you trust everybody on the same network since accidents do happen. That said, let’s get memcached installed!

Let’s get libevent which is required by Memcached:

wget http://monkey.org/~provos/libevent-1.3e.tar.gz
tar zxpfv libevent*
cd libevent*
./configure
make install

Now let’s download the newest Memcached source (at time of update to this post 1.4.5 was the latest)

wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
tar zxpfv memcached*
cd memcached*
./configure
make install

Let’s add Memcached user to run daemon as since we don’t need it to run as root privileges.

adduser memcached

We will start the server to use 48 megs of ram (-m 48), listen on ip 10.0.0.2 (-l 10.0.0.2) and run on port 11211 (-p 11211) as user memcached (-u memcached)

./memcached -u memcached -d -m 48 -l 10.0.0.2 -p 11211

If you get the following error (which you will get if you are doing this under CentOS 64 bit):

./memcached: error while loading shared libraries: libevent-1.3e.so.1: cannot open shared object file: No such file or directory

You can fix this by simply doing this:

ln -s /usr/local/lib/libevent-1.3e.so.1 /lib64/

That is all there is to it. You can see if daemon is running by telneting to the port.

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

Mcache: Install and configure mcache (msession) to be used for session caching in PHP.

Installing mcache, previously known as msession, on CentOS 32 bit system.

If you are on 64 bit system, you will get errors, lots of them.  I went through and fixed “some” errors by modifying code but it was just taking too much time so I decided not to go 64 bit route.  But below are the efforts I made.  Maybe somebody can help with rest of the steps.   Following instructions work fine with 32 bit systems. For more help look at the MCache Handbook.
Thank you Mohawk Software for all your efforts developing this!

Web site: http://www.mohawksoft.org/?q=node/32
PHP reference:  http://us2.php.net/manual/en/ref.msession.php

yum install ncurses-devel #otherwise you might see errors about no such file curses.h

wget http://www.mohawksoft.org/download/mcache-070415-M2.0b6.tar.gz
wget http://www.mohawksoft.org/download/phoenix-070415-M2.0b6.tar.gz
tar zxf mcache*.gz
tar zxf phoenix*.gz
cd phoenix
ln -s Linux.mak config/config.mak
make dirs
make links
make butils
export PATH=$PATH:/opt/mohawk/bin
make libs
vi sqldrv/Makefile # about line 24, comment out POSTGRES=1 and ODBC=1
cd ../mcac*
make server
vi tools/Makefile # after line which says: CARGS+=-DBINDIR=\"$(BINDIR)\"
                  # add:  CARGS+=-DSBINDIR=\"$(SBINDIR)\"
make utils
ln -s /opt/mohawk/lib/libphoenix.so.2.2.2 /lib/
/opt/mohawk/sbin/mcache &

In your php.ini, add:   
[Session]
; Use mcache as the save handler
session.save_handler = mcache
; Set the host which runs the mcache daemon
session.save_path = localhost

Let us test if mcache server is running:

/opt/mohawk/bin/mping
You should see:
Usage: /opt/mohawk/bin/mping host
Pings a session daemon
Using localhost
localhost:8086 is alive

Great! You mcache server is now up and running and listening for connections.

You can add above command to your start up scripts so server will run next time you reboot.  Easiest way to achieve this is to add that command to end of /etc/rc.local

32 bit installation is now complete! 

———————

I could not get 64 bit install to go but here are my notes for whoever wants to try it.  If you get it working, please come back and comment on how you got it installed.

64 bit install notes:First error:  CPU you selected does not support x86-64 instruction set
to fix this, edit config/unixgcc.mak and remove all instances of -mtune=pentium3

Following are code changes to fix some other errors:

line 83 in phmalloc.h
virtual void *memdup(void *mem, unsigned int cb);
to
virtual void *memdup(void *mem, size_t cb);

line 446 in mexpat.cpp
static void *xmlalloc(void *context, unsigned int cb)
to
static void *xmlalloc(void *context, size_t cb)

static void *xmlrealloc(void *context, void *p, unsigned int cb)
to
static void *xmlrealloc(void *context, void *p, size_t cb)