Memcached笔记——(一)安装&常规错误&监控

08年的时候接触过 Memcached ,当时还对它的客户端产品嗤之以鼻,毕竟手工代码没有各种ORM原生XML配置方便。尽管如此,Memcached现在已经成了服务器架构里不可或缺的一部分!  

相关链接: 
Memcached笔记——(一)安装&常规错误&监控 
Memcached笔记——(二)XMemcached&Spring集成 
Memcached笔记——(三)Memcached使用总结 
Memcached笔记——(四)应对高并发攻击 


一、下载  
1. Libevent  
简单的说就是一个事件触发的网络库,Memcached离不开它。 
Shell代码   收藏代码
  1. wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz  

2. Memcached  
今天的主角 
Shell代码   收藏代码
  1. wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz  

二、安装  
1. Libevent  
解压缩 
Shell代码   收藏代码
  1. tar zxvf libevent-2.0.17-stable.tar.gz  

编译、安装 
Shell代码   收藏代码
  1. ./configure --prefix=/usr && make && make install   

这里一定要注意指定 --prefix ,后面配置memcached的时候就有必要用到。 
2. Memcached  
解压 
Shell代码   收藏代码
  1. tar zxvf memcached-1.4.13.tar.gz  

编译、安装 
Shell代码   收藏代码
  1. ./configure --with-libevent=/usr/lib && make && make install  

这里一定要指定 libevent 的路径,否则启动的时候就有找不到libevent的so文件的错误! 
启动 
Shell代码   收藏代码
  1. memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid  

参数 
引用

-p <num>      TCP port number to listen on (default: 11211) 
-U <num>      UDP port number to listen on (default: 11211, 0 is off) 
-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 
-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) 
-v            verbose (print errors/warnings while in event loop) 
-P <file>     save PID in <file>, only used with -d option 

要关掉memcached 
Shell代码   收藏代码
  1. kill -9 `cat /var/run/memcached.pid`    

是否正常?Telnet上去看看 
Shell代码   收藏代码
  1. telnet xxx.xxx.xxx.xxx 11211  

然后输入
Shell代码   收藏代码
  1. stats  

接着就能看到: 
引用

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 

上面状况说明Memcached服务正常。 
还可以试试get、set、delete、replace 
引用
set foo 0 0 3     (保存命令) 
bar               (数据) 
STORED            (结果) 
get foo           (取得命令) 
VALUE foo 0 3     (数据) 
bar               (数据)


输入
Shell代码   收藏代码
  1. quit  
退出。 

三、系统服务  
参照Nginx的系统服务,自己写了一个Memcached的系统服务脚本。  
先构建 /etc/init.d/memcahed 这个文件,然后赋予其可执行权限: 
Shell代码   收藏代码
  1. touch /etc/init.d/memcached  
  2. chmod +x /etc/init.d/memcached  

memcached脚本如下: 
Shell代码   收藏代码
  1. #!/bin/bash  
  2. # v.0.0.1  
  3. # create by snowolf at 2012.5.25  
  4. #  
  5. # memcached  - This shell script takes care of starting and stopping memcached.  
  6. #  
  7. # chkconfig: - 90 10  
  8. # description: Memcache provides fast memory based storage.  
  9. # processname: memcached  
  10.   
  11. memcached_path="/usr/local/bin/memcached"  
  12. memcached_pid="/var/run/memcached.pid"  
  13. memcached_memory="1024"  
  14.   
  15. # Source function library.  
  16. . /etc/rc.d/init.d/functions  
  17.   
  18. [ -x $memcached_path ] || exit 0  
  19.   
  20. RETVAL=0  
  21. prog="memcached"  
  22.   
  23. # Start daemons.  
  24. start() {  
  25.     if [ -e $memcached_pid -a ! -z $memcached_pid ];then  
  26.         echo $prog" already running...."  
  27.         exit 1  
  28.     fi  
  29.   
  30.     echo -n $"Starting $prog "  
  31.     # Single instance for all caches  
  32.     $memcached_path -m $memcached_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached_pid  
  33.     RETVAL=$?  
  34.     [ $RETVAL -eq 0 ] && {  
  35.         touch /var/lock/subsys/$prog  
  36.         success $"$prog"  
  37.     }  
  38.     echo  
  39.     return $RETVAL  
  40. }  
  41.   
  42.   
  43. # Stop daemons.  
  44. stop() {  
  45.     echo -n $"Stopping $prog "  
  46.     killproc -d 10 $memcached_path  
  47.     echo  
  48.     [ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog  
  49.   
  50.     RETVAL=$?  
  51.     return $RETVAL  
  52. }  
  53.   
  54. # See how we were called.  
  55. case "$1" in  
  56.         start)  
  57.             start  
  58.             ;;  
  59.         stop)  
  60.             stop  
  61.             ;;  
  62.         status)  
  63.             status $prog  
  64.             RETVAL=$?  
  65.             ;;  
  66.         restart)  
  67.             stop  
  68.             start  
  69.             ;;  
  70.         *)  
  71.             echo $"Usage: $0 {start|stop|status|restart}"  
  72.             exit 1  
  73. esac  
  74. exit $RETVAL  

注意这几行配置,请根据实际情况配置memcached执行文件路径,以及Memcached使用内存大小: 
引用
memcached_path="/usr/local/bin/memcached" 
memcached_memory="1024"


追加为系统服务: 
Shell代码   收藏代码
  1. chkconfig --add memcached  
  2. chkconfig memcached on  


然后就可以通过 service memcached start|stop|status|restart 控制memcached了!  

四、常规错误  
一开始没有指定libevent路径安装memcached的时候,启动memcached就报这个错误: 
引用
memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory 

其实这个文件就在 /usr/lib 下。错就错在Linux是64bit系统,如果没有指定libevent路径,memcached就会去 /usr/lib64 下去找。 
找到这个文件 
Shell代码   收藏代码
  1. whereis libevent-2.0.so.5  

引用
libevent-2.0.so: /usr/lib/libevent-2.0.so.5

Shell代码   收藏代码
  1. ldd /usr/local/bin/memcached  

提示找不到 libevent-2.0.so.5  
引用
        linux-vdso.so.1 =>  (0x00007fff41dfd000) 
        libevent-2.0.so.5 => not found 
        librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000) 
        libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000) 
        libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000) 
        /lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)

定位 
Shell代码   收藏代码
  1. LD_DEBUG=libs /usr/local/bin/memcached -v  

引用
     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 

注意这句: 
引用
19905: trying file=/usr/lib64/libevent-2.0.so.5


做个软连接 
Shell代码   收藏代码
  1. ln -s /usr/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5  

再试试: 
Shell代码   收藏代码
  1. ldd /usr/local/bin/memcached  

这回找到了! 
引用
        linux-vdso.so.1 =>  (0x00007fffffef6000) 
        libevent-2.0.so.5 => /usr/lib64/libevent-2.0.so.5 (0x00002b5608a26000) 
        librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000) 
        libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000) 
        libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000) 
        /lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000)


五、监控  
可以在服务器上配置一个PHP页面来监测Memcached的情况, 
下载MemcachePHP  



配置也比较简单,主要包括账户配置,以及Memcached Server地址配置。 
Php代码   收藏代码
  1. define('ADMIN_USERNAME','memcache');    // Admin Username  
  2. define('ADMIN_PASSWORD','password');    // Admin Password  
  3. define('DATE_FORMAT','Y/m/d H:i:s');  
  4. define('GRAPH_SIZE',200);  
  5. define('MAX_ITEM_DUMP',50);  
  6.   
  7. $MEMCACHE_SERVERS[] = 'mymemcache-server1:11211'// add more as an array  
  8. $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'// add more as an array  


常见错误: 
引用
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 

在memcache.php顶端加上“date_default_timezone_set('Asia/Hong_Kong');”即可,具体地域设置参考: http://www.php.net/manual/zh/datetime.configuration.php#ini.date.timezone  

如果不方便搭建PHP服务,可以使用Perl脚本 memcache-top  
修改 @default_instances 或使用--instances参数: 
Shell代码   收藏代码
  1. perl memcache-top-v0.6 --instances 10.11.155.26 10.11.155.41  


 

先到这里,后续做压力测试,Java客户端开发,Spring系统集成等。  

相关链接: 
Memcached笔记——(一)安装&常规错误&监控 
Memcached笔记——(二)XMemcached&Spring集成 
Memcached笔记——(三)Memcached使用总结 
Memcached笔记——(四)应对高并发攻击 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值