Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17335
  • 博文数量: 3
  • 博客积分: 1465
  • 博客等级: 上尉
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-02 15:06
文章分类
文章存档

2011年(1)

2010年(2)

分类: 数据库开发技术

2011-05-14 11:37:07

1.

本手册介绍安装、运行和运维Memcached

目标读者:开发、测试和运维人员。

memcached danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于减少数据库负载,提升性能。

具有以下的特点:

l  协议简单

l  基于libevent的事件处理

l  内置内存存储方式

l  memcached不互相通信的分布式。

官方网站: ,列出一些memcached Users。下面是使用memcached的一般架构。

 

Figure 2memcached使用案例

l  libeventlibevent是一个事件触发的网络库,适用于windowslinuxbsd等多种平台,内部使用selectepollkqueue等系统调用管理事件机制。编译库代码时,编译脚本会判断OS支持哪种类型的事件机制(selectepoll kqueue),然后条件编译相应代码,供上层使用的接口仍然是保持统一的。

,安装软件到$HOME/local/详细目录约定参见UC讨论区--基础技术板块。

wget ~provos/libevent-1.4.13-stable.tar.gz

tar zxvf libevent-1.4.13-stable.tgz

cd libevent-1.4.13-stable/

./configure --prefix=$HOME/local/libevent

make;make install

如果有链接打不开,请登录页面:下载对应版本。

wget

tar zxvf memcached-1.4.5.tar.gz

cd memcached-1.4.5

./configure --prefix=$HOME/local/memcached \

--with-libevent=$HOME/local/libevent

make;make install

请用新版memcached-1.4.5,如有下载链接不可用,请尝试页面:下载该版本。

不指定--with-libevent则后面运行memcached时可能提示找不到libevent.so

./memcached

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

需要设置环境变量,在~/.bash_profile内加个lib的查找路径,即可解决:

vi ~/.bash_profile

export LD_LIBRARY_PATH= $HOME/local/libevent/lib:$LD_LIBRARY_PATH

source ~/.bash_profile

运维部署时:

./memcached -d -P $HOME/local/memcached/bin/memcached.pid -t 8 -m 2048 -f 1.1 -l 192.168.3.173 -p 11211

测试或调试时:

./memcached -t 8 -m 16 -l localhost -p 31211 -vv

监听unix socket时:

./memcached -d -t 8 -m 2048 -s /home/cws/local/memcached/memcache.sock

备注:

因为32位系统的应用程序最大可管理的内存上限为3G,如果32位机器有很多内存,请启动多个memcached,每个最大可配置-m参数为 2.8GB 内存。

请多线程方式启动memcached,线程数不要超过cpu个数,用-t选项,例如-t 8,表示启动8条线程。

-P参数可用来脚本停止memcached和监控。

-f指明chunk size的增长因子,用较小值1.1可减少内存分配方面的浪费,详见下述启动参数一节。

3.1. 启动参数

启动参数注释如下:

-p       TCP port number to listen on (default: 11211)

               指定服务TCP端口,默认为11211

-U       UDP port number to listen on (default: 11211, 0 is off)

               指定服务UDP端口  默认11211表示打开, 设置0表示关闭

-s UNIX socket path to listen on (disables network support)

               指定unix domain socket的文件名,则关闭端口绑定

-a      access mask for UNIX socket, in octal (default: 0700)

               UNIX domain socket文件属性屏蔽字。8进制,默认0700(和-s配合使用)

-l   interface to listen on (default: INADDR_ANY, all addresses)

               指定监听IP地址(默认对所有网卡IP都生效)

-d           run as a daemon

               以精灵程序的方式运行

-r            maximize core file limit

              设置coredump文件SIZE至上限

-u assume identity of (only when run as root)

               指定进程用户身份(只有以root运行时才有效,且root身份必须指定用户身份,memcached禁止以进程用户身份为root)

-m     max memory to use for items in megabytes (default: 64 MB)

               分配给memcached用作缓存的内存大小,单位为MB。默认64MB

               注意32位系统最大可管理的内存上限为3G,预留一些,测试分配2.8G是有效的。

               不要给memcached分配超过机器内存数,否则会自动使用swap分区,让性能大降,产生超时。

              (调优参数)

-M          return error on memory exhausted (rather than removing items)

               LRU算法开关。缓存满时不使用LRU算法替换旧的数据,返回错误

               每个slab内的数据使用LRU,不是全局的。因此,在一些情况下,反而会影响命中率

             (例如多数KEY-VALUE大小近似,都保存在同一slab的情况下)

-c     max simultaneous connections (default: 1024)

              最大并发连接数。默认1024

              memcached使用libevent, 可以支持10K个连接。且memcached推荐客户端缓存连接(长连接)

              (调优参数)

-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').

              是否调用mlockall()锁定内存页。

              注意:如果锁定了内存页,则若申请内存大小超过锁定值(可以通过ulimit -l查看)时就有可能失败

              (不建议使用。默认ulimit -l只有64KB

-v            verbose (print errors/warnings while in event loop)

                输出详细信息(在event轮询时输出错误或警告信息,以daemon方式运行无效)

-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

               打印memcachedlibeventlicence

-P save PID in , only used with -d option

               保存进程ID到指定文件。与-d配合使用

-f    chunk size growth factor (default: 1.25)

               chunk size的增长因子(合理范围1.05~2,默认:1.25

              1.2版本之前默认是2

(调优参数)

-n     minimum space allocated for key+value+flags (default: 48)

                每个ITEM保存数据占用的最小空间。最小空间大小=KEY的长度+value的长度+flags的长度(默认为48字节)

                而每个ITEM的实际占用空间为 ITEM保存占用数据保存的最小空间 + ITEM结构占用的空间 = 48 + 32 = 80字节。

                因此chunk size的默认初始值为80字节

              (调优参数)

-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.

              尝试使用大容量内存页(如果可能的话)。增加内存页容量可以减少虚存-物理内存映射页表缓冲命中失败(TLB-MISSED)的次数,

              并提高性能。但memcached会在启动的时候立即向OS申请-m参数指定的最大缓存块。

              (调优参数)

-D      Use 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.

              指定统计报告时IDKEY之间的分隔符。默认的分隔符为“:”。

              如果指定了此参数,统计采集功能就会自动启动;否则,可以通过发送“stats detail on”命令启动统计采集功能。

-t       number of threads to use (default: 4)

              memcached启动的工作线程数。默认为4

              默认会创建4个工作线程,主线程用于监听客户建立的连接请求、accpet请求,

              然后通过管道通知子线程,由子线程处理读写请求。

              memcached的多线程主要是通过实例化多个libevent实现的,

              分别是一个主线程和n个工作(workers)线程,无论是主线程还是workers线程全部通过libevent 管理网络事件,

              实际上每个线程都是一个单独的libevent实例。

              建议不要超过系统CPU的个数。

              (调优参数)

-R            Maximum number of requests per event, limits the number of

              requests process for a given connection to prevent

              starvation (default: 20)

              每次事件触发响应新连接的最大数目。设置此限制是防止其他I/O事件“挨饿”,得不到响应。

              每个工作线程都单独建立了libevent事件触发。

              (调优参数,一般不需要调整)

-C          Disable use of CAS

              关闭'CAS'指令。

              “cas” is a check and set operation which means

              “store this data but only if no one else has updated since I last fetched it.”

              意思是说如果这个值我最后一次取的没有被修改的话才存储这个值,

              比如我先获取一个key为”update_time”的值,然后有其他进程修改了这个值,

              此时我再调用cas设置这个值时则会返回一个EXSISTS的错误表示修改失败。

              默认支持cas指令。则每次value的修改,都会记录一个CAS序列号(CAS_UNIQUE)。

              gets指令会返回CAS_UNIQUE

-b          Set the backlog queue limit (default: 1024)

              指定监听队列长度(listen()的参数)。默认为1024

              (调优参数。不需要调整,内核对监听队列长度有个上限)

-B           Binding protocol - one of ascii, binary, or auto (default)

              指定绑定的memcached协议。包括:ascii: 文本协议;binary: 二进制协议;auto: 自动检测(默认选项)

-I            Override the size of each slab page. Adjusts max item size

              改变slab page的容量大小,以调整ITEM容量的最大值,默认为1MB

              设置参数:[k|K|m|M]

              不能少于1024bytes(即1K),不能大于128MBmemcached不推荐大于1MB

             (调优参数)

-S          Turn on Sasl authentication

              启动SSL认证。需要在编译支持SSL

              使用SSL认证时,只能使用二进制协议,不能使用文本协议。

阅读(1234) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~