Installation Operation Memcached under CentOS

Source: Internet
Author: User



Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.



Recently learned memcached, through my personal come in, with everyone to install memcached, as well as demonstrate the use of PHP extension memcached for simple operation;

1. Get Memcached

wget http://www.memcached.org/files/memcached-1.4.24.tar.gz
Install Memcached
1. Unzip tar -xf memcached-1.4.24.tar.gz;
2. Enter the directory cd memcached-1.4.24 /;
3. Configure ./configure;
4. Compile and install make && make install;
Where && means that the previous command is executed successfully before executing the following command;
After installation, the default directory of memcached is / usr / local / bin / memcached
Start memcached
Startup example: memcached -u root -d
Start parameter description:
The -d option is to start a daemon.
-m is the amount of memory allocated to Memcache, the unit is MB, and the default is 64MB.
-u is the user running Memcache, if it is currently root, you need to use this parameter to specify the user
-p <num> is to set Memcache's TCP listening port, preferably a port above 1024.
The -c option is the maximum number of concurrent connections running. The default is 1024.
-P <file> is set to save the pid file of Memcache.
PHP installation support for Memcached
There are two versions of the memcached client in php. Here is memcached. This is a new version of the client based on libmemcached.
1. Decompress the source code tar -xf libmemcached-1.0.18.tar.gz
2. Enter the source directory cd libmemcached-1.0.18 /
3.Configure./configure --prefix = / usr / local / libmemcached
4. Compile and install make && make install
Install Memcached PHP extension
1. Download the source code wget https://pecl.php.net/get/memcached-2.2.0.tgz;
2. Decompress the source code tar -xf memcached-2.2.0.tgz;
3. Enter the source directory cd memcached-2.2.0 /;
3. After the decompressed source code does not have a configuration file, the configuration file phpize needs to be generated first;
4.Source code configuration. memcached-sasl
5. Compile and install make && make install
Modify php.ini and add extension = "memcached.so".
 

 

Memcached boot mode

method one:

Append startup command in /etc/rc.d/rc.local file

/ usr / local / memcached / bin / memcached -u root -d -m 2048 -l 192.168.137.99 -p 11211 -P /tmp/memcached.pid

You can also not specify the IP, the default is this machine, such as

/ usr / local / memcached / bin / memcached -u deamon -d -m 2048 -p 11211 -P /tmp/memcached.pid

The user is preferably apache or deamon

 

Method Two:

#Write service script

1 vim /etc/init.d/memcached
Paste the following code

01 #! / Bin / sh
02 #
03 # memcached: MemCached Daemon
04 #
05 # chkconfig:-90 25
06 # description: MemCached Daemon
07 #
08 # Source function library.
09
10. /etc/rc.d/init.d/functions
11. / Etc / sysconfig / network
12
13 # [$ {NETWORKING} = "no"] && exit 0
14 # [-r / etc / sysconfig / dund] || exit 0
15 #. / Etc / sysconfig / dund
16 # [-z "$ DUNDARGS"] && exit 0
17
18 MEMCACHED = "/ usr / local / memcached / bin / memcached"
19 SERVER_IP = "192.168.137.98"
20 SERVER_PORT = "11211"
twenty one  
22 [-f $ MEMCACHED] || exit 1
twenty three  
24 start ()
25 {
26 echo -n $ "Starting memcached:"
27 daemon $ MEMCACHED -u daemon -d -m 2048 -l $ SERVER_IP -p $ SERVER_PORT -P /tmp/memcached.pid
28 echo
29}
30 stop ()
31 {
32 echo -n $ "Shutting down memcached:"
33 killproc memcached
34 echo
35}
36
37 # See how we were called.
38 case "$ 1" in
39 start)
40 start
41 ;;
42 stop)
43 stop
44 ;;
45 restart)
46 stop
47 sleep 3
48 start
49 ;;
50 *)
51 echo $ "Usage: $ 0 {start | stop | restart}"
52 exit 1
53 esac
54 exit 0
 

#Set to start the service

1 chmod 755 /etc/init.d/memcached #add execute permission
2 chkconfig --add memcached #add memcached to the service item
3 chkconfig --level 2345 memcached on #set boot
4 chkconfig --list memcached #Check if the setting is successful
#Service Management Command

1 service memcached start # start memcached
2 service memcached stop # close memcached
3 service memcached restart # restart memcached
 

 

Memcached startup parameter description:

The -d option is to start a daemon process,
-m is the amount of memory allocated to Memcache, the unit is MB, the default is 64MB
-M return error on memory exhausted (rather than removing items)
-u is the user running Memcache. If it is currently root, you need to use this parameter to specify the user.
-l is the IP address of the monitored server, which defaults to all network cards.
-p is to set the TCP listening port of Memcache, preferably a port above 1024
-c option is the maximum number of concurrent connections running, the default is 1024
-P is set to save the pid file of Memcache
-f <factor> chunk size growth factor (default: 1.25)
-I Override the size of each slab page. Adjusts max item size (new in version 1.4.2)
You can also start multiple daemons, but the port cannot be duplicated

 

 

-p specifies the port number (default 11211)
-m specifies the maximum memory size used (default 64MB, maximum 2G)
-t number of threads (default 4)
  
-l Connected IP address, the default is this machine
-d start start the memcached service (default is start)
-d restart restart memcached service
-d stop | shutdown shut down the running memcached service
-d uninstall uninstall memcached service
-m Maximum memory usage in MB. 64MB by default
-M returns an error when the memory is exhausted instead of deleting the item
-c The maximum number of simultaneous connections, the default is 1024
-f block size growth factor, default is 1.25
-n minimum allocated space, key + value + flags default to 48
-h show help

Install Memcached under CentOS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.