Control memcached from the command line
memcached is a general-purpose distributed memory caching system originally developed by Danga Interactive for LiveJournal but now used by many other sites. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Here we describe the options available from the command line to control a memcached instance via unix socket or IP:port.
Memcached can be set up to either take control commands via an IP address and port or via a unix socket.
When listening via IP:port by default memcached listens for connections on port
11211 and accepts connections from
INADDR_ANY. To prevent possibly malicious access you may want to consider restricting connections to an internal or firewalled connection. Alternatively you can set memcached to only listen for connections on
localhost - i.e. the server running the instance of memcached using
To start up an instance of memcached listening on
11211 only use the following:
memcached -d -m memory -l 127.0.0.1
memoryis the maximum number of megabytes of memory you want memcached to use.
If you want to specify a different port to listen on use the
Alternatively you can use unix sockets to communicate directly with the memcached instance. In this case commands can only be sent from a login shell that has access to the socket file - for example if you ssh into the server hosting it.
To start up memcached using unix sockets you would use the following command:
memcached -d -m memory -s ~/memcached.sock
This will create a file named
memcached.sock in your home directory to communicate through.
Memcached comes with a number of useful management commands. Of these the most frequently useful are
flush_all and the stats commands including
stats items and
To send a command to memcached you can use
nc (netcat). The general form for all commands is:
echo "command" | nc 127.0.0.1 11211
Or for socket connections:
echo "command" | nc -U ~/memcached.sock
To flush the entire contents of your memcached instance you could issue the
flush_all command with one of the following:
echo "flush_all" | nc 127.0.0.1 11211 echo "flush_all" | nc -U ~/memcached.sock
[user@web37 ~]$ echo "flush_all" | nc -U ~/memcached.sock OK [user@web37 ~]$
Statistics and state
You can use the stats commands to get information about the state of your memcached instance. For example:
echo "stats" | nc 127.0.0.1 11211 echo "stats" | nc -U ~/memcached.sock
Will output a list of stats for the current instance. Additional information can be found using
stats items to give total stats about items stored in the cached and
stats slabs which provides more information including performance hints.
- Get a list of all processes not run by you
- Bash Command Substitution
- Collaborate in the shell with screen (multiuser)
- Control Console with SysReq
- Use screen to manage persistent sessions
Get my latest Python projects, tips & tutorials direct to your Inbox.