Memcached is a distributed, high-performance, in-memory caching system that is primarily used to speed up sites that make heavy use of databases. Memcached is likewise used to cache whole database tables and queries to enhance execution of database. Memcached keeps running in memory and is in this way entirely quick, since it doesn’t have to write data to disk.
See Also: Install phpMemcachedAdmin on Linux Install Memcached on Ubuntu 14.04 /15.04
The easiest way to introduce Memcached is through a package manager, for example, yum. However, Memcached is not accessible from the default collection of packages, so the first thing we have to do is add a new repository server so we can install Memcached through yum.
Step #1 Install Repository
The fastest and easiest way to install and enable EPEL repository using YUM.
CentOS/RHEL 7 64bit:
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
CentOS/RHEL 6 64bit:
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
CentOS/RHEL 6 32 Bit:
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
CentOS/RHEL 5 64bit:
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
CentOS/RHEL 5 32 Bit:
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
Step #2 Install Memcached
Now as simple as running just following command to install Memcached:
# yum install memcached -y
Step #3 Configure Memcached
After installing Memcached, Open the configuration file of Memcached.
# vim /etc/sysconfig/memcached
Memcached isn’t protected with a password or username so anyone can access it through port 11211. So we are going to allow only for our server to access it. Do changes as below:
Note: Detail of above parameters PORT: The port used by memcached to run. USER: The start-up daemon for memcached service. MAXCONN: The value used to set max simultaneous connections to 1024. CACHESIZE: Set cache size memory to 2048. OPTIONS: Set IP address of server.
After the changes restart Memcached.
# systemctl restart memcached
# /etc/init.d/memcached restart
Step #4 Start on Boot
Now start Memcached on boot, so that If server reboot then Memcached service start automatically.
# systemctl enable memcached
# chkconfig memcached on
Step #5 Verify Memcached
Verify memcached using netstat command:
# netstat -tulpn | grep 11211
Check the stats of the server using memcached-tool.
# echo "stats settings" | nc localhost 11211
Step #6 Install Memcached PHP extension
Now we have installed Memcached server on our system. In any case, to use Memcached service using php scripts we have to install Memcache php augmentation.
# yum install php-pecl-memcache
Install Memcached Perl Library
If require install perl library for Memcached.
# yum install perl-Cache-Memcached
Install Memcached Python Library
If require install python library for Memcached.
# yum install python-memcached
After installing require packages restart the Apache service to reflect changes.
# systemctl restart httpd
# /etc/init.d/httpd restart
Cache MySQL Queries with Memcached
It isn’t a simple assignment for everything, you need to use API’s to modify your PHP codes to empower MySQL caching. You can discover the samples codes at Memcache with MySQL and PHP.