RHEL / CentOS 6 Linux Install Memcached High Performance Distributed Memory Object Cache Server

by on January 24, 2012 · 8 comments· LAST UPDATED December 16, 2013

in , ,

How do I install memcached server under Red Hat Enterprise Linux or CentOS Linux version6.x server running on IBM hardware to cache query for my php/perl/python based apps?

memcached is a high-performance, distributed memory object caching server, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. You need to install the following packages:
Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
Requirements
Estimated completion time10m

  • memcached : High Performance, Distributed Memory Object Cache server package.
  • 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.

Memcached is used by many high traffic sites including YouTube, Reddit, Facebook, Twitter, and many others.Google App Engine, AppScale and Amazon Web Services also offer a memcached service through an API. Memcached is also supported by popular open source projects and CMSs such as Drupal, Joomla, WordPress, and many more.

WARNING! These examples only works with RHEL / CentOS Linux version 6.x or above. For RHEL 5.x and older version use memcached source code installation method.

Task: Install memcached on CentOS/RHEL

Open a terminal or login to remote server using ssh client. Type the following yum command to install the same:
# yum install memcached.x86_64 php-pecl-memcache.x86_64
OR
# yum install memcached php-pecl-memcache
Sample outputs:

 
Loaded plugins: product-id, rhnplugin, subscription-manager
Updating certificate-based repositories.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package memcached.x86_64 0:1.4.4-3.el6 will be installed
--> Processing Dependency: libevent-1.4.so.2()(64bit) for package: memcached-1.4.4-3.el6.x86_64
---> Package php-pecl-memcache.x86_64 0:3.0.5-3.el6 will be installed
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-memcache-3.0.5-3.el6.x86_64
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-memcache-3.0.5-3.el6.x86_64
--> Running transaction check
---> Package libevent.x86_64 0:1.4.13-1.el6 will be installed
---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package              Arch      Version           Repository               Size
================================================================================
Installing:
 memcached            x86_64    1.4.4-3.el6       rhel-x86_64-server-6     68 k
 php-pecl-memcache    x86_64    3.0.5-3.el6       rhel-x86_64-server-6     60 k
Installing for dependencies:
 libevent             x86_64    1.4.13-1.el6      rhel-x86_64-server-6     66 k
 php-pear             noarch    1:1.9.4-4.el6     rhel-x86_64-server-6    393 k
 
Transaction Summary
================================================================================
Install       4 Package(s)
 
Total download size: 587 k
Installed size: 0
Is this ok [y/N]: y
Downloading Packages:
(1/4): libevent-1.4.13-1.el6.x86_64.rpm                  |  66 kB     00:00
(2/4): memcached-1.4.4-3.el6.x86_64.rpm                  |  68 kB     00:00
(3/4): php-pear-1.9.4-4.el6.noarch.rpm                   | 393 kB     00:00
(4/4): php-pecl-memcache-3.0.5-3.el6.x86_64.rpm          |  60 kB     00:00
--------------------------------------------------------------------------------
Total                                           2.4 MB/s | 587 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : 1:php-pear-1.9.4-4.el6.noarch                                1/4
  Installing : libevent-1.4.13-1.el6.x86_64                                 2/4
  Installing : memcached-1.4.4-3.el6.x86_64                                 3/4
  Installing : php-pecl-memcache-3.0.5-3.el6.x86_64                         4/4
Installed products updated.
 
Installed:
  memcached.x86_64 0:1.4.4-3.el6     php-pecl-memcache.x86_64 0:3.0.5-3.el6
 
Dependency Installed:
  libevent.x86_64 0:1.4.13-1.el6          php-pear.noarch 1:1.9.4-4.el6
 
Complete!
 

How do I configure Memcached server?

Edit /etc/sysconfig/memcached file, enter:
# vi /etc/sysconfig/memcached
Sample outputs:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="

For busy servers you need to increase the values as follows:

  1. MAXCONN: Use 1024 max simultaneous connections; the default is 1024. For busy server increase this number as per requirements or setup.
  2. CACHESIZE: Use 64 MB memory max to use for object storage; the default is 64 megabytes. For busy server you can set it to 512MB or 1GB (1024).
  3. OPTIONS: You can set server IP address (listen on address) here 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.

Here is my updated sample config file:

 
PORT="11211"
USER="memcached"
MAXCONN="4096"
CACHESIZE="512"
OPTIONS="-l 192.168.1.15"
 

Save and close the file.

How do I start / stop / restart Memcached server?

Type the following commands:
# /etc/init.d/memcached start
# /etc/init.d/memcached stop
# /etc/init.d/memcached restart
# /etc/init.d/memcached status

OR run a System V init script using service command
# /sbin/service memcached start
# /sbin/service memcached stop
# /sbin/service memcached restart
# /sbin/service memcached status

How do I verify that Memcached is running and working correctly on Linux?

Type the following command to see if it is running or not:
# pgrep memcached
# netstat -tulpn | grep :11211

Sample outputs:

tcp        0      0 192.168.1.15:11211           0.0.0.0:*                   LISTEN      24964/memcached
udp        0      0 192.168.1.15:11211           0.0.0.0:*                               24964/memcached

Use the memcached-tool to get general stats about the server:
# memcached-tool 192.168.1.15:11211 stats
Sample outputs:

#192.168.1.15:11211 Field       Value
         accepting_conns           1
               auth_cmds           0
             auth_errors           0
                   bytes     5481902
              bytes_read     8242409
           bytes_written    26023492
              cas_badval           0
                cas_hits           0
              cas_misses           0
               cmd_flush           0
                 cmd_get        9042
                 cmd_set        4469
             conn_yields           0
   connection_structures          48
        curr_connections          47
              curr_items        3197
               decr_hits           0
             decr_misses           0
             delete_hits           6
           delete_misses         112
               evictions           0
                get_hits        7490
              get_misses        1552
               incr_hits           0
             incr_misses           0
          limit_maxbytes   536870912
     listen_disabled_num           0
                     pid       24964
            pointer_size          64
           rusage_system    0.149977
             rusage_user    0.043993
                 threads           4
                    time  1327395688
       total_connections         171
             total_items        3226
                  uptime         198
                 version       1.4.4

How do I configure Firewall to protect my Memcached server?

Make sure iptables only allows access to your own servers. Edit /etc/sysconfig/iptables and append the following rules or add it as follows to your shell scripts:

 
## add to your shell script ##
## assumption default INPUT policy is set to DROP ##
## only accept connection to tcp/udp port 11211 if ip is between 192.168.1.10 and 192.168.1.15 ##
iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW  -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT
iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW  -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT
 

See our RHEL / CentOS firewall tutorial here and here for more information about setting up firewall.

How Do I Cache MySQL Queries Using Memcached?

You need to modify your php code using APIs. You can find examples and code samples here. Alternately, you can install memcached extensions such as WordPress extension/plugins if you are using worpdress as CMS.

References:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 8 comments… read them below or add one }

1 unwiredtech January 24, 2012 at 10:22 am

Thank you for this tut. I am going to implement this one on our site.

Reply

2 Ramy Allam January 28, 2012 at 11:31 am

Thanks a lot , I hope to add a tutorial for repcached and implementation of master slave replication for memcache

Reply

3 Santi Saez February 23, 2012 at 7:10 am

Hey guys,

Actually RHEL-6 installs and outdated version of memcached (1.4.4, released ~2 years ago), I suggest using at least memcached 1.4.10, starting this release there is a big performance and scalability improvements (see developers release notes).

Using PowerStack repository you can easily run latest memcached stable version in your CentOS and RHEL boxes (4, 5 and 6 releases supported, i686 + x86_64), last LAMP stack stable versions and other key packages like: nginx, HAproxy, node.js, redis, Varnish, etc.

All code + .spec files are available on GitHub, check out the wiki page for installation instructions:

http://powerstack.org

BTW: we’re planning to upgrade to libevent 2 with the upcoming memcached releases :)

Regards,

Reply

4 BiBi April 4, 2012 at 6:03 pm

Thanks very much, this website is very usefull. Thanks again.

Reply

5 Mac January 24, 2013 at 1:45 pm

The powerstack repo what other packages are available?

Will the powerstack repo have updates for the installed packages as well, so you can keep your server(s) up to date?

thanks

Reply

6 Alper June 2, 2013 at 9:57 pm

:S

checking build system type… Invalid configuration `x86_64-unknown-linux-‘: machine `x86_64-unknown-linux’ not recognized
configure: error: /bin/sh ./config.sub x86_64-unknown-linux- failed

Reply

7 Shane November 14, 2013 at 5:46 am

Thanks for the tutorial.

The version I have running is 1.4.4, how do I upgrade to latest version? What commands would I need to type in ssh to make this happen?

Reply

8 Nix Craft December 16, 2013 at 12:23 pm

You can not update to new version. Your best choice to to uninstall package provided by Red hat and install the latest memcached using source code.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: