How to set up memcached to use unix socket?

You may find that just setting the socket path doesn’t work. Memcached drops privileges before it creates its socket, though after it’s written its PID. It’s common to put the socket in /var/run (e.g. as mysql does), but only root can write there, so create /var/run/memcached and chown it to nobody, then set /var/run/memcached/memcached.sock as the socket path. Writing it to /tmp would work too, but by its very nature that could get trashed; sticking it in /var/run is a bit more permanent.

You can check it’s working by using netcat to connect to it:

nc -U /var/run/memcached/memcached.sock

Just type stats at the blank prompt; if it’s working you’ll get a load of output.

Leave a Comment