≡ Menu

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

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:

  • 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 yum install memcached.x86_64 php-pecl-memcache.x86_64
# netstat -tulpn | grep :11211
Sample outputs:

tcp        0      0 *                   LISTEN      24964/memcached     
udp        0      0 *                               24964/memcached

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

# 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 and ##
iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW  -m iprange --src-range -j ACCEPT
iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW  -m iprange --src-range -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.


Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 9 comments… add one }
  • unwiredtech January 24, 2012, 10:22 am

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

  • Ramy Allam January 28, 2012, 11:31 am

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

  • Santi Saez February 23, 2012, 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:


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


  • BiBi April 4, 2012, 6:03 pm

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

  • Mac January 24, 2013, 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?


  • Alper June 2, 2013, 9:57 pm


    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

  • Shane November 14, 2013, 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?

    • Nix Craft December 16, 2013, 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.

  • Friend November 1, 2015, 4:39 pm

    Shouldn’t it be the memcached php module installed if your using memcached? Your install command reflects the php-memcache module not memcached and there are differences so it would be smart to install the proper php module.

    sudo yum search php-pecl-memcache

    php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
    php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">