How to Install Memcached | Linux | CentOS 6.7

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 memcached source code installation method.

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

If you got the following error:

Error: Package: php-pecl-memcache-3.0.5-4.el6.x86_64 (base)
Requires: php(api) = 20090626
Installed: php-common-5.4.45-2.el6.remi.x86_64 (@remi)
php(api) = 20100412-x86-64
Available: php-common-5.3.3-40.el6_6.x86_64 (base)
php(api) = 20090626
Available: php-common-5.3.3-46.el6_6.x86_64 (updates)
php(api) = 20090626
Error: Package: php-pecl-memcache-3.0.5-4.el6.x86_64 (base)
Requires: php(zend-abi) = 20090626
Installed: php-common-5.4.45-2.el6.remi.x86_64 (@remi)
php(zend-abi) = 20100525-x86-64
Available: php-common-5.3.3-40.el6_6.x86_64 (base)
php(zend-abi) = 20090626
Available: php-common-5.3.3-46.el6_6.x86_64 (updates)
php(zend-abi) = 20090626
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest

go to /etc/yum.repos.d/remi.repo and change enabled=0 to enabled=1. Save file and enter again yum install memcached.x86_64 php-pecl-memcache.x86_64

name=Les RPM de remi pour Enterprise Linux 6 - $basearch

Sample outputs:

[root@code ~]# yum install memcached.x86_64 php-pecl-memcache.x86_64
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
* base:
* epel:
* extras:
* remi:
* updates:
remi | 2.9 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package memcached.x86_64 0:1.4.22-1.el6.remi will be installed
--> Processing Dependency: for package: memcached-1.4.22-1.el6.remi.x86_64
---> Package php-pecl-memcache.x86_64 0:3.0.8-2.el6.remi.5.4 will be installed
--> Running transaction check
---> Package libevent-last.x86_64 0:2.0.22-1.el6.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

Package Arch Version Repository Size
memcached x86_64 1.4.22-1.el6.remi remi 89 k
php-pecl-memcache x86_64 3.0.8-2.el6.remi.5.4 remi 100 k
Installing for dependencies:
libevent-last x86_64 2.0.22-1.el6.remi remi 198 k

Transaction Summary
Install 3 Package(s)

Total download size: 387 k
Installed size: 1.2 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): libevent-last-2.0.22-1.el6.remi.x86_64.rpm | 198 kB 00:00
(2/3): memcached-1.4.22-1.el6.remi.x86_64.rpm | 89 kB 00:00
(3/3): php-pecl-memcache-3.0.8-2.el6.remi.5.4.x86_64.rpm | 100 kB 00:00
Total 3.3 MB/s | 387 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libevent-last-2.0.22-1.el6.remi.x86_64 1/3
Installing : memcached-1.4.22-1.el6.remi.x86_64 2/3
Installing : php-pecl-memcache-3.0.8-2.el6.remi.5.4.x86_64 3/3
Verifying : php-pecl-memcache-3.0.8-2.el6.remi.5.4.x86_64 1/3
Verifying : memcached-1.4.22-1.el6.remi.x86_64 2/3
Verifying : libevent-last-2.0.22-1.el6.remi.x86_64 3/3

memcached.x86_64 0:1.4.22-1.el6.remi php-pecl-memcache.x86_64 0:3.0.8-2.el6.remi.5.4

Dependency Installed:
libevent-last.x86_64 0:2.0.22-1.el6.remi

[root@code ~]#

Configure Memcached server:

vi /etc/sysconfig/memcached

Sample outputs:


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

  • MAXCONN: Use 1024 max simultaneous connections; the default is 1024. For busy server increase this number as per requirements or setup.
  • 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).
  • 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.

This is my updated sample config file:
(change to your server ip address. 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

Verify that Memcached is running and working correctly on Linux:

pgrep memcached
netstat -tulpn | grep :11211

Sample outputs:

[root@code ~]# netstat -tulpn | grep :11211
tcp 0 0* LISTEN 8836/memcached
udp 0 0* 8836/memcached

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

memcached-tool stats

Sample outputs:

#192.168.1:11211 Field       Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 0
bytes_read 7
bytes_written 0
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 0
cmd_set 0
cmd_touch 0
conn_yields 0
connection_structures 6
crawler_reclaimed 0
curr_connections 5
curr_items 0
decr_hits 0
decr_misses 0
delete_hits 0
delete_misses 0
evicted_unfetched 0
evictions 0
expired_unfetched 0
get_hits 0
get_misses 0
hash_bytes 524288
hash_is_expanding 0
hash_power_level 16
incr_hits 0
incr_misses 0
libevent 2.0.22-stable
limit_maxbytes 67108864
listen_disabled_num 0
lrutail_reflocked 0
malloc_fails 0
pid 8836
pointer_size 64
reclaimed 0
reserved_fds 20
rusage_system 0.011998
rusage_user 0.006998
threads 4
time 1448003324
total_connections 6
total_items 0
touch_hits 0
touch_misses 0
uptime 137
version 1.4.22
[root@code ~]#

Run memchahced on server boot:

chkconfig --level 2345 memcached on
chkconfig memcached on

Configure Firewall to protect 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:

iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW  -m iprange --src-range 192.168.1 -j ACCEPT
iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW -m iprange --src-range 192.168.1 -j ACCEPT

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.