一、引言

             写有关NoSQL数据库有关的文章已经有一段时间了,可以高兴的说,Redis暂时就算写完了,从安装到数据类型,在到集群,几乎都写到了。如果以后有了心得,再补充吧。然后就是MongoDB了,有关MongoDB的文章也写了一些了,安装、配置和基本使用都写了,然后就是一些高级的课题了。最近由于工作比较忙的原因,写东西就慢了,MongoDB的文章也暂缓了。今天正好有时间,我就把在Linux环境下安装和配置Memcached的过程写下来。其实这个文章不是今天写的了,写了有几天了,只是利用今天把文章发出来。

二、Memcached简介

           Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。

           Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。Memcached简洁而强大。它的简洁设计便于快速开发,减轻开发难度,解决了大数据量缓存的很多问题。它的API兼容大部分流行的开发语言。 本质上,它是一个简洁的key-value存储系统。和Redis是有些类似的,但是也有很大的不同了。

三、Memcached在Linux上的安装

         今天我们开始讲如何在Linux系统上安装和配置 Memcached 服务,过程很简单,马上开始我们的安装吧。

         1、准备安装环境

                     yum install gcc wget make cmake libtool autoconf


         2、下载相应的库和Memcached源码

                     libevent,因为Memcached依赖这个Libevent库,所以必须先下载安装

                     官网地址:http://libevent.org/


         3、先安装 Libevent 库文件

                    首先检查系统中是否安装了libevent

                       [root@linux memcached]# pwd
                       /root/software/download/memcached

                       [root@linux memcached]# rpm -qa |grep libevent
                       [root@linux memcached]# //无值说明libevent没有安装


                   如果安装了则查看libevent的安装路径,后续安装时需要用到

                       [root@linux bin]# rpm -ql libevent
                       未安装软件包 libevent
                       [root@linux bin]# 


                   如果没有安装,则先安装libevent


                    3.1、下载libevent

                        [root@linux download]# ls
                        libevent  memcached  mongodb  redis

                        [root@linux download]# cd libevent/
                        [root@linux libevent]# wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz

                        [root@linux libevent]# ls
                        libevent-2.1.8-stable.tar.gz
                        [root@linux libevent]# 


                    3.2、tar 解压Libevent库文件             

                        [root@linux libevent]# pwd
                        /root/software/download/libevent

                        [root@linux libevent]# tar zxvf libevent-2.1.8-stable.tar.gz
                        [root@linux libevent]# ls
                        libevent-2.1.8-stable  libevent-2.1.8-stable.tar.gz


                    3.3、cd 进入根目录

                        [root@linux libevent]# cd libevent-2.1.8-stable
                        [root@linux libevent-2.1.8-stable]# pwd
                        /root/software/download/libevent/libevent-2.1.8-stable

                         [root@linux libevent-2.1.8-stable]# ./configure -h/-help //在根目录下面查看一下配置文件,方便以后使用


                    3.4、设置安装路径,安装目录可以自定义。我的安装目录是:/root/application/program/memcached/libevent

                        [root@linux libevent-2.1.8-stable]# ./configure --prefix=/root/application/program/memcached/libevent
                        checking for a BSD-compatible install... /usr/bin/install -c
                        checking whether build environment is sane... yes
                        checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
                        checking for gawk... gawk
                        checking whether make sets $(MAKE)... yes
                        checking whether make supports nested variables... yes
                        checking whether make supports nested variables... (cached) yes
                        checking for style of include used by make... GNU
                        checking for gcc... gcc
                        ......
                        ......
                        checking size of pthread_t... 8
                        checking for library containing ERR_remove_thread_state... -lcrypto
                        checking that generated files are newer than configure... done
                        configure: creating ./config.status
                        config.status: creating libevent.pc
                        config.status: creating libevent_openssl.pc
                        config.status: creating libevent_pthreads.pc
                        config.status: creating libevent_core.pc
                        config.status: creating libevent_extra.pc
                        config.status: creating Makefile
                        config.status: creating config.h
                        config.status: creating evconfig-private.h
                        config.status: evconfig-private.h is unchanged
                        config.status: executing depfiles commands
                        config.status: executing libtool commands
                        [root@linux libevent-2.1.8-stable]# 


                    3.5、编译和安装 make && make install

                        [root@linux libevent-2.1.8-stable]# make && make install


         4、安装Memcached源文件

                    //这不可以不执行,默认有相应的权限。如果没有,修改安装包的执行操作权限,chmod 777 memcached-1.4.15.tar.gz  

                    4.1、下载 memcached-1.4.15 源程序

                          [root@linux memcached]# pwd
                          /root/software/download/memcached
                          [root@linux memcached]# wget memcached 的地址

                          [root@linux memcached]# ls
                          memcached-1.4.15.tar.gz
                          [root@linux memcached]# 


                    4.2、解压Memcached 压缩文件

                          [root@linux memcached]# pwd
                          /root/software/download/memcached

                          [root@linux memcached]# tar zxvf memcached.tar.gz
                           memcached-1.4.15/
                           memcached-1.4.15/Makefile.am
                           memcached-1.4.15/trace.h
                           memcached-1.4.15/config.guess
                           memcached-1.4.15/cache.c
                           memcached-1.4.15/util.c
                           memcached-1.4.15/assoc.c
                           ....
                           ....
                           memcached-1.4.15/doc/xml2rfc/rfc2629-other.ent
                           memcached-1.4.15/doc/xml2rfc/reference.RFC.0768.xml
                           memcached-1.4.15/doc/xml2rfc/rfc2629-xhtml.ent
                           memcached-1.4.15/doc/xml2rfc/rfc2629-noinc.xsl
                           memcached-1.4.15/doc/xml2rfc/rfc2629-refchk.xsl
                           memcached-1.4.15/doc/xml2rfc/rfc2629.dtd
                           memcached-1.4.15/doc/protocol.txt
                           memcached-1.4.15/doc/Makefile
                           memcached-1.4.15/compile
                           [root@linux memcached]# 


                    4.3、进入到 memcached 根目录

                          [root@linux memcached]# ls
                          memcached-1.4.15  memcached-1.4.15.tar.gz
                          [root@linux memcached]# cd memcached-1.4.15
                          [root@linux memcached-1.4.15]# pwd
                          /root/software/download/memcached/memcached-1.4.15


                    4.4、配置安装目录

                          [root@linux memcached-1.4.15]# pwd
                          /root/software/download/memcached/memcached-1.4.15

                          [root@linux memcached]# ./configure -h/-help  //可以查看memcached 配置文件,方便使用

                          [root@linux memcached-1.4.15]# ./configure --prefix=/root/application/program/memcached/memcachedfile --with-libevent=/root/application/program/memcached/libevent
                          checking build system type... x86_64-unknown-linux-gnu
                          checking host system type... x86_64-unknown-linux-gnu
                          checking target system type... x86_64-unknown-linux-gnu
                          checking for a BSD-compatible install... /usr/bin/install -c
                          checking whether build environment is sane... yes
                          checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
                          ....
                          ....
                          checking for xml2rfc... no
                          checking for xsltproc... /usr/bin/xsltproc
                          configure: creating ./config.status
                          config.status: creating Makefile
                          config.status: creating doc/Makefile
                          config.status: creating config.h
                          config.status: executing depfiles commands
                          [root@linux memcached-1.4.15]# 


                    4.5、编译和安装 make && make install

                          [root@linux memcached-1.4.15]# make && make install


         5、启动Memcached服务环境

                   //启动程序必须在安装Memcached服务的目录的Bin目录下执行操作
                   [root@linux bin]# pwd
                   /root/application/program/memcached/memcachedfile/bin

                   [root@linux bin]# ./memcached -m 64 -p 11211 -u nobody -vvv  前端
                   slab class   1: chunk size        96 perslab   10922
                   slab class   2: chunk size       120 perslab    8738
                   slab class   3: chunk size       152 perslab    6898
                   slab class   4: chunk size       192 perslab    5461
                   slab class   5: chunk size       240 perslab    4369
                   slab class   6: chunk size       304 perslab    3449
                   ....
                   ....
                   <29 server listening (udp)
                   <30 server listening (udp)
                   <29 server listening (udp)
                   <30 server listening (udp)
                   <29 server listening (udp)
                   <30 server listening (udp)
                   <29 server listening (udp)
                   <30 server listening (udp)
                   //当前处于阻塞状态,说明Memcached启动成功,但是该窗口不能关闭,否则服务也会关闭

                   [root@linux bin]# ./memcached -m 64 -p 11211 -u nobody -d  后台启动
                   [root@linux bin]#

                   //查看memcache是否开启:
                   [root@linux ~]# netstat -ntpl|grep memcache


          6、关闭Memcached服务环境

                  [root@linux ~]# kill PID(Memcached服务的进程号)  

                  [root@linux ~]# kill 12177


         7、配置

                启动参数介绍如下:和上面的命令不对应

                   -d选项是启动一个守护进程,

                   -m是分配给Memcache使用的内存数量,单位是MB,这里是10MB,

                   -u是运行Memcache的用户,这里是root,

                   -l是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址192.168.0.200,

                   -p是设置Memcache监听的端口,这里设置了12000,最好是1024以上的端口,

                   -c选项是最大运行的并发连接数,默认是1024,这里设置了256,按照服务器的负载量来设定,

                   -P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,也可以启动多个守护进程,不过端口不能重复。


         8、我们可以查看memcached命令的帮助文件

 1                  [root@linux bin]# pwd
 2                  /root/application/program/memcached/memcachedfile/bin
 3                  [root@linux bin]# ./memcached -h/-help
 4                  memcached 1.4.15
 5                  -p <num>      TCP port number to listen on (default: 11211)
 6                  -U <num>      UDP port number to listen on (default: 11211, 0 is off)
 7                  -s <file>     UNIX socket path to listen on (disables network support)
 8                  -a <mask>     access mask for UNIX socket, in octal (default: 0700)
 9                  -l <addr>     interface to listen on (default: INADDR_ANY, all addresses)
10                                <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
11                  -d            run as a daemon
12                  -r            maximize core file limit
13                  -u <username> assume identity of <username> (only when run as root)
14                  -m <num>      max memory to use for items in megabytes (default: 64 MB)
15                  -M            return error on memory exhausted (rather than removing items)
16                  -c <num>      max simultaneous connections (default: 1024)
17                  -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').
18                  -v            verbose (print errors/warnings while in event loop)
19                  -vv           very verbose (also print client commands/reponses)
20                  -vvv          extremely verbose (also print internal state transitions)
21                  -h            print this help and exit
22                  -i            print memcached and libevent license
23                  -P <file>     save PID in <file>, only used with -d option
24                  -f <factor>   chunk size growth factor (default: 1.25)
25                  -n <bytes>    minimum space allocated for key+value+flags (default: 48)
26                  -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.
27                  -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.
28                  -t <num>      number of threads to use (default: 4)
29                  -R            Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: 20)
30                  -C            Disable use of CAS
31                  -b            Set the backlog queue limit (default: 1024)
32                  -B            Binding protocol - one of ascii, binary, or auto (default)
33                  -I            Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m)
34                  -o            Comma separated list of extended or experimental options
35                                - (EXPERIMENTAL) maxconns_fast: immediately close new connections if over maxconns limit
36                                - hashpower: An integer multiplier for how large the hash table should be. Can be grown at runtime if not big enough.Set this based on "STAT hash_power_level" before a restart.
37                 [root@linux bin]# 


         9、我们使用telnet命令来连接Linux系统上的Memcached服务。


                   9.1、在Window环境下通过telnet连接Memcached服务

 1                         C:\Users\Administrator>telnet192.168.127.130 11211
 2 
 3                         //当前处于黑屏状态,等待输入命令,直接输入stats,直接回车。
 4                         stats
 5                         STAT pid 12177
 6                         STAT uptime 619
 7                         STAT time 1522221744
 8                         STAT version 1.4.15
 9                         STAT libevent 2.1.8-stable
10                         STAT pointer_size 64
11                         STAT rusage_user 0.005197
12                         STAT rusage_system 0.067566
13                         STAT curr_connections 10
14                         STAT total_connections 12
15                         STAT connection_structures 11
16                         STAT reserved_fds 20
17                         STAT cmd_get 0
18                         STAT cmd_set 0
19                         STAT cmd_flush 0
20                         STAT cmd_touch 0
21                         STAT get_hits 0
22                         STAT get_misses 0
23                         STAT delete_misses 0
24                         STAT delete_hits 0
25                         STAT incr_misses 0
26                         STAT incr_hits 0
27                         STAT decr_misses 0
28                         STAT decr_hits 0
29                         STAT cas_misses 0
30                         STAT cas_hits 0
31                         STAT cas_badval 0
32                         STAT touch_hits 0
33                         STAT touch_misses 0
34                         STAT auth_cmds 0
35                         STAT auth_errors 0
36                         STAT bytes_read 37
37                         STAT bytes_written 21
38                         STAT limit_maxbytes 67108864
39                         STAT accepting_conns 1
40                         STAT listen_disabled_num 0
41                         STAT threads 4
42                         STAT conn_yields 0
43                         STAT hash_power_level 16
44                         STAT hash_bytes 524288
45                         STAT hash_is_expanding 0
46                         STAT bytes 0
47                         STAT curr_items 0
48                         STAT total_items 0
49                         STAT expired_unfetched 0
50                         STAT evicted_unfetched 0
51                         STAT evictions 0
52                         STAT reclaimed 0
53                         END
54 
55                         //能连接上,说明memcache成功启用,可使用stats命令查看当前状态


                   9.2、在Linux环境下使用telnet命令连接Memcached服务

 1                         [root@linux bin]# telnet 192.168.127.130 11211
 2                         Trying 192.168.127.130...
 3                         Connected to 192.168.127.130.
 4                         Escape character is '^]'.
 5                         //当前窗口处于等待状态,可以直接输入命令执行
 6                         stats //直接运行该命令
 7                         STAT pid 12177
 8                         STAT uptime 619
 9                         STAT time 1522221744
10                         STAT version 1.4.15
11                         STAT libevent 2.1.8-stable
12                         STAT pointer_size 64
13                         STAT rusage_user 0.005197
14                         STAT rusage_system 0.067566
15                         STAT curr_connections 10
16                         STAT total_connections 12
17                         STAT connection_structures 11
18                         STAT reserved_fds 20
19                         STAT cmd_get 0
20                         STAT cmd_set 0
21                         STAT cmd_flush 0
22                         STAT cmd_touch 0
23                         STAT get_hits 0
24                         STAT get_misses 0
25                         STAT delete_misses 0
26                         STAT delete_hits 0
27                         STAT incr_misses 0
28                         STAT incr_hits 0
29                         STAT decr_misses 0
30                         STAT decr_hits 0
31                         STAT cas_misses 0
32                         STAT cas_hits 0
33                         STAT cas_badval 0
34                         STAT touch_hits 0
35                         STAT touch_misses 0
36                         STAT auth_cmds 0
37                         STAT auth_errors 0
38                         STAT bytes_read 37
39                         STAT bytes_written 21
40                         STAT limit_maxbytes 67108864
41                         STAT accepting_conns 1
42                         STAT listen_disabled_num 0
43                         STAT threads 4
44                         STAT conn_yields 0
45                         STAT hash_power_level 16
46                         STAT hash_bytes 524288
47                         STAT hash_is_expanding 0
48                         STAT bytes 0
49                         STAT curr_items 0
50                         STAT total_items 0
51                         STAT expired_unfetched 0
52                         STAT evicted_unfetched 0
53                         STAT evictions 0
54                         STAT reclaimed 0
55                         END
56 
57                         //能连接上,说明memcache成功启用,可使用stats命令查看当前状态


         10、查看Memcached服务是否启动:ps aux|grep memcached 或者 ps -ef|grep memcached

                    [root@linux ~]# ps aux|grep memcached
                    nobody    12177  0.0  0.0 323584  1052 ?        Ssl  15:12   0:00 ./memcached -m 64 -p 11211 -u nobody -d
                    root      12292  0.0  0.0 116860  1012 pts/0    S+   15:33   0:00 grep --color=auto memcached

                    [root@linux ~]# ps -ef|grep memcached
                    nobody    12177      1  0 15:12 ?        00:00:00 ./memcached -m 64 -p 11211 -u nobody -d
                    root      12295  11965  0 15:33 pts/0    00:00:00 grep --color=auto memcached


         11、如果我们通过telnet命令无法连接Linux上的Linux系统上的Memcached服务,可能和端口有关系。

                    //在使用window的cmd连接Linux环境上的Memcached的时候,必须先把11211这个端口增加到防火墙,否则无法连接,执行命令如下:
                    [root@linux ~]# firewall-cmd --zone=public --add-port=11211/tcp --permanent
                    [root@linux ~]# firewall-cmd --reload

                    //再次重新连接就没有问题了


四、总结

        
好了,写完了。有关Memcached的文章可能要晚一点再出来了,先要把MongoDB的文章写完,再来写有关Memcached的文章了,学习和使用也需要一个过程。还有一个原因,由于Redis的崛起和强大,已经完全可以替代Memcached了,而且很多的公司也不使用它了,所以由于原因种种,该系列就往后推了。

posted on 2018-04-27 13:50  可均可可  阅读(7546)  评论(3编辑  收藏  举报