Monday, April 23, 2012

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

How do I install memcached server under Red Hat Enterprise Linux 6.x server running on IBM hardware to cache query for my php 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 : Perl 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

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
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.
  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 sample update file:
 
PORT="11211"
USER="memcached"
MAXCONN="4096"
CACHESIZE="512"
OPTIONS="-l 192.168.1.15"
 

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?

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:
# 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.

No comments:

Post a Comment