ScaleScaleScaleScale

Tips / Nginx


How to configure multiple memcached instances

memcached

Running multiple memcached instances on one server is possile and takes only 5 minutes of your time, you need to do some hack into the init.d script and memcached configuration file but its a safe way to have two (or even more if you want) instances of memcached running on the same server. In this case I am using CentOS 6.x.

Install memcached and the php-memcache extension

yum install memcached
pecl install memcached

Replace “memcached” references with “memcached2”

cp /etc/init.d/memcached /etc/init.d/memcached2 -fv
cp /etc/sysconfig/memcached /etc/sysconfig/memcached2 -fv
cp /var/run/memcached/memcached.pid cp /var/run/memcached/memcached2.pid
touch /var/lock/subsys/memcached2
replace "/etc/sysconfig/memcached" "/etc/sysconfig/memcached2" -- /etc/init.d/memcached2
replace "/var/run/memcached/memcached.pid" "/var/run/memcached/memcached2.pid" -- /etc/init.d/memcached2
replace "/var/lock/subsys/memcached2" "/var/lock/subsys/memcached2" -- /etc/init.d/memcached2

Set the new port at your memcached2 service:

replace "11211" "11311" -- /etc/init.d/memcached2
replace "11211" "11311" -- /etc/sysconfig/memcached2

Now at php.ini configuration, you should also need to set both ports:

extension=memcache.so
memcache.default_port=11211,11311

Now restart your webserver to apply changes:

service httpd restart

And if you are using php-fpm with Nginx:

service php-fpm restart

Now start both memcached services:

/etc/init.d/memcached start
/etc/init.d/memcached2 start
[root@my.server.com:~]/etc/init.d/memcached restart
Stopping memcached: [ OK ]
Starting memcached: [ OK ]

[root@my.server.com:~]/etc/init.d/memcached2 restart
Stopping memcached: [ OK ]
Starting memcached: [ OK ]

And then check at the netstat output to see if the two instances are listening on different ports:

[root@my.server.com:~]netstat -nalp | grep memcache | grep LISTEN -i
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 5567/memcached
tcp 0 0 0.0.0.0:11311 0.0.0.0:* LISTEN 7225/memcached
tcp 0 0 :::11211 :::* LISTEN 5567/memcached
tcp 0 0 :::11311 :::* LISTEN 7225/memcached

All done, there you have two memcached instances running without problems =)

Popular search terms:

  • relaunch memcached instance
  • memcached config mutiple server access
  • running multiple memcache services
  • config php-fpm with memcached server
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • stan

    If you READ the contents of /etc/init.d/memcached you’ll see that it is even easier to have multiple instances than your method, simply by copying the /etc/memcached.conf file into several /etc/memcached_.conf files.

    This eliminates the need for messing around with any startup scripts, pid files etc.

  • I just wanted to point out that the replace command appears to be provided my mysqld-server package which means this install guide will not work very well unless you have MySQL installed also.

  • Alex

    thanks, useful !

  • Tested and worked @ centos 6.6 – wordpress & w3tc

    Thank you ^_^