---
title: "Memcached笔记——（一）安装&常规错误&监控"
author: "snowolf"
date: "2012-03-09T14:27:06Z"
tags: ["libevent", "memcached", "shell"]
categories: ["开源软件"]
slug: "memcached"
draft: false
lastmod: "2013-09-23T18:32:03Z"
id: 1447348
url: "https://snowolf.iteye.com/blog/1447348"
---

08年的时候接触过\[url=http://memcached.org/]Memcached\[/url]，当时还对它的客户端产品嗤之以鼻，毕竟手工代码没有各种ORM原生XML配置方便。尽管如此，Memcached现在已经成了服务器架构里不可或缺的一部分！ :idea: \[b]相关链接： \[url=/blog/1447348]Memcached笔记——（一）安装&常规错误&监控 \[/url] \[url=/blog/1471805]Memcached笔记——（二）XMemcached&Spring集成\[/url] \[url=/blog/1576818]Memcached笔记——（三）Memcached使用总结\[/url] \[url=/blog/1677495]Memcached笔记——（四）应对高并发攻击\[/url] \[/b] \[size=large]\[b]一、下载\[/b]\[/size] 1.\[url=http://libevent.org/]Libevent\[/url] 简单的说就是一个事件触发的网络库，Memcached离不开它。 \[code="shell"]wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz\[/code] 2.\[url=http://memcached.org/]Memcached\[/url] 今天的主角 \[code="shell"]wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz\[/code] \[size=large]\[b]二、安装\[/b]\[/size] 1.\[url=http://libevent.org/]Libevent\[/url] 解压缩 \[code="shell"]tar zxvf libevent-2.0.17-stable.tar.gz\[/code] 编译、安装 \[code="shell"]./configure --prefix=/usr && make && make install \[/code] 这里一定要注意指定\[b]--prefix\[/b]，后面配置memcached的时候就有必要用到。 2.\[url=http://memcached.org/]Memcached\[/url] 解压 \[code="shell"]tar zxvf memcached-1.4.13.tar.gz\[/code] 编译、安装 \[code="shell"]./configure --with-libevent=/usr/lib && make && make install\[/code] 这里一定要指定\[b]libevent\[/b]的路径，否则启动的时候就有找不到libevent的so文件的错误！ 启动 \[code="shell"]memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid\[/code] 参数 \[quote] -p TCP port number to listen on (default: 11211) -U UDP port number to listen on (default: 11211, 0 is off) -l interface to listen on (default: INADDR\_ANY, all addresses) 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 -u assume identity of (only when run as root) -m max memory to use for items in megabytes (default: 64 MB) -M return error on memory exhausted (rather than removing items) -c max simultaneous connections (default: 1024) -v verbose (print errors/warnings while in event loop) -P save PID in , only used with -d option \[/quote] 要关掉memcached \[code="shell"]kill -9 \`cat /var/run/memcached.pid\` \[/code] 是否正常？Telnet上去看看 \[code="shell"]telnet xxx.xxx.xxx.xxx 11211\[/code] 然后输入\[code="shell"]stats\[/code] 接着就能看到： \[quote] STAT pid 3021 STAT uptime 3621 STAT time 1331261509 STAT version 1.4.13 STAT libevent 2.0.17-stable STAT pointer\_size 64 STAT rusage\_user 0.000000 STAT rusage\_system 0.000999 STAT curr\_connections 6 STAT total\_connections 7 STAT connection\_structures 7 STAT reserved\_fds 20 STAT cmd\_get 0 STAT cmd\_set 0 STAT cmd\_flush 0 STAT cmd\_touch 0 STAT get\_hits 0 STAT get\_misses 0 STAT delete\_misses 0 STAT delete\_hits 0 STAT incr\_misses 0 STAT incr\_hits 0 STAT decr\_misses 0 STAT decr\_hits 0 STAT cas\_misses 0 STAT cas\_hits 0 STAT cas\_badval 0 STAT touch\_hits 0 STAT touch\_misses 0 STAT auth\_cmds 0 STAT auth\_errors 0 STAT bytes\_read 72 STAT bytes\_written 1038 STAT limit\_maxbytes 52428800 STAT accepting\_conns 1 STAT listen\_disabled\_num 0 STAT threads 4 STAT conn\_yields 0 STAT hash\_power\_level 16 STAT hash\_bytes 524288 STAT hash\_is\_expanding 0 STAT expired\_unfetched 0 STAT evicted\_unfetched 0 STAT bytes 0 STAT curr\_items 0 STAT total\_items 0 STAT evictions 0 STAT reclaimed 0 END \[/quote] 上面状况说明Memcached服务正常。 还可以试试get、set、delete、replace \[quote]set foo 0 0 3 （保存命令） bar （数据） STORED （结果） get foo （取得命令） VALUE foo 0 3 （数据） bar （数据）\[/quote] 输入\[code="shell"]quit\[/code]退出。 \[size=large]\[b]三、系统服务\[/b]\[/size] 参照Nginx的系统服务，自己写了一个Memcached的系统服务脚本。 :D 先构建\[b]/etc/init.d/memcahed\[/b]这个文件，然后赋予其可执行权限： \[code="shell"] touch /etc/init.d/memcached chmod +x /etc/init.d/memcached \[/code] memcached脚本如下： \[code="shell"] #!/bin/bash # v.0.0.1 # create by snowolf at 2012.5.25 # # memcached - This shell script takes care of starting and stopping memcached. # # chkconfig: - 90 10 # description: Memcache provides fast memory based storage. # processname: memcached memcached\_path="/usr/local/bin/memcached" memcached\_pid="/var/run/memcached.pid" memcached\_memory="1024" # Source function library. . /etc/rc.d/init.d/functions \[ -x $memcached\_path ] || exit 0 RETVAL=0 prog="memcached" # Start daemons. start() { if \[ -e $memcached\_pid -a ! -z $memcached\_pid ];then echo $prog" already running...." exit 1 fi echo -n $"Starting $prog " # Single instance for all caches $memcached\_path -m $memcached\_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached\_pid RETVAL=$? \[ $RETVAL -eq 0 ] && { touch /var/lock/subsys/$prog success $"$prog" } echo return $RETVAL } # Stop daemons. stop() { echo -n $"Stopping $prog " killproc -d 10 $memcached\_path echo \[ $RETVAL = 0 ] && rm -f $memcached\_pid /var/lock/subsys/$prog RETVAL=$? return $RETVAL } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status $prog RETVAL=$? ;; restart) stop start ;; \*) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac exit $RETVAL \[/code] 注意这几行配置，请根据实际情况配置memcached执行文件路径，以及Memcached使用内存大小： \[quote]memcached\_path="/usr/local/bin/memcached" memcached\_memory="1024"\[/quote] 追加为系统服务： \[code="shell"]chkconfig --add memcached chkconfig memcached on \[/code] 然后就可以通过\[b]service memcached start|stop|status|restart\[/b]控制memcached了！ :D \[size=large]\[b]四、常规错误\[/b]\[/size] 一开始没有指定libevent路径安装memcached的时候，启动memcached就报这个错误： \[quote]memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory \[/quote] 其实这个文件就在\[b]/usr/lib\[/b]下。错就错在Linux是64bit系统，如果没有指定libevent路径，memcached就会去\[b]/usr/lib64\[/b]下去找。 找到这个文件 \[code="shell"]whereis libevent-2.0.so.5\[/code] \[quote]libevent-2.0.so: /usr/lib/libevent-2.0.so.5\[/quote] \[code="shell"]ldd /usr/local/bin/memcached\[/code] 提示找不到\[b]libevent-2.0.so.5\[/b] \[quote] linux-vdso.so.1 =&gt; (0x00007fff41dfd000) libevent-2.0.so.5 =&gt; not found librt.so.1 =&gt; /lib64/librt.so.1 (0x0000003c94a00000) libpthread.so.0 =&gt; /lib64/libpthread.so.0 (0x0000003c93a00000) libc.so.6 =&gt; /lib64/libc.so.6 (0x0000003c92e00000) /lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)\[/quote] 定位 \[code="shell"]LD\_DEBUG=libs /usr/local/bin/memcached -v\[/code] \[quote] 19905: find library=libevent-2.0.so.5 \[0]; searching 19905: search path=/usr/lib/lib/tls/x86\_64:/usr/lib/lib/tls:/usr/lib/lib/x86\_64:/usr/lib/lib (RPATH from file /usr/local/bin/memcached) 19905: trying file=/usr/lib/lib/tls/x86\_64/libevent-2.0.so.5 19905: trying file=/usr/lib/lib/tls/libevent-2.0.so.5 19905: trying file=/usr/lib/lib/x86\_64/libevent-2.0.so.5 19905: trying file=/usr/lib/lib/libevent-2.0.so.5 19905: search cache=/etc/ld.so.cache 19905: search path=/lib64/tls/x86\_64:/lib64/tls:/lib64/x86\_64:/lib64:/usr/lib64/tls/x86\_64:/usr/lib64/tls:/usr/lib64/x86\_64:/usr/lib64 (system search path) 19905: trying file=/lib64/tls/x86\_64/libevent-2.0.so.5 19905: trying file=/lib64/tls/libevent-2.0.so.5 19905: trying file=/lib64/x86\_64/libevent-2.0.so.5 19905: trying file=/lib64/libevent-2.0.so.5 19905: trying file=/usr/lib64/tls/x86\_64/libevent-2.0.so.5 19905: trying file=/usr/lib64/tls/libevent-2.0.so.5 19905: trying file=/usr/lib64/x86\_64/libevent-2.0.so.5 19905: trying file=/usr/lib64/libevent-2.0.so.5 19905: /usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory \[/quote] 注意这句： \[quote]19905: trying file=/usr/lib64/libevent-2.0.so.5\[/quote] 做个软连接 \[code="shell"]ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5\[/code] 再试试： \[code="shell"]ldd /usr/local/bin/memcached\[/code] 这回找到了！ \[quote] linux-vdso.so.1 =&gt; (0x00007fffffef6000) libevent-2.0.so.5 =&gt; /usr/lib64/libevent-2.0.so.5 (0x00002b5608a26000) librt.so.1 =&gt; /lib64/librt.so.1 (0x0000003c94a00000) libpthread.so.0 =&gt; /lib64/libpthread.so.0 (0x0000003c93a00000) libc.so.6 =&gt; /lib64/libc.so.6 (0x0000003c92e00000) /lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)\[/quote] \[size=large]\[b]五、监控\[/b]\[/size] 可以在服务器上配置一个PHP页面来监测Memcached的情况， \[url=http://livebookmark.net/memcachephp/memcachephp.zip]下载MemcachePHP\[/url] \[img]http://dl.iteye.com/upload/attachment/0065/8749/733cf774-c176-3bb9-8946-25765722f93a.png\[/img] 配置也比较简单，主要包括账户配置，以及Memcached Server地址配置。 \[code="php"] define('ADMIN\_USERNAME','memcache'); // Admin Username define('ADMIN\_PASSWORD','password'); // Admin Password define('DATE\_FORMAT','Y/m/d H:i:s'); define('GRAPH\_SIZE',200); define('MAX\_ITEM\_DUMP',50); $MEMCACHE\_SERVERS\[] = 'mymemcache-server1:11211'; // add more as an array $MEMCACHE\_SERVERS\[] = 'mymemcache-server2:11211'; // add more as an array \[/code] 常见错误： \[quote]Warning: date(): It is not safe to rely on the system's timezone settings. You are \*required* to use the date.timezone setting or the date\_default\_timezone\_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/usr/local/apache.../memcache.php on line 726 \[/quote] 在memcache.php顶端加上“date\_default\_timezone\_set('Asia/Hong\_Kong');”即可，具体地域设置参考：\[url]http://www.php.net/manual/zh/datetime.configuration.php#ini.date.timezone\[/url] 如果不方便搭建PHP服务，可以使用Perl脚本\[url=http://code.google.com/p/memcache-top/]memcache-top\[/url] 修改\[b]@default\_instances\[/b]或使用--instances参数： \[code="shell"]perl memcache-top-v0.6 --instances 10.11.155.26 10.11.155.41\[/code] \[img]http://dl.iteye.com/upload/attachment/0065/8745/dd07bc19-0bf0-3f4f-8e37-0498c730dc90.png\[/img] 先到这里，后续做压力测试，Java客户端开发，Spring系统集成等。 :) \[b]相关链接： \[url=/blog/1447348]Memcached笔记——（一）安装&常规错误&监控 \[/url] \[url=/blog/1471805]Memcached笔记——（二）XMemcached&Spring集成\[/url] \[url=/blog/1576818]Memcached笔记——（三）Memcached使用总结\[/url] \[url=/blog/1677495]Memcached笔记——（四）应对高并发攻击\[/url] \[/b]