Install Memcached on Fedora 16/15, CentOS/Red Hat (RHEL) 6.2/5.7

Memcached is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load. Memcached is a very useful also in other cases, than only dynamic web applications.

This guide explains howto install Memcached 1.4.4/1.4.5/1.4.7 stable version of distributed memory object caching system on Fedora 16/15/14/13/12 and CentOS 6.2/6.1/6/5.7, Red Hat (RHEL) 6.2/6.1/6/5.7. Fedora 16/15/14/13/12, CentOS 6 and Red Hat (RHEL) 6 has Memcached 1.4.5 as default so extra repositories is not needed.

Install Memcached 1.4.4/1.4.5/1.4.7 on Fedora 16/15/14/13/12, CentOS/Red Hat (RHEL) 6.2/6.1/6/5.7

Install Remi repository (not needed on Fedora 16, Fedora 15, Fedora 14, Fedora 13, Fedora 12 and RHEL 6):

## Remi Dependency on CentOS and Red Hat (RHEL)
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
 
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

Install Memcached package

Fedora 16/15/14/13/12, CentOS/Red Hat (RHEL) 6.2/6.1/6

yum install memcached

CentOS/Red Hat (RHEL) 5.7

yum --enablerepo=remi install memcached

Configure memcached

Most important value to configure is CACHESIZE, which is cache size on Megabytes. Example Following configuration use 512 Mb memory for Memcached

nano -w /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="512"
OPTIONS=""

Start Memcached

# Set Memcached to start automatically on boot
chkconfig memcached on
# Start Memcached
/etc/init.d/memcached start
## OR ##
service memcached start

Check that Memcached is Running and Working

echo stats | nc localhost 11211
STAT pid 7599
STAT uptime 10
STAT time 1265288542
STAT version 1.4.4
STAT pointer_size 32
STAT rusage_user 0.003999
STAT rusage_system 0.052991
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 6
STAT bytes_written 0
STAT limit_maxbytes 536870912
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
END
 
# Try to get some value
echo get some_value | nc localhost 11211
END
 
# Not found, but check the stats again
echo stats | nc localhost 11211
STAT pid 7599
STAT uptime 10
STAT time 1265288542
STAT version 1.4.4
[...]
STAT cmd_get 1
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 1
STAT delete_misses 0
[...]
STAT evictions 0
END

Everything working nice. Then it’s time to test memcache example with some web application.

Open Memcached Port (11211) on Iptables Firewall (If the Memcached will also be used other local servers)

Edit /etc/sysconfig/iptables file:

nano -w /etc/sysconfig/iptables

Add following line before COMMIT:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT

Restart Iptables Firewall:

service iptables restart
## OR ##
/etc/init.d/iptables restart

Test remote connection:

echo stats | nc memcache_host_name_or_ip 11211

Install Memcache and/or Memcached PHP Module on Fedora, CentOS and Red Hat (RHEL)

More information about Memcache Module.
More information about Memcached Module.

Install Memcache Module and PHP

yum install php php-pecl-memcache

Install Memcached Module and PHP

yum install php php-pecl-memcached

Restart Web server

/etc/init.d/httpd restart
## OR ##
service httpd restart
Follow If Not True Then False Updates!
  1. Install Apache/PHP 5.3.10 on Fedora 16/15, CentOS/Red Hat (RHEL) 6.2/5.7
  2. Install MongoDB 2.0.1 on Fedora 16/15, CentOS/Red Hat (RHEL) 6/5.7
  3. Install VirtualBox 4.1.8 on Fedora 16/15, CentOS/Red Hat (RHEL) 6.2/5.7
  4. Install OpenOffice 3.3.0/3.4.0 on Fedora 16/15, CentOS/RHEL 6.2/5.7
  5. Install Sun/Oracle Java JDK/JRE 7u2 on Fedora 16/15, CentOS/RHEL 6.2/5.7

14 Comments

  1. The above instructions work fine for me with Fedora 12. Fedora 13 wasn’t working. /usr/lib/php/modules/memcache.so was missing. Copying that file from Fedora 12 installation seems to work, but I wonder why it was missing. rpm -ql memcached doesn’t show memcache.so.

  2. Hi Charles,

    I verified again that the this guide is working also with Fedora 13.

    If you want install Memcache PHP module then you need to install following package:

    yum install php-pecl-memcache

    Then you get /usr/lib/php/modules/memcache.so file right place.

    I added this also on end of this guide. :)

  3. Nice site…and nice work ..
    Keep up…..

  4. Hi,
    thank for this nice tutorial for installing memcached, i’d to know what the best memory size for memcached on vps with 512MB of ram?

    • Of course, it depends on the intended use and the fact that whatever else the server is running, but maybe about 64-192 MB is good for memcached.

  5. no response from: rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

    site appears to be dead

    • Hi jeff,

      For me it’s working normally…maybe it was down when you try to install it. So please try again.

  6. Thanks!

  7. cant installl
    —> Package perl-common-sense.i386 0:3.0-1.el5.rf set to be updated
    –> Finished Dependency Resolution
    perl-AnyEvent-5.240-1.el5.rf.noarch from rpmforge has depsolving problems
    –> Missing Dependency: perl(Net::SSLeay) >= 1.33 is needed by package perl-AnyEvent-5.240-1.el5.rf.noarch (rpmforge)
    Error: Missing Dependency: perl(Net::SSLeay) >= 1.33 is needed by package perl-AnyEvent-5.240-1.el5.rf.noarch (rpmforge)
    You could try using –skip-broken to work around the problem
    You could try running: package-cleanup –problems
    package-cleanup –dupes
    rpm -Va –nofiles –nodigest

    • Hi onel0ve,

      It seems that the problem is caused by rpmforge repo packages.

      Could you first post output of following command:

      yum repolist
  8. Hi Jeff,
    Thanks a lot, the installation is done, class Memcache work fine but the class Memcached is not working, i get the message : Fatal error: Class ‘Memcached’ not found !
    have you any idea about that

    • Hi HaYsSeN,

      If you want to install class Memcached then you need to install php-pecl-memcached package too:

      yum install php-pecl-memcached

      I added this to guide too.

  9. Hi. Loved this very clear instruction.

    I already have Memcached 1.2.0 installed:


    > memcached -h
    memcached 1.2.0
    -p port number to listen on
    -s unix socket path to listen on (disables network support)
    ....

    I have the usual CentOS Base, EPEL, and even the REMI repositories installed now.


    > yum repolist
    Loaded plugins: fastestmirror, priorities
    Loading mirror speeds from cached hostfile
    * epel: fedora-epel.mirror.lstn.net
    * remi: rpms.famillecollet.com
    repo id repo name status
    addons CentOS-5 - Addons enabled : 0
    base CentOS-5 - Base enabled : 3,566
    centosplus CentOS-5 - Plus enabled : 57
    contrib CentOS-5 - Contrib enabled : 0
    epel Extra Packages for Enterprise Linux 5 - enabled : 6,851
    extras CentOS-5 - Extras enabled : 266
    remi Les RPM de remi pour Enterprise Linux 5 enabled : 641
    updates CentOS-5 - Updates enabled : 569
    repolist: 11,950

    But when I do a:


    yum --enablerepo=remi update memcached

    It somehow updates it to “1.4.5″. So I restart memcached with “service memcached restart”.


    > service memcached restart
    Stopping memcached: [FAILED]
    Starting memcached: [ OK ]

    Then when I do “memcached -h” again, I still see 1.2.0.

    What am I missing?

    Thanks.

    • Hi Phoenix,

      Nice to see you here!

      Could you post output of following command:

      rpm -qa memcached

      Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Bear