Memcached is a high-performance memory object caching system, very popular in speeding up dynamic web applications by alleviating database load. It's based on libevent to scale to any size needed, and is specifically optimized to avoid swapping and always use non-blocking I/O.

Here is an example shows how to install memcached server under Red Hat Enterprise Linux or CentOS Linux version6.x server.

Package installation

The memcached package is available on CentOS and RHEL base repository, you can just run yum to install it
Here is the output from CentOS 6.7

# yum install memcached
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base:
* elrepo:
* epel:
* extras:
* remi-safe:
* updates:
Resolving Dependencies
--> Running transaction check
---> Package memcached.x86_64 0:1.4.4-3.el6 will be installed
--> Processing Dependency: for package: memcached-1.4.4-3.el6.x86_64
--> Running transaction check
---> Package libevent.x86_64 0:1.4.13-4.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
Package Arch Version Repository Size
memcached x86_64 1.4.4-3.el6 base 68 k
Installing for dependencies:
libevent x86_64 1.4.13-4.el6 base 66 k
Transaction Summary
Total download size: 134 k
Installed size: 374 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): libevent-1.4.13-4.el6.x86_64.rpm | 66 kB 00:00
(2/2): memcached-1.4.4-3.el6.x86_64.rpm | 68 kB 00:00
Total 335 kB/s | 134 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libevent-1.4.13-4.el6.x86_64 1/2
Installing : memcached-1.4.4-3.el6.x86_64 2/2
Verifying : libevent-1.4.13-4.el6.x86_64 1/2
Verifying : memcached-1.4.4-3.el6.x86_64 2/2
memcached.x86_64 0:1.4.4-3.el6
Dependency Installed:
libevent.x86_64 0:1.4.13-4.el6

There are some other packages you may want to install for you application, for example:

perl-Cache-Memcached : Perl client (library) to work with memcached server.
python-memcached : Python client (library) to work with memcached server.
php-pecl-memcache : PHP extension to work with the Memcached server.
phpMemcachedAdmin : PHP extension to manage Memcached server.

Config Memcached server

Memcached server configuration file is located at /etc/sysconfig/memcached

You may want to change the default parameters by edit /etc/sysconfig/memcached file
Default configuration:

MAXCONN="1024" # number of connections
CACHESIZE="64" # size in MB, you may want to set it to higher large busy server
OPTIONS="" # default is INADDR_ANY

Note: for OPTIONS, you can set server listening IP address so that apache/php/nginx based serer can connect to the server. By default it is set to INADDR_ANY. This is an important option to consider as there is no other way to secure the installation. Binding to an internal or firewalled network interface is suggested.

For example, if you want to specify server  listening port to, set


Start/Stop memcached server

Start memcache server is easy, just run

/etc/init.d/memcached start

or start it manually

memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS

To stop, restart, and check memcached server status , use same command with different options

#/etc/init.d/memcached start|stop|restart|status

To enable memcached server start on boot

#chkconfig --add memcached
#chkconfig --level 345 memcached on

Check, and monitor memcached server

check if memcached running or not

#/etc/init.d/memcached status
#netstat -tulpn | grep 11211 

Or use memcached-tool

# memcached-tool
Usage: memcached-tool <host[:port]> [mode]
memcached-tool display # shows slabs
memcached-tool # same. (default is display)
memcached-tool stats # shows general stats
memcached-tool dump # dumps keys and values

Note: is an example ip, change it to your memcached server ip

Setup Firewall to protect Memcached server

Make sure iptables only allows access to your own servers, put the line below into /etc/sysconfig/iptables 

-A INPUT -s -dport 11211 -j ACCEPT

Remember to change the ip and range for your own memcached server, for more iptable examples, see Linux iptables useful examples

Comments powered by CComment