Memcached is an open source, high-performance, distributed memory object caching system, generic in nature. It's used for speeding up dynamic web application, reduce database load average. Caching queried data for a certain amount of time. Once the user requested that data, memcache retrieving and displaying the cached information.
The Memcached package doesn’t exist in the standard CentOS repository so download the repo from the EPELs to choose rpm packages as per your OS version and install it.
Centos 5 - 32 Bit:
# rpm -Uhv https://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
Centos 5 64 Bit:
#rpm -Uhv https://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
Centos 6 32 Bit:
# rpm -Uhv https://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Centos 6 64 Bit:
# rpm -Uhv https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:epel-release ########################################### [100%]
warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:epel-release ########################################### [100%]
Install memcached using YUM command:
# yum install memcached
...
memcached x86_64 1.4.4-3.el6
libevent x86_64 1.4.13-4.el6
...
Installed:
memcached.i386 0:1.4.5-1.el5
Dependency Installed:
libevent.i386 0:1.4.13-1
...
memcached x86_64 1.4.4-3.el6
libevent x86_64 1.4.13-4.el6
...
Installed:
memcached.i386 0:1.4.5-1.el5
Dependency Installed:
libevent.i386 0:1.4.13-1
Next, install the memcache PHP extension,
# wget http://pecl.php.net/get/memcache-3.0.8.tgz
# tar -xf memcache-3.0.8.tgz
# cd memcache-3.0.8
# phpize && ./configure --enable-memcache && make
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
...
...
# make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20100525/
# tar -xf memcache-3.0.8.tgz
# cd memcache-3.0.8
# phpize && ./configure --enable-memcache && make
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
...
...
# make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20100525/
After Installed memcache to enable it in PHP,
# whereis php.ini
php: /usr/bin/php /usr/lib/php /usr/lib/php.ini /usr/local/bin/php /usr/local/lib/php
php: /usr/bin/php /usr/lib/php /usr/lib/php.ini /usr/local/bin/php /usr/local/lib/php
Add the extension in the php.ini file,
# /usr/lib/php.ini
extension="memcache.so"
extension="memcache.so"
Now, restart the memcached and httpd service,
# /etc/init.d/memcached restart
Stopping memcached: [ OK ]
Starting memcached: [ OK ]
Stopping memcached: [ OK ]
Starting memcached: [ OK ]
To find out whether the service is working using the netstat command.
# netstat -an | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN
tcp 0 0 :::11211 :::* LISTEN
udp 0 0 0.0.0.0:11211 0.0.0.0:*
udp 0 0 :::11211 :::*
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN
tcp 0 0 :::11211 :::* LISTEN
udp 0 0 0.0.0.0:11211 0.0.0.0:*
udp 0 0 :::11211 :::*
Memcached service startup at server booting time,
# chkconfig --level 2345 memcached on
# chkconfig --list | grep memcached
memcached 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# chkconfig --list | grep memcached
memcached 0:off 1:off 2:on 3:on 4:on 5:on 6:off
How to configure Memcached:
Configuration file available in /etc/sysconfig/memcached
# vi /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
MAXCONN : Maximum simultaneous connections, by default is 1024
CACHESIZE : Maximum Memcache storage size, by default size is 64 MB
OPTIONS : Set Server IP Address, by default INADDR_ANY but there is no other way to secure
To check Memcached configured with PHP:
# php -i | grep memcache
memcache
memcache support => enabled
memcache.allow_failover => 1 => 1
memcache.chunk_size => 32768 => 32768
memcache.compress_threshold => 20000 => 20000
memcache.default_port => 11211 => 11211
memcache.hash_function => crc32 => crc32
memcache.hash_strategy => consistent => consistent
memcache.lock_timeout => 15 => 15
memcache.max_failover_attempts => 20 => 20
memcache.protocol => ascii => ascii
memcache.redundancy => 1 => 1
memcache.session_redundancy => 2 => 2
Registered save handlers => files user memcache
memcache
memcache support => enabled
memcache.allow_failover => 1 => 1
memcache.chunk_size => 32768 => 32768
memcache.compress_threshold => 20000 => 20000
memcache.default_port => 11211 => 11211
memcache.hash_function => crc32 => crc32
memcache.hash_strategy => consistent => consistent
memcache.lock_timeout => 15 => 15
memcache.max_failover_attempts => 20 => 20
memcache.protocol => ascii => ascii
memcache.redundancy => 1 => 1
memcache.session_redundancy => 2 => 2
Registered save handlers => files user memcache
To get general stats about the Memcached:
# memcached-tool 192.168.0.1:11211 stats
192.168.0.1:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 0
bytes_read 14
bytes_written 770
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 0
cmd_set 0
conn_yields 0
connection_structures 11
curr_connections 10
curr_items 0
decr_hits 0
decr_misses 0
delete_hits 0
delete_misses 0
evictions 0
get_hits 0
get_misses 0
incr_hits 0
incr_misses 0
limit_maxbytes 134217728
listen_disabled_num 0
pid 832151
pointer_size 64
rusage_system 0.005999
rusage_user 0.002999
threads 4
time 1399099463
total_connections 12
total_items 0
uptime 40
version 1.4.4
192.168.0.1:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 0
bytes_read 14
bytes_written 770
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 0
cmd_set 0
conn_yields 0
connection_structures 11
curr_connections 10
curr_items 0
decr_hits 0
decr_misses 0
delete_hits 0
delete_misses 0
evictions 0
get_hits 0
get_misses 0
incr_hits 0
incr_misses 0
limit_maxbytes 134217728
listen_disabled_num 0
pid 832151
pointer_size 64
rusage_system 0.005999
rusage_user 0.002999
threads 4
time 1399099463
total_connections 12
total_items 0
uptime 40
version 1.4.4
Do you want to test the memcached configuration in the PHP file.
<?php
// Connection constants
define('MEMCACHED_HOST', 'localhost');
define('MEMCACHED_PORT', '11211');
// Connection creation
$memcache = new Memcache;
$memcache->connect('localhost',11211) or die ("Could not connect");
$product = array('id' => "11", 'name' =>"linuxfaq");
$key = "key1";
$memcache->set("11",$product);
$product1 = $memcache->get("11");
echo "<pre>";print_r($product1);echo "</pre>";
?>
// Connection constants
define('MEMCACHED_HOST', 'localhost');
define('MEMCACHED_PORT', '11211');
// Connection creation
$memcache = new Memcache;
$memcache->connect('localhost',11211) or die ("Could not connect");
$product = array('id' => "11", 'name' =>"linuxfaq");
$key = "key1";
$memcache->set("11",$product);
$product1 = $memcache->get("11");
echo "<pre>";print_r($product1);echo "</pre>";
?>
This is perfect, thank you so much!
Thank you for your comments...