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.linuxidc.com:/home/shirdrn# tar -xvzf libevent-2.0.16-stable.tar.gz
- root@ www.linuxidc.com:/home/shirdrn# cd libevent-2.0.16-stable/
- root@ www.linuxidc.com:/home/shirdrn/libevent-2.0.16-stable# ./configure
- root@ www.linuxidc.com:/home/shirdrn/libevent-2.0.16-stable# make
- root@ www.linuxidc.com:/home/shirdrn/libevent-2.0.16-stable# make install
安装完成以后,通过下面的方式来验证是否安装成功:
- root@ www.linuxidc.com:/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 root 97847 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 root 22414 2011-12-15 14:52 libevent_pthreads-2.0.so.5.1.4
- -rw-r–r– 1 root root 19526 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.101、安装memcached-1.4.10的过程,如下所示:
- root@ www.linuxidc.com:/home/shirdrn# tar -xvzf memcached-1.4.10.tar.gz
- root@ www.linuxidc.com:/home/shirdrn# cd memcached-1.4.10/
- root@ www.linuxidc.com:/home/shirdrn/memcached-1.4.10# ./configure –prefix=/usr/local/memcached –with-libevent=/usr/local
- root@ www.linuxidc.com:/home/shirdrn/memcached-1.4.10# make
- root@ www.linuxidc.com:/home/shirdrn/memcached-1.4.10# make install
- root@ www.linuxidc.com:/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
- /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.linuxidc.com:/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
- -d run as a daemon
- -r maximize 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)
- -M return error on memory exhausted (rather than removing items)
- -c <num> max simultaneous connections (default: 1024)
- -k lock 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’).
- -v verbose (print errors/warnings while in event loop)
- -vv very verbose (also print client commands/reponses)
- -vvv extremely verbose (also print internal state transitions)
- -h print this help and exit
- -i print 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)
- -L Try 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)
- -R Maximum number of requests per event, limits the number of
- requests process for a given connection to prevent
- starvation (default: 20)
- -C Disable use of CAS
- -b Set the backlog queue limit (default: 1024)
- -B Binding protocol – one of ascii, binary, or auto (default)
- -I Override the size of each slab page. Adjusts max item size
- (default: 1mb, min: 1k, max: 128m)
- -o Comma 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.linuxidc.com:/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/7 S+ 15:04 0:00 grep –color=auto memcached
可见当前存在memcached进程,进程号为10402。
再查看一下memcached监听的端口是否被memcached进程占用,如下所示:
- root@ www.linuxidc.com:/home/shirdrn/memcached-1.4.10# netstat -nap | grep 19830
- tcp 0 0 192.168.0.184:19830 0.0.0.0:* LISTEN 10402/memcached
- udp 0 0 192.168.0.184:19830 0.0.0.0:* 10402/memcached
可见,memcached可以通过端口19830向外部(memcache客户端)提供缓存服务。
3、终止memcached服务
因为我们已经将memcached的进程号存储到/tmp/memcached.pid文件中,可以执行如下命令来终止服务:
- root@ www.linuxidc.com:/home/shirdrn/memcached-1.4.10# kill `cat /tmp/memcached.pid`
直接通过ps获取到进程号,然后kill掉该进程即可。