memcached
NinGoo's blog

如何监控MemCached的状态

使用MemCached以后,肯定希望知道cache的效果,对于MemCached的一些运行状态进行监控是必要的。MemCached也提供了stats接口输出一些信息,最简单的方式,就是telnet上去输入stats查看:

telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to memcache_test_host (127.0.0.1 ).
Escape character is '^]'.
stats
STAT pid 7186
STAT uptime 1695
STAT time 1238401344
STAT version 1.2.6
STAT pointer_size 64
STAT rusage_user 0.003999
STAT rusage_system 0.002999
STAT curr_items 1
STAT total_items 54
STAT bytes 135
STAT curr_connections 2
STAT total_connections 111
STAT connection_structures 4
STAT cmd_get 3
STAT cmd_set 54
STAT get_hits 0
STAT get_misses 3
STAT evictions 0
STAT bytes_read 5957
STAT bytes_written 50914
STAT limit_maxbytes 2147483648
STAT threads 1
END

这种方式相当的不方便,所以网上就有各种不同客户端接口写的工具,比如用perl写的这个memcache-tool

./memcached_tool
Usage: memcached-tool  [mode]

       memcached-tool 10.0.0.5:11211 display    # shows slabs
       memcached-tool 10.0.0.5:11211            # same.  (default is display)
       memcached-tool 10.0.0.5:11211 stats      # shows general stats
       memcached-tool 10.0.0.5:11211 move 7 9   # takes 1MB slab from class #7
                                                # to class #9.

You can only move slabs around once memory is totally allocated, and only
once the target class is full.  (So you can't move from #6 to #9 and #7
to #9 at the same itme, since you'd have to wait for #9 to fill from
the first reassigned page)

$ ./memcached_tool 127.0.0.1:11211 stats
#127.0.0.1:11211 Field       Value
                   bytes         135
              bytes_read        5964
           bytes_written       51394
                 cmd_get           3
                 cmd_set          54
   connection_structures           4
        curr_connections           3
              curr_items           1
               evictions           0
                get_hits           0
              get_misses           3
          limit_maxbytes  2147483648
                     pid        7186
            pointer_size          64
           rusage_system    0.002999
             rusage_user    0.003999
                 threads           1
                    time  1238401521
       total_connections         112
             total_items          54
                  uptime        1872
                 version       1.2.6

命令行的方式,在批处理调用的时候比较方便。但是在展现方面还是web方式更加直观有效,所以就有了php写的memcache.php,是的,用一次就知道这是我想要的。

在PHP使用MemCached

在PHP中使用Memcached,有两种方式,一种是安装PHP的memcache扩展(实际上还有另外一个memcached扩展,是基于比较流行的libmemcached库封装的),该扩展是用c写的,效率较高,需要在服务器上安装。另外一种则是直接使用客户端的php-memcached-client类库,但是这个我在网上找了半天也没找到一个官方的网站。所以呢,还是装个扩展吧。假设php安装在/home/admin/php目录:

wget http://pecl.php.net/get/memcache-2.2.5.tgz
gzip -d memcache-2.2.5.tgz
tar xvf memcache-2.2.5.tar
cd memcache-2.2.5
/home/admin/php/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519

./configure --enable-memcache --with-php-config=/home/admin/php/bin/php-config --with-zlib-dir
Installing shared extensions:     /home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/

注意到最后一行返回的信息,将下面两行添加到/home/admin/php/lib/php.ini

extension_dir = "/home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension=memcache.so

然后重启web服务器即可。如果安装成功,则通过phpinfo()可以获得该扩展的相关信息:

memcache support enabled
Active persistent connections 0
Version 2.2.5
Revision $Revision: 1.111 $
Directive Local Value Master Value
memcache.allow_failover 1 1
memcache.chunk_size 8192 8192
memcache.default_port 11211 11211
memcache.default_timeout_ms 1000 1000
memcache.hash_function crc32 crc32
memcache.hash_strategy standard standard
memcache.max_failover_attempts 20 20

以上参数都可以在php.ini中进行设置。下面是一段官方网站的php测试代码:

<?php
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)\n";
$get_result = $memcache->get('key');
echo "Data from the cache:\n";
var_dump($get_result);
?>

运行后输出如下:

Server's version: 1.2.6
Store data in the cache (data will expire in 10 seconds)
Data from the cache: object(stdClass)#3 (2)
{ ["str_attr"]=>  string(4) "test" ["int_attr"]=>  int(123) }

在64位Linux上安装MemCached

在一台64位Linux的机器上安装了MemCached,遇到一个小问题,特记录之。

MemCached使用了libevent,所以必须先安装libevent。安装libevent到/usr/lib

wget http://www.monkey.org/~provos/libevent-1.4.9-stable.tar.gz
gzip -d libevent-1.4.9-stable.tar.gz
tar xvf libevent-1.4.9-stable.tar
cd libevent-1.4.9-stable
./configure --prefix=/usr
make
make install

安装MemCached的到/u01/memcached

wget http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
gzip -d memcached-1.2.6.tar.gz
tar xvf memcached-1.2.6.tar
cd memcached-1.2.6
./configure --prefix=/u01/memcached --with-libevent=/usr
make
make install

但是执行memcached命令时出现错误:

#/u01/memcached/bin/memcached -h
/u01/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2:
cannot open shared object file: No such file or directory

一般对于这种依赖的库找不到的情况,在Linux中可以通过设置LD_DEBUG环境变量来获得更多的信息

#LD_DEBUG=help ls
Valid options for the LD_DEBUG environment variable are:

  libs        display library search paths
  reloc       display relocation processing
  files       display progress for input file
  symbols     display symbol table processing
  bindings    display information about symbol binding
  versions    display version dependencies
  all         all previous options combined
  statistics  display relocation statistics
  unused      determined unused DSOs
  help        display this help message and exit

To direct the debugging output into a file instead of standard output
a filename can be specified using the LD_DEBUG_OUTPUT environment variable.

这里由于是库文件依赖有问题,则使用libs参数:

#LD_DEBUG=libs /u01/memcached/bin/memcached -h
     30596:     find library=libevent-1.4.so.2 [0]; searching
     30596:      search cache=/etc/ld.so.cache
     30596:      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)
     30596:       trying file=/lib64/tls/x86_64/libevent-1.4.so.2
     30596:       trying file=/lib64/tls/libevent-1.4.so.2
     30596:       trying file=/lib64/x86_64/libevent-1.4.so.2
     30596:       trying file=/lib64/libevent-1.4.so.2
     30596:       trying file=/usr/lib64/tls/x86_64/libevent-1.4.so.2
     30596:       trying file=/usr/lib64/tls/libevent-1.4.so.2
     30596:       trying file=/usr/lib64/x86_64/libevent-1.4.so.2
     30596:       trying file=/usr/lib64/libevent-1.4.so.2
     30596:
/u01/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2:
cannot open shared object file: No such file or directory

可以看到是在加载/usr/lib64/libevent-1.4.so.2文件时出现了问题,系统中确实是没有该文件的,查找后发现libevent-1.4.so.2存在于/usr/lib目录,这可能是libevent在64位Linux系统上的一个bug吧,没有关系,复制一份或者建一个软链接即可解决问题。

#ln -s /usr/lib/libevent-1.4.so.2 /usr/lib64/libevent-1.4.so.2
#/u01/memcached/bin/memcached -h
memcached 1.2.6
-p       TCP port number to listen on (default: 11211)
-U       UDP port number to listen on (default: 0, off)
-s      unix socket path to listen on (disables network support)
-a      access mask for unix socket, in octal (default 0700)
-l   interface to listen on, default is INDRR_ANY
-d            run as a daemon
-r            maximize core file limit
-u  assume identity of  (only when run as root)
-m       max memory to use for items in megabytes, default is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c       max simultaneous connections, default is 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  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)
-h            print this help and exit
-i            print memcached and libevent license
-b            run a managed instanced (mnemonic: buckets)
-P      save PID in , only used with -d option
-f    chunk size growth factor, default 1.25
-n     minimum space allocated for key+value+flags, default 48

启动MemCached,-m表示分配的内存

#/u01/memcached/bin/memcached -d -m 1024 -u admin -l 127.0.0.1 -p 11211

缓存为王:Memcached和MySQL的结合应用

在计算机和网络的世界里,缓存无处不在,从CPU的寄存器到一级缓存二级缓存,从硬盘的文件到阵列卡的缓存到文件的缓存,从浏览器的缓存到web服务器的缓存到数据库的缓存,Cache is King

Memcached是一个分布式的内存缓存系统,在很多大型网站中都有成功的应用案例。设计思想说起来也很简单,服务器端保存精悍而独立,基本上就是一个slab对象缓存管理器,而在客户端实现负载均衡,采用hash算法来选择不同的服务端缓存key-value数据。

FotologFrank同学最近在MySQL官方网站上做了一个Memcached结合MySQL应用的webinar