红联Linux门户
Linux帮助

Linux下Memcached-1.4.10安装

发布时间:2014-11-14 15:55:13来源:linux网站作者:shirdrn

memcache是一款流行的缓存产品,它分为两个部分:一个是运行在服务器端的memcached进程,一个是在客户端进行调用获取缓存中数据客户端,例如比较常用的PHP客户端。这里,记录一下安装服务器端的memcached的过程,比较容易。


准备软件包

libevent-2.0.16-stable.tar.gz  [http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.16-stable.tar.gz]
memcached-1.4.10.tar.gz  [http://memcached.googlecode.com/files/memcached-1.4.10.tar.gz]
memcache-3.0.6.tgz  [http://pecl.php.net/get/memcache-3.0.6.tgz]


安装配置

安装libevent-2.0.16

安装libevent-2.0.16的过程如下所示:

root@ www:/home/shirdrn# tar -xvzf libevent-2.0.16-stable.tar.gz  
root@ www:/home/shirdrn# cd libevent-2.0.16-stable/ 
root@ www:/home/shirdrn/libevent-2.0.16-stable# ./configure  
root@ www:/home/shirdrn/libevent-2.0.16-stable# make 
root@ www:/home/shirdrn/libevent-2.0.16-stable# make install 

安装完成以后,通过下面的方式来验证是否安装成功:

root@ www:/usr/local# ls -al  /usr/local/lib | grep libevent
lrwxrwxrwx  1 root root   21 2011-12-15 14:52 libevent-2.0.so.5 -> libevent-2.0.so.5.1.4 
-rwxr-xr-x  1 root root  1072325 2011-12-15 14:52 libevent-2.0.so.5.1.4 
-rw-r--r--  1 root root  1683924 2011-12-15 14:52 libevent.a 
lrwxrwxrwx  1 root root   26 2011-12-15 14:52 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.4 
-rwxr-xr-x  1 root root   646606 2011-12-15 14:52 libevent_core-2.0.so.5.1.4 
-rw-r--r--  1 root root  1045954 2011-12-15 14:52 libevent_core.a 
-rwxr-xr-x  1 root root  974 2011-12-15 14:52 libevent_core.la 
lrwxrwxrwx  1 root root   26 2011-12-15 14:52 libevent_core.so -> libevent_core-2.0.so.5.1.4 
lrwxrwxrwx  1 root root   27 2011-12-15 14:52 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.4 
-rwxr-xr-x  1 root root   450238 2011-12-15 14:52 libevent_extra-2.0.so.5.1.4 
-rw-r--r--  1 root root   638042 2011-12-15 14:52 libevent_extra.a 
-rwxr-xr-x  1 root root  981 2011-12-15 14:52 libevent_extra.la 
lrwxrwxrwx  1 root root   27 2011-12-15 14:52 libevent_extra.so -> libevent_extra-2.0.so.5.1.4 
-rwxr-xr-x  1 root root  939 2011-12-15 14:52 libevent.la 
lrwxrwxrwx  1 root root   29 2011-12-15 14:52 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.1.4 
-rwxr-xr-x  1 root root97847 2011-12-15 14:52 libevent_openssl-2.0.so.5.1.4 
-rw-r--r--  1 root root   131388 2011-12-15 14:52 libevent_openssl.a 
-rwxr-xr-x  1 root root 1010 2011-12-15 14:52 libevent_openssl.la 
lrwxrwxrwx  1 root root   29 2011-12-15 14:52 libevent_openssl.so -> libevent_openssl-2.0.so.5.1.4 
lrwxrwxrwx  1 root root   30 2011-12-15 14:52 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.4 
-rwxr-xr-x  1 root root22414 2011-12-15 14:52 libevent_pthreads-2.0.so.5.1.4 
-rw-r--r--  1 root root19526 2011-12-15 14:52 libevent_pthreads.a 
-rwxr-xr-x  1 root root 1002 2011-12-15 14:52 libevent_pthreads.la 
lrwxrwxrwx  1 root root   30 2011-12-15 14:52 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.4 
lrwxrwxrwx  1 root root   21 2011-12-15 14:52 libevent.so -> libevent-2.0.so.5.1.4


安装memcached-1.4.10
1、安装memcached-1.4.10的过程,如下所示:

root@ www:/home/shirdrn# tar -xvzf memcached-1.4.10.tar.gz 
root@ www:/home/shirdrn# cd memcached-1.4.10/ 
root@ www:/home/shirdrn/memcached-1.4.10# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local 
root@ www:/home/shirdrn/memcached-1.4.10# make 
root@ www:/home/shirdrn/memcached-1.4.10# make install


2、测试memcached-1.4.10安装是否成功:

root@ www:/usr/local# ls -al /usr/local/memcached/bin/mem* 
-rwxr-xr-x 1 root root 365926 2011-12-15 14:54 /usr/local/memcached/bin/memcached


启动memcached服务:

/usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.0.184 -p 19830 -c 5000 -P /tmp/memcached.pid 

执行上面的命令行,将启动memcached服务进程,并且进程号存储在/tmp/memcached.pid文件中。

上述命令行中选项的含义简介如下:

-d  指定memcached进程作为一个守护进程启动
-m  指定分配给memcached使用的内存,单位是MB
-u  指定运行memcached的用户
-l  指定监听的服务器IP地址,如果有多个地址的话,使用逗号分隔,格式可以为“IP地址:端口号”,例如:-l 192.168.0.184:19830,192.168.0.195:13542;端口号也可以通过-p选项指定
-p  指定memcached监听的端口,要保证该端口号未被占用
-c  指定是最大运行的并发连接数,默认是1024
-P  指定保存memcached进程的pid文件

你也可以通过如下命令行获取memcached帮助信息,如下所示:

root@ www:/home/shirdrn/memcached-1.4.10# /usr/local/memcached/bin/memcached -help 
memcached 1.4.10 
-p <num>  TCP port number to listen on (default: 11211) 
-U <num>  UDP port number to listen on (default: 11211, 0 is off) 
-s <file> UNIX socket path to listen on (disables network support) 
-a <mask> access mask for UNIX socket, in octal (default: 0700) 
-l <addr> interface to listen on (default: INADDR_ANY, all addresses) 
<addr> may be specified as host:port. If you don't specify 
a port number, the value you specified with -p or -U is 
used. You may specify multiple addresses separated by comma 
or by using -l multiple times 
-drun as a daemon 
-rmaximize core file limit 
-u <username> assume identity of <username> (only when run as root) 
-m <num>  max memory to use for items in megabytes (default: 64 MB) 
-Mreturn error on memory exhausted (rather than removing items) 
-c <num>  max simultaneous connections (default: 1024) 
-klock down all paged memory.  Note that there is a 
limit on how much memory you may lock.  Trying to 
allocate more than that would fail, so be sure you 
set the limit correctly for the user you started 
the daemon with (not for -u <username> user; 
under sh this is done with 'ulimit -S -l NUM_KB'). 
-vverbose (print errors/warnings while in event loop) 
-vv   very verbose (also print client commands/reponses) 
-vvv  extremely verbose (also print internal state transitions) 
-hprint this help and exit 
-iprint memcached and libevent license 
-P <file> save PID in <file>, only used with -d option 
-f <factor>   chunk size growth factor (default: 1.25) 
-n <bytes>minimum space allocated for key+value+flags (default: 48) 
-LTry to use large memory pages (if available). Increasing 
the memory page size could reduce the number of TLB misses 
and improve the performance. In order to get large pages 
from the OS, memcached will allocate the total item-cache 
in one large chunk. 
-D <char> Use <char> as the delimiter between key prefixes and IDs. 
This is used for per-prefix stats reporting. The default is 
":" (colon). If this option is specified, stats collection 
is turned on automatically; if not, then it may be turned on 
by sending the "stats detail on" command to the server. 
-t <num>  number of threads to use (default: 4) 
-RMaximum number of requests per event, limits the number of 
requests process for a given connection to prevent  
starvation (default: 20) 
-CDisable use of CAS 
-bSet the backlog queue limit (default: 1024) 
-BBinding protocol - one of ascii, binary, or auto (default) 
-IOverride the size of each slab page. Adjusts max item size 
(default: 1mb, min: 1k, max: 128m) 
-oComma separated list of extended or experimental options 
- (EXPERIMENTAL) maxconns_fast: immediately close new 
connections if over maxconns limit 
- hashpower: An integer multiplier for how large the hash 
table should be. Can be grown at runtime if not big enough. 
Set this based on "STAT hash_power_level" before a  
restart. 

下面看一下,如何查看memcached进程启动是否成功,可以通过查看进程信息来确定,如下所示:
root@ www:/home/shirdrn/memcached-1.4.10# ps aux | grep memcached

root 10402  0.0  0.0 126740  1060 ?Ssl  15:03   0:00 /usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.0.184 -p 19830 -c 5000 -P /tmp/memcached.pid 
root 10411  0.0  0.0  12692  1068 pts/7S+   15:04   0:00 grep --color=auto memcached 

可见当前存在memcached进程,进程号为10402。

再查看一下memcached监听的端口是否被memcached进程占用,如下所示:

root@ www:/home/shirdrn/memcached-1.4.10# netstat -nap | grep 19830 
tcp0  0 192.168.0.184:19830 0.0.0.0:*   LISTEN  10402/memcached  
udp0  0 192.168.0.184:19830 0.0.0.0:*   10402/memcached 

可见,memcached可以通过端口19830向外部(memcache客户端)提供缓存服务。


3、终止memcached服务

因为我们已经将memcached的进程号存储到/tmp/memcached.pid文件中,可以执行如下命令来终止服务:

root@ www:/home/shirdrn/memcached-1.4.10# kill `cat /tmp/memcached.pid` 

或者:

直接通过ps获取到进程号,然后kill掉该进程即可。