Thursday, March 1, 2012

How to Compile and Install Memcached



How to Compile and Install Memcached

Prerequisites:  libevent and memcached source code

Version: libevent 2.0.10, memcached 1.4.5

Memcached file download: http://memcached.org/

Summary: This guide outlines the steps required to build and install a memcached server.

  1. Create a directory to store your source files.

mkdir -p ~/source

  1. Download the libevent source code to ~/source

  1. Expand the libevent source archive.

cd ~/source; tar xzf libevent-2.0.10-stable.tar.gz

  1. Run the configure script with the following options.

cd  ~/source/libevent-2.0.10-stable

./configure --prefix=/opt/apps/libevent \
--disable-openssl

  1. Compile and install libevent

make
make install

  1. Reconfigure the run-time link bindings to include the libevent shared libraries

echo /opt/apps/libevent/lib > /etc/ld.so.conf.d/libevent.conf
ldconfig

  1. Download the memcached source code to ~/source

  1. Expand the memcached source archive.

cd ~source; tar xzf memcached-1.4.5.tar.gz

  1. Run the configure script with the following options.

./configure --prefix=/opt/apps/memcached \
--with-libevent=/opt/apps/libevent \
--enable-64bit




  1. Compile and install memcached

make
make install

cp ~/source/memcached-1.4.5/scripts/memcached-tool /opt/apps/memcached/bin/

  1. Create required memcached files/directories.

mkdir -p /opt/apps/memcached/conf
mkdir -p /opt/apps/memcached/init
mkdir -p /var/run/memcached
touch /var/lock/subsys/memcached

  1. Create a new configuration file /opt/apps/memcached/conf/memcached.conf. A sample can be seen here:
PORT="11211"
USER="nobody"
# max connection 2048
MAXCONN="2048"
# set ram size to 2048 - 2GiB
CACHESIZE="512"
# listen to loopback ip 127.0.0.1, for network connection use real ip e.g., 10.0.0.4
OPTIONS="-l whatever-app"
 


  1. Create the memcached init script /opt/apps/memcached/init/memcached.init and configure it to run on system startup. A sample copy can be seen below.  Modify the file accordingly to fit your site’s needs.

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

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

PORT=11211
USER=nobody
MAXCONN=1024
CACHESIZE=64
OPTIONS=""

#if [ -f /etc/sysconfig/memcached ];then
#    . /etc/sysconfig/memcached
#fi

if [ -f /opt/apps/memcached/conf/memcached.conf ];then
    . /opt/apps/memcached/conf/memcached.conf
fi

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
    exit 0
fi

RETVAL=0
prog="/opt/apps/memcached/bin/memcached"

start () {
    echo -n $"Starting $prog: "
    # insure that /var/run/memcached has proper permissions
    chown $USER /var/run/memcached
    $prog -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
    echo -n $"Stopping $prog: "
    killproc memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/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)
    restart
    ;;
    condrestart)
    [ -f /var/lock/subsys/memcached ] && restart || :
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
    exit 1
esac

exit $?
 


ln -s /opt/apps/memcached/init/memcached.init /etc/init.d/memcached

 chkconfig memcached on

  1. Verify memcached is configured to start up automatically upon system reboot.

# chkconfig --list memcached
memcached       0:off   1:off   2:on    3:on    4:on    5:on    6:off

  1. Start the memcached server.

/etc/init.d/memcached start

  1. Useful commands. Replace <host-ip> with the address memcached is bound to. <memcached-port> is the port memcached is listening on.

Display memcached slabs:
/opt/apps/memcached/bin/memcached-tool <host-ip>:<memcached-port> display

Display memcached general stats:
/opt/apps/memcached/bin/memcached-tool <host-ip>:<memcached-port> stats

Display memcached  keys and values:
/opt/apps/memcached/bin/memcached-tool <host-ip>:<memcached-port> dump


No comments:

Post a Comment