Centos – Right way to configure memcache

centosconfigurationmemcachememcached

I'm trying to change memcache memory size limit in my CentOS env. I've examined other questions and found out there SHOULD be a file, which is mostly under /etc/sysconfig/memcached and if not I should be able to find it with locate.

Both didn't work out for me. The only file it find is in /root/rpms/memcached-1.4.5/t/sasl/memcached.conf And doesn't seem to hold any reference to memory limits.

When I start memcache, I'm setting it through the command line like this:

memcached -d -m 2048 -u root -l 0.0.0.0 -p 11211

I assume this means I'm just not using any configuration settings file, and it might be better to add one. How do I add such a file and make memcached start use its configuration.

Best Answer

Try finding config file with rpm -q memcached -c

If you have init script for memcached in /etc/init.d/ then you can see if it's try to load any configuration file if not or there is no init script you can create one.

This is example of init script for my memcached:

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

# Standard LSB functions
#. /lib/lsb/init-functions

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

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

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

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

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

RETVAL=0
prog="memcached"
pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
lockfile=${LOCKFILE-/var/lock/subsys/memcached}

start () {
        echo -n $"Starting $prog: "
        # Ensure that $pidfile directory has proper permissions and exists
        piddir=`dirname $pidfile`
        if [ ! -d $piddir ]; then
                mkdir $piddir
        fi
        if [ "`stat -c %U $piddir`" != "$USER" ]; then
                chown $USER $piddir
        fi

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

restart () {
        stop
        start
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} memcached
        RETVAL=$?
        ;;
  restart|reload|force-reload)
        restart
        ;;
  condrestart|try-restart)
        [ -f ${lockfile} ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
        RETVAL=2
        ;;
esac

exit $RETVAL

You can see that init script is trying to load config file it this place:

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

You can try copy this script and make config file /etc/sysconfig/memcached This is sample config:

PORT="66266"
USER="memcached"
MAXCONN="1024"
CACHESIZE="2048"
OPTIONS=""

After making init script(don't forget to chmod +x /etc/init.d/memcached) and config file you will be able to start memcached with service memcached start

Afterwards you can add this init script to chkconfig and then system will start memcached automaticly after restart.

chkconfid -add memcached
chkconfig memcached on