Install and use memcached in Centos7
Introduction to memcached
Memcached is a high-performance distributed memory object Cache System for dynamic Web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, thus improving the speed of dynamic and database-driven websites. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can write it in any language and communicate with the daemon through memcached protocol.
For the enhanced version of memcached: Redis, I introduced its installation and usage in the first two blogs: redis installation and deployment on Centos7, php-redis extension and simple use on Centos7, if you want to know the similarities and differences between memcached and redis and under what circumstances should we choose a cache system, the following blog is a good inspiration for everyone: Cache Technology PK: Choose Memcached or Redis ?, I would like to thank the author of this blog for his incisive analysis.
In this blog, I will guide you to install and use memcached under Centos7.
Step 1: Install memcached
Memcached installation includes: 1. Server Installation; 2. Client installation
1. Server installation:
Here, because the compilation and installation of memcached server is too complicated, I chose the dependency management tool yum to install memcached Server:
[root@localhost /]# yum install -y memcached
-Y indicates automatic response, that is, all dependent packages required are installed by default.
After this step, the installation is complete.
Let's start memcached:
[root@localhost /]# /usr/bin/memcached -b -l 127.0.0.1 -p 11211 -m 150 -u root
-B daemon mode (the program is still running after exiting the terminal window),-l specifies the IP address 127.0.0.1,-p specifies the port number 11211, and-m specifies the memory allocated for memcached (unit: m),-u specifies which user to use to start memcached
Check whether memcached is running:
[Root @ localhost/] # ps-ef | grep memcached // or [root @ localhost/] # pstree-p | grep memcached
If the memcached process exists, it indicates that the memcached server has been installed successfully.
2. Client installation
The client is installed in two steps: 1. Install the libmemcached client library; 2. Install the PHP-memcached extension for php.
Now let's download the two installation packages required for later installation.
[Root @ localhost/] # cd/usr/local/src # All my source package habits are put in this directory [root @ localhost src] # wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz # download libmemcached source package [root @ localhost src] # wget http://pecl.php.net/get/memcached-2.2.0.tgz # download memcached source package [root @ localhost src] # lslibmemcached-1.0.18.tar.gz memcached-2.2.0.tgz
1. Install the libmemcached client library:
First unzip the libmemcached-1.0.18.tar.gz, Configuration
[root@localhost src]# tar -zxvf libmemcached-1.0.18.tar.gz[root@localhost src]# cd libmemcached-1.0.18/[root@localhost libmemcached-1.0.18]# ./configure --prefix=/usr/lib/libmemcached
-Prefix: Specifies the installation directory, which will be used later.
Compilation and installation:
[root@localhost libmemcached-1.0.18]# make && make install
2. Install the PHP-memcached extension for php
First unzip the libmemcached-1.0.18.tar.gz, Configuration
[root@localhost libmemcached-1.0.18]# cd ..[root@localhost src]# tar -zxvf memcached-2.2.0.tgz[root@localhost src]# cd cd memcached-2.2.0
In this step, we will use the phpize generated when installing php to generate the configure configuration file.
[Root @ localhost memcached-2.2.0] #/usr/local/php/bin/phpize (or/usr/bin/phpize) // The specific usage depends on the directory where your phpize file is located. You should use whereis phpize to view the path. If php is installed by default, use phpize directly.
Configuration
[root@localhost memcached-2.2.0]# ./configure -with-php-config=/usr/bin/php-config --with-libmemcached-dir=/usr/lib/libmemcached --disable-memcached-sasl
-With-php-config: Specify php-config. The file is the same as the directory where phpize is located.-with-libmemcached-dir specifies the libmemcached installation directory. Just now we-prefix the directory, -disable-memcached-sasl indicates that sasl is not supported by our system. h
Compilation and Installation
[root@localhost memcached-2.2.0]# make && make install
If the installation is successful, the prompt is: Installing shared extension:/usr/local/php/lib/extensions/no-debug-non-zts-20160524/and other class information
Next, edit the php configuration file php. ini, you can use whereis php. ini to view the location (in/etc/php. ini) to add the php-memcached extension to the configuration file.
Add the following content to php. ini:
extension=memcached.so
Step 2: restart the apache server to make the configuration take effect.[root@localhost memcached-2.2.0]# systemctl restart httpd.service
After the restart, check whether the php-memcached extension has been installed.
[Root @ localhost memcached-2.2.0] # echo "<? Php echo phpinfo ()?> ">/Home/www/index. php (if the web directory is not changed here, it is in/var/www/html /)
Enter 127.0.0.1 in the address bar of the browser to view the php extension. If the extension is shown in a piece, the installation is successful:
Of course, the more convenient method is to use php-m
The above shows that we have successfully added memcached to the php extension.
Step 3: Use php for simple memcached operationsIn fact, memcached is similar to redis and MySQL. It is also a database (redis is a non-relational database, mysql is a relational database), so it is also similar, you can also use commands and php operations. You can use more memcached commands on your own.
<? Php $ m = new Memcached (); // instantiate the Memcached class $ arr = array ('2017. 0.0.1 ', 11211); $ m-> addServers ($ arr); $ m-> set ('name', 'lsgogroup', 3600 ); // set the cache value. The validity period is 3600 seconds. If the validity period is set to 0, the cache value permanently exists (before the system restarts) $ m-> get ('name'); // read the cache value $ m-> delete ('name'); // delete the cache value?>
The redis command is much more than what we mentioned above. It is only simple to use here.
This blog is basically based on the self-MOOC Network: Exploring the memcache cache at a close distance. If you want to learn more about the installation process and use memcached, you can watch these videos. I believe you will get a lot from it.