Allowing remote connection of memcache server

memcached

My memcache server is up and running on Server A.

service memcached start
memcached -d -u nobody -m 512 -p 11211 127.0.0.1
chkconfig --list | grep memcache
chkconfig memcached on

I also have a web application on Server B (different IP). How can I configure Server A for memcache to allow incoming connections from Server B?

Best Answer

By default memcached only serves the localhost as it otherwise has no protection from the wider network / internet and is insecure. As you see in the output you posted it's currently only serving connections over 127.0.0.1:

memcached -d -u nobody -m 512 -p 11211 127.0.0.1

So, you need to edit the configuration file and give it the IP address of the network interface that you want to serve. To do this head over to the directory where the config file is located. On ubuntu, this is:

cd /etc/

Then open the config file in your editor of choice:

sudo nano memcached.conf

Then scrol down until you find this:

# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 127.0.0.1

Now just replace 127.0.0.1 with the IP of your network interface and save.

Now you'll need to restart the memcached server:

sudo service memcached restart

Memcached will now be serving only requests received via that IP/network interface. You can test this with the following command:

ps aux | grep memcached

If everything went ok, you should see the IP you entered in the config rather than the localhost.