zoukankan      html  css  js  c++  java
  • cacheServer memcache / varnish / redis / nosql redis / Redis Sentinel

    s

    问题2:主从节点挂载异常,主192.168.3.2 从192.168.3.3 

    解决2:

    从机192.168.3.3节点执行命令 su - redisuser -c "/opt/zedis/redis-server /opt/zedis/redis.conf" 启动redis进程,

    然后执行 /opt/zedis/redis-cli slaveof 192.168.3.2 6379命令挂到主节点上

    问题1:Redis slowlog巡检邮件说明及常见慢日志分析方法

    解决1:

    一、邮件内容解释
    1.1邮件说明:
    在redis里处理时间超过10ms就会被记入慢日志,发邮件也是为了给各位作参考,如果确认没问题,可自行忽略,每天只会发送前一天产生的慢日志。
    1.2标题说明:
    1、A-I列为对应redis服务器的基本信息
    2、J为慢日志发生时间戳
    3、K列为命令执行时间(CPU耗时+IO等待+其他)
    4、L列为执行命令和参数
    5、M列为CPU耗时
    6、M-O列为swap使用情况,目前大部分redis已取消swap设置,所以有些会显示无
    二、问题分析
    2.1、 slowlog产生原因判断
    1、如果命令执行时间远大于CPU耗时时,可能是资源抢占导致,这个需要结合当时的物理机资源使用状态进行分析。(资源争抢只能查看两天的数据,请及时查看)
    具体可以及时联系管理员查看资源占用情况,或者登陆基础运维监控平台http://alarm.itsm.cns*****.com/manalysis/resources/overview/进行查看。
    2、如果cpu耗时和命令执行时间相近的,可能是存在大value值,或者使用了HGETALL,KEYS,FLUSHDB这类的会引起阻塞的命令导致,可以结合当时的命令和对应的key进行具体分析。
    2.2、具体案例分析
    slowlog信息如下:
    登录基础运维监控平台,查看机器资源使用情况如下:
    分析:
    该服务器redis命令执行时间远大于CPU耗时,通过登陆基础运维监控平台http://alarm.itsm.cnsuning.com/manalysis/resources/overview/查看当时物理机cpu使用情况,发现当时存在资源争抢的情况,可以初步判断是资源争抢引起的慢查询日志。
    2.3、常见易阻塞命令例举

    命令

    释义

    时间复杂度

    SMEMBERS

    返回集合 key 中的所有成员

    O(N), N 为集合的基数

    DEL

    删除给定的一个或多个 key

    O(N), N 为被删除的 key 的数量

    bgsave

    在后台异步(Asynchronously)保存当前数据库的数据到磁盘

    O(N), N 为要保存到数据库中的 key 的数量

    MGET

    返回所有(一个或多个)给定 key 的值

    O(N) , N 为给定 key 的数量

    ZADD

    将一个或多个 member 元素及其 score 值加入到有序集 key 当中

    O(M*log(N)), N 是有序集的基数, M 为成功添加的新成员的数量

    KEYS

    查找所有符合给定模式 pattern 的 key

    O(N), N 为数据库中 key 的数量

    LRANGE

    返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定

    O(S+N), S 为偏移量 start , N 为指定区间内元素的数量

    ZRANGE

    返回有序集 key 中,指定区间内的成员

    O(log(N)+M)

    HVALS

    返回哈希表 key 中所有域的值

    O(N), N 为哈希表的大小

    SADD

    将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略

    O(N), N 是被添加的元素的数量

    HMSET

    同时将多个 field-value (域-值)对设置到哈希表 key 中

    O(N), N 为 field-value 对的数量

    EVALSHA

    根据给定的 sha1 校验码,对缓存在服务器中的脚本进行求值

    根据脚本的复杂度而定

    HMGET

    返回哈希表 key 中,一个或多个给定域的值

    O(N), N 为给定域的数量

    LINDEX

    返回列表 key 中,下标为 index 的元素

    O(N), N 为到达下标 index 过程中经过的元素数量

    SDIFFSTORE

    将结果保存到 destination 集合, 该集合是所有给定集合之间的差集

    O(N), N 是所有给定集合的成员数量之和

    SINTER

    返回一个集合的全部成员,该集合是所有给定集合的交集

    O(N * M), N 为给定集合当中基数最小的集合, M 为给定集合的个数

    SREM

    移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略

    O(N), N 为给定 member 元素的数量

    HGETALL

    返回哈希表 key 中,所有的域和值

    O(N), N 为哈希表的大小

    HKEYS

    返回哈希表 key 中的所有域

    O(N), N 为哈希表的大小

    ZREM

    移除有序集 key 中的一个或多个成员,不存在的成员将被忽略

    O(M*log(N)), N 为有序集的基数, M 为被成功移除的成员的数量

    SUNIONSTORE

    将结果保存到 destination 集合,该集合是所有给定集合的并集

    O(N), N 是所有给定集合的成员数量之和

    ZREMRANGEBYRANK

    移除有序集 key 中,指定排名(rank)区间内的所有成员

    O(log(N)+M), N 为有序集的基数,而 M 为被移除成员的数量

    ZREMRANGEBYSCORE

    移除有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员

    O(log(N)+M), N 为有序集的基数,而 M 为被移除成员的数量

    Redis基础原理和日常操作方法

    http://itsm.cns*****.com/kindeditor/img/20170527/759128afca564051b491e6a51a5bad40ab070d19e1a749fa.pptx

    1
    2
    3
    4
    5
    <strong>启动redis</strong>
    su - redisuser -c "/opt/zedis/redis-server /opt/zedis/redis.conf"
     
    <strong>检查redis</strong>
    /opt/zedis/redis-cli

    redis 启动维护

    [root@lindows ~]#  su - redisuser -c "/opt/zedis/redis-server /opt/zedis/redis.conf"

    [root@lindows ~]# ps -ef | grep redis

    502      29025     1  0 11:45 ?        00:00:00 /opt/zedis/redis-server *:6379

    课程> Web开发> PHP> Redis视频教程 

    http://edu.csdn.net/course/detail/2126

    课程> 数据库> NoSQL> Redis

    http://edu.csdn.net/course/detail/2591

    课程> 系统/网络> Linux> Linux下安装部署JDK7+Tomcat7+MySQL5.6+Redis3.07

    http://edu.csdn.net/course/detail/3242

    Redis 3.0.0正式版发布,全新的分布式高可用数据库

    http://www.iteye.com/news/30407 2015-04-02 16:52

    Redis 3.0.0 正式版终于到来了!最重要的新特性是集群(Redis Cluster),提供Redis功能子集(比如不支持多数据库)的分布式、容错的实现(最多支持1000结点)。

    ( 转)Redis Sentinel初体验

    http://lionlx.iteye.com/blog/2199975

    Redis Cluster集群主从方案(附Jedis Cluster教程)

    http://wosyingjun.iteye.com/blog/2289220

    Redis Sentinel主从高可用方案(附Jedis Sentinel教程)

    http://wosyingjun.iteye.com/blog/2289593

    基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案

    http://warm-breeze.iteye.com/blog/2020413

    redis 数据量大,异常停机,不停全量同步问题
    【问题概述】
    reids 从机服务器宕机,当机器恢复,启动redis服务,redis从机自动发布全量同步且数据量超过5G以上,同步完一次,redis继续发出全量同步命令,循环进行同步全量。CPU消耗有告警。

    【现象描述】
    redis从机收到了主机的dump.rdb传输过来的文件,但是读取完毕后,从机继续向主机发起全量同步命令,并报IO error,
    另一个错误是主机超时,主机在一定时间内ping从机无法得到回应,导致主机认为从机宕机,当从机恢复继续发出全量同步命令。
    同时伴有CPU消耗增高。

    【问题跟踪及分析】
    这个问题是由redis配置文件中一个参数设置导致,在redis.conf中:
    client-output-buffer-limit slave 256mb 64mb 60
    这个参数配置,这个参数代表含义:负责发数据给slave的client,如果buffer超过256m或者连续60秒超过64m,就会被立刻强行关闭,就会导致循环同步

    【解决方案】
    设置client-output-buffer-limit 增加buffer和超时时间,可以在redis运行中设置(2.8.8版本中可以),删除已经生成的dump.rdb文件(根据情况,也可以不删),
    再执行:
    /opt/redis-2.8.8/src/redis-cli s*****config set client-output-buffer-limit 'slave 2gb 1gb 1200'

    这样再次进行同步,循环同步数据问题解决.本次问题中,同步数据量为8GB,要根据数据量的大小来合理设置参数. 
    在数据量低于7G情况下,可以不修改参数尝试去同步,但是数据量在超过7G(包括7GB),必须修改上面参数,主机先修改,从机再修改,删除dump文件,重新同步。

    memcache download

    http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.15-stable.tar.gz

    http://code.jellycan.com/files/memcached-1.2.6.tar.gz

    http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip

    http://hi.baidu.com/loveyoursmile/blog/item/1177312af38429335243c16f.html

    http://www.urielkatz.com/archive/detail/memcached-64-bit-windows/

    http://www.urielkatz.com/projects/memcached-win64/memcached-win64.zip

    在64位Linux上安装MemCached

    http://www.ningoo.net/html/2009/install_memcached_on_linux_64.html

    命令行启动memcache服务

    [root@B2Cmonitor Desktop]# /usr/local/bin/memcached -d -u root -m 1024 -l 192.168.100.149 -p 11211

    [root@B2Cmonitor nmon]# crontab -l

    00 00 * * * /nmon/nmon_x86_rhel45 -s60 -c1440 -f -m /nmon

    命令行查看memcache状态

    http://www.51testing.com/?uid-116228-action-viewspace-itemid-107545

    memcache 的运行状态可以方便的用stats命令显示。
    首先用telnet 127.0.0.1 11211这样的命令连接上memcache,然后直接输入stats就可以得到当前memcache的状态。
    这些状态的说明如下:

    pid memcache服务器的进程ID
    uptime 服务器已经运行的秒数
    time 服务器当前的unix时间戳
    version memcache版本
    pointer_size 当前操作系统 的指针大小(32位系统一般是32bit)
    rusage_user 进程的累计用户时间
    rusage_system 进程的累计系统时间
    curr_items 服务器当前存储的items数量
    total_items 从服务器启动以后存储的items总数量
    bytes 当前服务器存储items占用的字节数
    curr_connections 当前打开着的连接数
    total_connections 从服务器启动以后曾经打开过的连接数
    connection_structures 服务器分配的连接构造数
    cmd_get get命令(获取)总请求次数
    cmd_set set命令(保存)总请求次数
    get_hits 总命中次数
    get_misses 总未命中次数
    evictions 为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items)
    bytes_read 总读取字节数(请求字节数)
    bytes_written 总发送字节数(结果字节数)
    limit_maxbytes 分配给memcache的内存大小(字节)
    threads 当前线程数

    [lindows@B2Cmonitor ~]$ /usr/local/bin/memcached -h
    memcached 1.4.5

    -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 <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses)
    -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)

    lindowsmatoMacBook-Pro:~ lindows$ telnet 192.168.100.149 11211
    Trying 192.168.100.149...
    Connected to 192.168.100.149.
    Escape character is '^]'.

    ERROR
    stats
    STAT pid 21251
    STAT uptime 12550645
    STAT time 1320139198
    STAT version 1.4.5
    STAT pointer_size 64
    STAT rusage_user 10126.795493
    STAT rusage_system 21859.064917
    STAT curr_connections 216
    STAT total_connections 5374458
    STAT connection_structures 426
    STAT cmd_get 684113237
    STAT cmd_set 2941276
    STAT cmd_flush 0
    STAT get_hits 683389325
    STAT get_misses 723912
    STAT delete_misses 520993
    STAT delete_hits 51931
    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 auth_cmds 0
    STAT auth_errors 0
    STAT bytes_read 22365116565
    STAT bytes_written 960876073524
    STAT limit_maxbytes 3145728000
    STAT accepting_conns 1
    STAT listen_disabled_num 0
    STAT threads 4
    STAT conn_yields 0
    STAT bytes 313173
    STAT curr_items 260
    STAT total_items 2941276
    STAT evictions 0
    STAT reclaimed 467616
    END

    ERROR

    quit

    Connection closed by foreign host.
    lindowsmatoMacBook-Pro:~ lindows$

    memcache充当session server方案

    http://netli.iteye.com/blog/686925

    java_memcached-release_2.6.2.zip

    https://github.com/gwhalin/Memcached-Java-Client /downloads

    http://dl.iteye.com/topics/download/685c752b-8690-34c5-8783-6d05c9060aab

    lindowsmatoMacBook-Pro:java_memcached-release_2.6.2 lindows$ ls -l
    total 512
    -rwxr-xr-x  1 lindows  staff    1814  8 12 09:04 README
    -rwxr-xr-x  1 lindows  staff  100472  5 25 13:37 commons-pool-1.5.6.jar
    -rwxr-xr-x  1 lindows  staff  118424  8 12 09:00 java_memcached-release_2.6.2.jar
    -rwxr-xr-x  1 lindows  staff   25496  5 25 12:35 slf4j-api-1.6.1.jar
    -rwxr-xr-x  1 lindows  staff    7669  5 25 12:35 slf4j-simple-1.6.1.jar

    192.168.121.17

    java_memcached-release_2.0.1.jar

    [root@arvato4snweb2 ~]# ps -ef | grep memcache
    apache    2780     1  0 Jul05 ?        00:01:47 /usr/local/memcached/bin/memcached -d -m 128 -l 127.0.0.1 -p 11211 -u apache
    apache    2782     1  0 Jul05 ?        00:00:00 /usr/local/memcached/bin/memcached -d -m 128 -l 127.0.0.1 -p 11210 -u apache
    root     14800     1  0 Jul30 ?        00:01:49 java -Xms1024m -Xmx1024m -classpath /home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/ant.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/jdom.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/classes12.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/commons-logging-1.0.4.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/java_memcached-release_2.0.1.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/log4j-1.2.8.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/mysql-connector-java-3.1.12-bin.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/Sys_Lib/proxool-0.9.0RC3.jar:/home/ecccs/work/LiveChat41/Live_Chat41_Server/ReportForICC/classes -Djava.nio.use_epoll=true business.JReportApp 1

    [root@arvato4snweb2 ~]# netstat -atpln | grep 11211
    tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      2780/memcached     
    tcp        0      0 127.0.0.1:37875             127.0.0.1:11211             TIME_WAIT   -                  
    tcp        0      0 127.0.0.1:37879             127.0.0.1:11211             TIME_WAIT   -                  
    tcp        0      0 127.0.0.1:37878             127.0.0.1:11211             TIME_WAIT   -                  
    tcp        0      0 127.0.0.1:37876             127.0.0.1:11211             TIME_WAIT   -                  
    tcp        0      0 127.0.0.1:37121             127.0.0.1:11211             TIME_WAIT   -                  
    tcp        0      0 127.0.0.1:37122             127.0.0.1:11211             TIME_WAIT   -                  
    tcp        0      0 127.0.0.1:37126             127.0.0.1:11211             TIME_WAIT   -                  
    tcp        0      0 127.0.0.1:11211             127.0.0.1:50781             ESTABLISHED 2780/memcached     
    tcp        0      0 ::ffff:127.0.0.1:50781      ::ffff:127.0.0.1:11211      ESTABLISHED 9671/java

    [root@arvato4snweb2 ~]# netstat -an | grep 11211 | wc -l
    15

    192.168.100.149

    [root@B2Cmonitor bin]# ps -ef | grep memcache
    root     10159  9611  0 17:20 pts/1    00:00:00 grep memcache
    lindows  21251     1  0 Jun09 ?        06:02:56 /usr/local/bin/memcached -d -u root -m 3000 -l 192.168.100.149 -p 11211
    [root@B2Cmonitor bin]# netstat -an | grep 11211 | wc -l
    142

    [root@B2Cmonitor lib]# netstat -atpln | grep 11211 | more
    tcp        0      0 192.168.100.149:11211       0.0.0.0:*                   LISTEN      21251/memcached    
    tcp        0      0 192.168.100.149:11211       192.168.100.99:38319        ESTABLISHED 21251/memcached    
    tcp        0      0 192.168.100.149:11211       192.168.119.129:37421       ESTABLISHED 21251/memcached    
    tcp        0      0 192.168.100.149:11211       192.168.119.129:42541       ESTABLISHED 21251/memcached    
    tcp        0      0 192.168.100.149:11211       192.168.119.129:37679       ESTABLISHED 21251/memcached    
    tcp        0      0 192.168.100.149:11211       192.168.119.129:42536       ESTABLISHED 21251/memcached

    [root@B2Cmonitor lib]# ls -l   /usr/local/lib
    total 4980
    lrwxrwxrwx 1 root root      21 Jun  9 10:48 libevent-2.0.so.5 -> libevent-2.0.so.5.1.0
    -rwxr-xr-x 1 root root  946708 Jun  9 10:48 libevent-2.0.so.5.1.0
    -rw-r--r-- 1 root root 1541136 Jun  9 10:48 libevent.a
    lrwxrwxrwx 1 root root      26 Jun  9 10:48 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.0
    -rwxr-xr-x 1 root root  555297 Jun  9 10:48 libevent_core-2.0.so.5.1.0
    -rw-r--r-- 1 root root  930318 Jun  9 10:48 libevent_core.a
    -rwxr-xr-x 1 root root     977 Jun  9 10:48 libevent_core.la
    lrwxrwxrwx 1 root root      26 Jun  9 10:48 libevent_core.so -> libevent_core-2.0.so.5.1.0
    lrwxrwxrwx 1 root root      27 Jun  9 10:48 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.0
    -rwxr-xr-x 1 root root  413593 Jun  9 10:48 libevent_extra-2.0.so.5.1.0
    -rw-r--r-- 1 root root  610890 Jun  9 10:48 libevent_extra.a
    -rwxr-xr-x 1 root root     984 Jun  9 10:48 libevent_extra.la
    lrwxrwxrwx 1 root root      27 Jun  9 10:48 libevent_extra.so -> libevent_extra-2.0.so.5.1.0
    -rwxr-xr-x 1 root root     942 Jun  9 10:48 libevent.la
    lrwxrwxrwx 1 root root      30 Jun  9 10:48 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.0
    -rwxr-xr-x 1 root root   18094 Jun  9 10:48 libevent_pthreads-2.0.so.5.1.0
    -rw-r--r-- 1 root root   18678 Jun  9 10:48 libevent_pthreads.a
    -rwxr-xr-x 1 root root    1005 Jun  9 10:48 libevent_pthreads.la
    lrwxrwxrwx 1 root root      30 Jun  9 10:48 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.0
    lrwxrwxrwx 1 root root      21 Jun  9 10:48 libevent.so -> libevent-2.0.so.5.1.0
    drwxr-xr-x 2 root root    4096 Jun  9 10:48 pkgconfig

    [root@B2Cmonitor lib]# ls -l /usr/local/lib/pkgconfig/
    total 8
    -rw-r--r-- 1 root root 333 Jun  9 10:48 libevent.pc
    -rw-r--r-- 1 root root 370 Jun  9 10:48 libevent_pthreads.pc

    http://pecl.php.net/package/memcache

    http://downloads.php.net/pierre/

    http://sunney2010.iteye.com/blog/656905

    http://hi.baidu.com/%D6%B0%B3%A1%D0%C2%CA%D6%B9%FE%B9%FE/blog/item/1765f3d6c7763ad4a144dfda.html

    http://www.cnblogs.com/daviyang/archive/2010/04/16/1859385.html

    http://hi.baidu.com/baijunhui/blog/item/e395ddd40804b90ca08bb769.html/cmtid/ddde05433540ce1f9213c6cc

    http://pecl.php.net/package/memcache

    http://www.360doc.com/content/10/1210/15/59865_76785815.shtml

    http://www.it118.org/Specials/c1b83237-8710-472c-8ceb-dd3059340f7d/9ff63b86-6629-4a99-8162-84506ccf3c06.htm

    http://hi.baidu.com/lzyu/blog/item/05eac813dba80d045baf5364.html

    http://hi.baidu.com/%D2%BB%D2%B3%B0%C9/blog/item/1ec6ed79ced80de20bd18759.html

    http://hi.baidu.com/baijunhui/blog/item/e395ddd40804b90ca08bb769.html

    http://my.oschina.net/wdlinux/blog/8993

    http://www.monkey.org/~provos/libevent/

    http://www.danga.com/memcached/apis.bml

    Java API

    A Java API is maintained by Greg Whalin from Meetup.com . You can find that library here:

    An improved Java API maintained by Dustin Sallings is also available. Aggressively optimised, ability to run async, supports binary protocol, etc. See site for details:

    http://num7.iteye.com/blog/212778

    Memcache是什么
    Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力。
    它可以应对任意多个连接,使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。
    Memcache官方网站:http://www.danga.com/memcached,更多详细的信息可以来这里了解 :)

    为什么会有Memcache和memcached两种名称?
    其实Memcache是这个项目的名称,而memcached是它服务器端的主程序文件名,知道我的意思了把~~~~。一个是项目名称,一个是主程序文件名,在网上看到了很多人不明白,于是混用了。

    Memcache的安装
    分为两个过程:memcache服务器端的安装和memcached客户端的安装。
    所谓服务器端的安装就是在服务器(一般都是linux系统)上安装Memcache实现数据的存储
    所谓客户端的安装就是指php(或者其他程序,Memcache还有其他不错的api接口提供)去使用服务器端的Memcache提供的函数,需要php添加扩展。

    具体的配置大家可以参考:
    Linux下的Memcache安装 :http://www.ccvita.com/257.html
    Windows下的Memcache安装 :http://www.ccvita.com/258.html
    Memcache基础教程 :http://www.ccvita.com/259.html
    Discuz!的Memcache缓存实现 :http://www.ccvita.com/261.html
    Memcache协议中文版http://www.ccvita.com/306.html

    Memcache的使用和协议分析详解

    http://blog.csdn.net/heiyeshuwu/archive/2006/11/13/1380838.aspx

    http://baike.baidu.com/view/1193094.htm


    Spring+memcache

    http://neptune.iteye.com/blog/220454

    MemCached 压力测试

    http://www.iteye.com/topic/77560

    命令行查看memcache状态

    http://www.51testing.com/?uid-116228-action-viewspace-itemid-107545

    memcache 的运行状态可以方便的用stats命令显示。
    首先用telnet 127.0.0.1 11211这样的命令连接上memcache,然后直接输入stats就可以得到当前memcache的状态。
    这些状态的说明如下:

    pid memcache服务器的进程ID
    uptime 服务器已经运行的秒数
    time 服务器当前的unix时间戳
    version memcache版本
    pointer_size 当前操作系统 的指针大小(32位系统一般是32bit)
    rusage_user 进程的累计用户时间
    rusage_system 进程的累计系统时间
    curr_items 服务器当前存储的items数量
    total_items 从服务器启动以后存储的items总数量
    bytes 当前服务器存储items占用的字节数
    curr_connections 当前打开着的连接数
    total_connections 从服务器启动以后曾经打开过的连接数
    connection_structures 服务器分配的连接构造数
    cmd_get get命令(获取)总请求次数
    cmd_set set命令(保存)总请求次数
    get_hits 总命中次数
    get_misses 总未命中次数
    evictions 为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items)
    bytes_read 总读取字节数(请求字节数)
    bytes_written 总发送字节数(结果字节数)
    limit_maxbytes 分配给memcache的内存大小(字节)
    threads 当前线程数


    memcached安装

    http://www.51testing.com/?uid-116228-action-viewspace-itemid-149319

    1、  安装文件准备

    1)、Memcache的服务器端程序:当前最新版本号为

      下载地址:http://www.danga.com/memcached/download.bml

    2)、Memcache的安装先决条件:先安装libevent,当前最新版本号为

      Libevent介绍:libevent是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制。著名的用于apache的php缓存库memcached 据说也是libevent based,而且libevent在使用上可以做到跨平台,而且根据libevent官方网站上公布的数据统计,似乎也有着非凡的性能。

      下载地址:http://monkey.org/~provos/libevent/

    2、  操作系统 要求

    鉴于我们线上环境和线下的要保持一致,我们都将采用linux。具体的版本号为:

    Redhat advance server4 v4,可以使用更高版本(更高版本未经测试

    如果是redhat advance server4 v2 ,请保证gcc编译器可以使用,或者升级到v4然后安装gcc编译器。一般升级的方式都是从光盘启动升级过程,升级后安装gcc编译器。默认的v2版本没有安装gcc编译器。

    3、  编译器要求

    Memcache的安装文件是要求我们安装gcc编译器的。否则我们的libevent和memcache都无法安装。

    检查是否有gcc编译器的命令:gcc –v

    如果系统的gcc编译器可以用,将会有一段描述,否则提示找不到类库。

    成功的例如:

    Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs

    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux

    Thread model: posix

    gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)

    4、  安装

     先安装libevent,然后才能安装memcache

     1)、libevent安装

    //先解压缩

    [root@localhost]#tar -zxvf  libevent-1.4.8-stable.tar.gz

    //切换到libevent的目录中

    [root@localhost]#cd  libevent-1.4.8-stable

    //指定安装路径到/usr/目录下
    [root@localhost]#./configure --prefix=/usr/

    //编译

    [root@localhost]#make

    //安装

    [root@localhost]#.make install

     2)、memcache服务器安装

    //先解压缩memcached-1.2.6.tar.gz

    [root@localhost]#tar -zxvf  memcached-1.2.6.tar.gz

    //切换到memcache的目录中

    [root@localhost]#cd  memcached-1.2.6

    //指定安装路径到/usr/local/server/memcache目录下,同时指定libevent的安装位置
    [root@localhost]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/

    //编译

    [root@localhost]#make

    //安装

    [root@localhost]#.make install

     测试每步是否安装成功:

       测试libevent:

       [root@localhost]# ls /usr/lib |grep libevent

    libevent-1.4.so.2

    libevent-1.4.so.2.1.1

    libevent.a

    libevent.la

    libevent.so

    libevent_core-1.4.so.2

    libevent_core-1.4.so.2.1.1

    libevent_core.a

    libevent_core.la

    libevent_core.so

    libevent_extra-1.4.so.2

    libevent_extra-1.4.so.2.1.1

    libevent_extra.a

    libevent_extra.la

    libevent_extra.so

      测试memcache:

    [root@localhost]# ls -al /usr/local/memcached/bin

    total 264

    drwxr-xr-x  2 root root   4096 Sep 19 15:31 .

    drwxr-xr-x  4 root root   4096 Sep 19 15:31 ..

    -rwxr-xr-x  1 root root 120949 Sep 19 15:31 memcached

    -rwxr-xr-x  1 root root 129947 Sep 19 15:31 memcached-debug

    5、  启动memcache

    1)、启动Memcache的服务器端:

    [root@localhost]# /usr/local/bin/memcached -d -m 100 -u root -l 192.168.36.200 -p 11211 -c 256 -P /tmp/memcached.pid 

    # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid


        -d选项是启动一个守护进程,
        -m是分配给Memcache使用的内存数量,单位是MB,我这里是100MB,
        -u是运行Memcache的用户,我这里是root,
        -l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.36.200,
        -p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,我们这里统一使用11211
        -c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定。
        -P是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid,

    2)、如果要结束Memcache进程,执行:

    [root@localhost]# kill cat /tmp/memcached.pid

    6、  监测是否启动成功方式

    我们可以使用telnet来对我们的memcache服务器进行访问

    例如:telnet 192.168.36.199 11211 (访问的是192.168.36.199这个ip的11211端口)

    连接上后,直接敲击stats命令看当前缓存服务器状态

    7、  设定memcache的telnet访问限制

    请限定telnet的访问,使之只能在中转机上访问

    8、  memcache的常见概念

    memcached会预先分配内存,memcached分配内存方式称之为allocator,首先,这里有3个概念:
    1 slab
    2 page
    3 chunk
    解 释一下,一般来说一个memcahced进程会预先将自己划分为若干个slab,每个slab下又有若干个page,每个page下又有多个chunk, 如果我们把这3个咚咚看作是object得话,这是两个一对多得关系。再一般来说,slab得数量是有限得,几个,十几个,或者几十个,这个跟进程配置得 内存有关。而每个slab下得page默认情况是1m,也就是说如果一个slab占用100m得内存得话,那么默认情况下这个slab所拥有得page得 个数就是100,而chunk就是我们得数据存放得最终地方。

    9、  Memcache的常用命令

     Memcache常见的命令都在协议文件上:安装文件的的doc目录下的protocol.txt文件中有详细说明

     1)、查询状态命令:stats:

        Name              Type     Meaning

    ----------------------------------

    pid               32u      Process id of this server process

    uptime            32u      Number of seconds this server has been running

    time              32u      current UNIX time according to the server

    version           string   Version string of this server

    pointer_size      32       Default size of pointers on the host OS

                               (generally 32 or 64)

    rusage_user       32u:32u  Accumulated user time for this process

                               (seconds:microseconds)

    rusage_system     32u:32u  Accumulated system time for this process

                               (seconds:microseconds)

    curr_items        32u      Current number of items stored by the server

    total_items       32u      Total number of items stored by this server

                               ever since it started

    bytes             64u      Current number of bytes used by this server

                               to store items

    curr_connections  32u      Number of open connections

    total_connections 32u      Total number of connections opened since

                               the server started running

    connection_structures 32u  Number of connection structures allocated

                               by the server

    cmd_get           64u      Cumulative number of retrieval requests

    cmd_set           64u      Cumulative number of storage requests

    get_hits          64u      Number of keys that have been requested and

                               found present

    get_misses        64u      Number of items that have been requested

                               and not found

    evictions         64u      Number of valid items removed from cache                                                                          

                               to free memory for new items                                                                                      

    bytes_read        64u      Total number of bytes read by this server

                               from network

    bytes_written     64u      Total number of bytes sent by this server to

                               network

    limit_maxbytes    32u      Number of bytes this server is allowed to

                               use for storage.

    threads           32u      Number of worker threads requested.

                               (see doc/threads.txt)

    2)、查询版本号 version

    3)、退出命令 quit

    4)、显示各个slab的信息,包括chunk的大小、数目、使用情况等:stats slabs5)、显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数):stats items
    6)、显示内存分配:stats malloc

    7)、清空缓存数据(其实是将所有缓存数据标记为过期):flush_all

     

    end

  • 相关阅读:
    转载:一句代码改变Swing难看的字体
    生产者消费者问题c语言实现
    水题~~~~HDU 4788
    html中dom居中的5种方式
    scss中的三角函数
    nuxt基础Ⅱ
    nuxt基础Ⅰ
    win10软件安装出现错误代码2503/2502
    npm淘宝镜像安装
    axios的使用
  • 原文地址:https://www.cnblogs.com/lindows/p/14390576.html
Copyright © 2011-2022 走看看