How To Compile Memcached On CentOS

by lifeLinux on July 28, 2012

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. In this article, I’ll show you “How to compile memcached on CentOS” step-by-step.

Download memcached & libevent

The first step, download latest versions of memcached & libevent, type following commands

# cd /opt
# wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
# wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz

Installing libevent

To install libevent, type the following commands

# tar zxvf libevent-1.4.14b-stable.tar.gz
# cd libevent-1.4.14b-stable
# ./configure --prefix=/usr/lib/libevent
# make
# make install

Installing memcached

To install memcached, type the following commands

# cd /opt
# tar zxvf memcached-1.4.13.tar.gz
# cd memcached-1.4.13
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/lib/libevent
# make
# make install

Configuring memcached

Adding a user called “memcached”, enter

# useradd -s /sbin/nologin -d /usr/share memcached

If you wish to add memcached service to start when the machine boot, create the memcached script file in /etc/init.d

# touch /etc/init.d/memcached
# chmod +x /etc/init.d/memcached
# vi /etc/init.d/memcached

Add the following content

#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached

# Source function library.
. /etc/init.d/functions

MEMCACHED="/usr/local/memcached/bin/memcached"
VAR_RUN="/var/run"
VAR_LOCK="/var/lock/subsys/"

PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=256
OPTIONS=""

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
        exit 0
fi

RETVAL=0
prog="memcached"

start () {
        echo -n $"Starting $prog: "
        daemon --pidfile $VAR_RUN/memcached.pid $MEMCACHED -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P $VAR_RUN/memcached.pid $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $VAR_LOCK/memcached
}
stop () {
        echo -n $"Stopping $prog: "
        killproc -p $VAR_RUN/memcached.pid $MEMCACHED
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
            rm -f $VAR_LOCK/memcached
            rm -f $VAR_RUN/memcached.pid
        fi
}

restart () {
        stop
        start
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status memcached
        ;;
  restart|reload|force-reload)
        restart
        ;;
  condrestart)
        [ -f $VAR_LOCK/memcached ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
        exit 1
esac

exit $?

And type the following commands to add memcached service to start when the machine boot, and start memcached service

# chkconfig --level 345 memcached on
# service memcached start

Testing memcached

Connecting to memcached, type

# telnet localhost 11211

Output

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Leave a Comment

Previous post:

Next post: