o_200810031547memcached_banner.jpg默认官方memcached是不支持主从同步的,为了解决memcached的高可用,我们是在客户端实现双写和调度,把一份session 多次写入后端的session服务器上,这样使得多台memcached服务器之间的数据有了冗余备份,即便集群中某一台memcached宕机以后,也不会丢失session;在用户访问上,我们在客户端上实现把调度自动切换到没有宕机的memcached上,这样又保证了线上业务的正常使用;memcached 它没有redis那种持久化的特点,它的数据上保存到内存中,一旦服务器掉电,或者重启服务都会导致数据丢失;

  在前边的tomcat session server msm的那篇博客我们用memcached做tomcat session服务器,默认官方memcached是不支持主从同步的,为了解决memcached的高可用,我们是在客户端实现双写和调度,把一份session 多次写入后端的session服务器上,这样使得多台memcached服务器之间的数据有了冗余备份,即便集群中某一台memcached宕机以后,也不会丢失session;在用户访问上,我们在客户端上实现把调度自动切换到没有宕机的memcached上,这样又保证了线上业务的正常使用;memcached 它没有redis那种持久化的特点,它的数据上保存到内存中,一旦服务器掉电,或者重启服务都会导致数据丢失;

  memcached单机部署

  1、使用yum安装memcached

  查看memcached包简介信息

[root@slave01 ~]# yum info memcachedLoaded plugins: fastestmirrorRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fastLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comAvailable PackagesName  : memcachedArch  : x86_64Version  : 1.4.15Release  : 10.el7_3.1Size  : 85 kRepo  : base/7/x86_64Summary  : High Performance, Distributed Memory Object CacheURL   :  : BSDDescription : memcached is a high-performance, distributed memory object caching   : system, generic in nature, but intended for use in speeding up dynamic   : web applications by alleviating database load.[root@slave01 ~]# 

  提示:base仓库收录memcached的版本上1.4.15;

  安装memcached

[root@slave01 ~]# yum install -y memcachedLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.combase                          | 3.6 kB 00:00:00  docker-ce-stable                       | 3.5 kB 00:00:00  epel                          | 4.7 kB 00:00:00  extras                          | 2.9 kB 00:00:00  mariadb                         | 2.9 kB 00:00:00  updates                         | 2.9 kB 00:00:00  (1/4): extras/7/x86_64/primary_db                   | 206 kB 00:00:00  (2/4): updates/7/x86_64/primary_db                   | 3.8 MB 00:00:00  (3/4): epel/x86_64/primary_db                    | 6.9 MB 00:00:01  (4/4): epel/x86_64/updateinfo                    | 1.0 MB 00:00:05  Resolving Dependencies--> Running transaction check---> Package memcached.x86_64 0:1.4.15-10.el7_3.1 will be installed--> Processing Dependency: libevent-2.0.so.5()(64bit) for package: memcached-1.4.15-10.el7_3.1.x86_64--> Running transaction check---> Package libevent.x86_64 0:2.0.21-4.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================================================================== Package      Arch      Version         Repository    Size==================================================================================================================================Installing: memcached      x86_64      1.4.15-10.el7_3.1      base      85 kInstalling for dependencies: libevent      x86_64      2.0.21-4.el7        base      214 kTransaction Summary==================================================================================================================================Install 1 Package (+1 Dependent package)Total download size: 299 kInstalled size: 901 kDownloading packages:(1/2): libevent-2.0.21-4.el7.x86_64.rpm                 | 214 kB 00:00:00  (2/2): memcached-1.4.15-10.el7_3.1.x86_64.rpm                | 85 kB 00:00:00  ----------------------------------------------------------------------------------------------------------------------------------Total                        782 kB/s | 299 kB 00:00:00  Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : libevent-2.0.21-4.el7.x86_64                     1/2 Installing : memcached-1.4.15-10.el7_3.1.x86_64                    2/2 Verifying : memcached-1.4.15-10.el7_3.1.x86_64                    1/2 Verifying : libevent-2.0.21-4.el7.x86_64                     2/2 Installed: memcached.x86_64 0:1.4.15-10.el7_3.1                       Dependency Installed: libevent.x86_64 0:2.0.21-4.el7                         Complete![root@slave01 ~]#

  提示:从上面的安装信息可以看到memcached依赖libevent这个包,原因是Memcache 借助了操作系统的 libevent 工具做高效的读写。libevent 是个程序库,它将 Linux 的 epoll、BSD 类操作系统的 kqueue 等事件处理功能封装成统一的接口。即使对服务器的连接数增加,也能发挥高性能。memcached 使用这个 libevent 库,因此能在 Linux、BSD、Solaris 等操作系统上发挥其高性能;

  查看memcached程序环境

[root@slave01 ~]# rpm -ql memcached/etc/sysconfig/memcached/usr/bin/memcached/usr/bin/memcached-tool/usr/lib/systemd/system/memcached.service/usr/share/doc/memcached-1.4.15/usr/share/doc/memcached-1.4.15/AUTHORS/usr/share/doc/memcached-1.4.15/CONTRIBUTORS/usr/share/doc/memcached-1.4.15/COPYING/usr/share/doc/memcached-1.4.15/ChangeLog/usr/share/doc/memcached-1.4.15/NEWS/usr/share/doc/memcached-1.4.15/README.md/usr/share/doc/memcached-1.4.15/protocol.txt/usr/share/doc/memcached-1.4.15/readme.txt/usr/share/doc/memcached-1.4.15/threads.txt/usr/share/man/man1/memcached-tool.1.gz/usr/share/man/man1/memcached.1.gz[root@slave01 ~]# 

  提示:/etc/sysconfig/memcached这个文件是memcached的配置文件,/usr/bin/memcached是可执行程序;/usr/lib/systemd/system/memcached.service是memcached的unit file文件;

  查看配置文件

[root@slave01 ~]# cat /etc/sysconfig/memcached PORT="11211"USER="memcached"MAXCONN="1024"CACHESIZE="64"OPTIONS=""[root@slave01 ~]# 

  提示:PORT用于指定memcached的监听端口,默认是11211,USER用于指定启动memcached的用户,默认是memcached,MAXCONN用于指定最大连接数,默认1024.CACHESIZE用于指定最大内存,默认是64M,OPTIONS用于指定其他选项的;

  查看memcached帮助

[root@slave01 ~]# memcached -hmemcached 1.4.15-p   TCP port number to listen on (default: 11211)-U   UDP port number to listen on (default: 11211, 0 is 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: INADDR_ANY, all addresses)     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-r   maximize core file limit-u  assume identity of  (only when run as root)-m   max memory to use for items in megabytes (default: 64 MB)-M   return error on memory exhausted (rather than removing items)-c   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  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   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)-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   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.-t   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)-S   Turn on Sasl authentication-o   Comma separated list of extended or experimental options    - (EXPERIMENTAL) maxconns_fast: immediately close new    connections if over maxconns limit    - 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.[root@slave01 ~]# 

  提示:-P用于指定监听TCP端口,默认11211;-U用于指定监听UDP端口,默认也是11211,如果指定是0表示关闭UDP监听;-s用于指定unix socket文件;-a用于指定unix socket文件的权限,默认是0700的权限;-l用于指定监听的ip地址信息,默认是监听本机所有可用地址;-d 用于指定memcached运行为守护进程;-r用于限定core file的大小;-u用于指定运行memcached的用户名称;-m用于指定分配给memcached使用的内存,单位是MB;-M用于指定内存耗尽时返回错误,而不是删除;-c指定最大连接数,默认数1024;-k用于指定锁定所有分页内存。-v -vv -vvv 打印详细信息,v的数量越多,表示打印的信息就越详细;-h打印帮助信息;-i打印memcached和libevent的license信息;-p用于指定pid文件;-f用于指定chunk 增长因子,默认数1.25;-n用于指定为缓存数据项的key、value、flag设置最小分配字节数,默认是48;-L用于指定尝试使用大内存分页;-D用于指定统计报告中Key前缀和ID之间的分隔符,默认是冒号“:”;-t用于指定处理线程的数量,默认数4;-R用于指定对连续达到的客户端请求数设置一个限额,如果超过该设置,会选择另一个连接来处理请求,默认为20;-C用于指定关闭CAS;-b用户指定backlog队列长度,默认是1024;-B用于指定使用的协议,默认行为是自动协商(autonegotiate),可能使用的选项有auto、ascii、binary。-I用于指定覆盖默认的STAB页大小,默认是1M;-S用于指定打开Sasl身份验证;-o用于指定号分隔的选项,一般用于用于扩展或实验性质的选项;

  启动memecached

Memcached双主模型之repcached_第1张图片

  提示:启动memcached可以把启动的选项写到配置文件,然后使用systemctl方式启动,也可以直接在命令行使用memcached命令指定选项启动;

  到此,单机memcached就安装启动完成;

  2、memcached编译安装

  安装编译环境

root@slave01 ~]# yum groupinstall "development tools" -y Loaded plugins: fastestmirrorThere is no installed groups file.Maybe run: yum groups mark convert (see man yum)Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comResolving Dependencies--> Running transaction check---> Package autoconf.noarch 0:2.69-11.el7 will be installed--> Processing Dependency: m4 >= 1.4.14 for package: autoconf-2.69-11.el7.noarch---> Package automake.noarch 0:1.13.4-3.el7 will be installed--> Processing Dependency: perl(Thread::Queue) for package: automake-1.13.4-3.el7.noarch--> Processing Dependency: perl(TAP::Parser) for package: automake-1.13.4-3.el7.noarch---> Package bison.x86_64 0:3.0.4-2.el7 will be installed---> Package byacc.x86_64 0:1.9.20130304-3.el7 will be installed……省略部分内容……Installed: autoconf.noarch 0:2.69-11.el7       automake.noarch 0:1.13.4-3.el7    bison.x86_64 0:3.0.4-2.el7        byacc.x86_64 0:1.9.20130304-3.el7   cscope.x86_64 0:15.8-10.el7        ctags.x86_64 0:5.8-13.el7     diffstat.x86_64 0:1.57-4.el7       doxygen.x86_64 1:1.8.5-4.el7    elfutils.x86_64 0:0.176-4.el7       flex.x86_64 0:2.5.37-6.el7     gcc.x86_64 0:4.8.5-39.el7        gcc-c++.x86_64 0:4.8.5-39.el7    gcc-gfortran.x86_64 0:4.8.5-39.el7      git.x86_64 0:1.8.3.1-23.el7_8    indent.x86_64 0:2.2.11-13.el7       intltool.noarch 0:0.50.2-7.el7    libtool.x86_64 0:2.4.2-22.el7_3       patch.x86_64 0:2.7.1-12.el7_7    patchutils.x86_64 0:0.3.3-4.el7       rcs.x86_64 0:5.9.0-7.el7     redhat-rpm-config.noarch 0:9.1.0-88.el7.centos   rpm-build.x86_64 0:4.11.3-43.el7   rpm-sign.x86_64 0:4.11.3-43.el7       subversion.x86_64 0:1.7.14-14.el7   swig.x86_64 0:2.0.10-5.el7        systemtap.x86_64 0:4.0-11.el7    Dependency Installed: apr.x86_64 0:1.4.8-5.el7      apr-util.x86_64 0:1.5.2-6.el7      boost-date-time.x86_64 0:1.53.0-28.el7   boost-system.x86_64 0:1.53.0-28.el7     boost-thread.x86_64 0:1.53.0-28.el7    bzip2.x86_64 0:1.0.6-13.el7       cpp.x86_64 0:4.8.5-39.el7      dwz.x86_64 0:0.11-3.el7        dyninst.x86_64 0:9.3.1-3.el7     efivar-libs.x86_64 0:36-12.el7      emacs-filesystem.noarch 1:24.3-23.el7   gdb.x86_64 0:7.6.1-119.el7       gettext-common-devel.noarch 0:0.19.8.1-3.el7 gettext-devel.x86_64 0:0.19.8.1-3.el7    glibc-devel.x86_64 0:2.17-307.el7.1    glibc-headers.x86_64 0:2.17-307.el7.1    gnutls.x86_64 0:3.3.29-9.el7_6     kernel-debug-devel.x86_64 0:3.10.0-1127.18.2.el7  kernel-headers.x86_64 0:3.10.0-1127.18.2.el7 libdwarf.x86_64 0:20130207-4.el7      libgfortran.x86_64 0:4.8.5-39.el7    libmodman.x86_64 0:2.0.1-8.el7      libmpc.x86_64 0:1.0.1-3.el7      libproxy.x86_64 0:0.4.11-11.el7      libquadmath.x86_64 0:4.8.5-39.el7    libquadmath-devel.x86_64 0:4.8.5-39.el7    libstdc++-devel.x86_64 0:4.8.5-39.el7   m4.x86_64 0:1.4.16-10.el7       mokutil.x86_64 0:15-8.el7      mpfr.x86_64 0:3.1.1-4.el7       neon.x86_64 0:0.30.0-4.el7      nettle.x86_64 0:2.7.1-8.el7       pakchois.x86_64 0:0.4-10.el7     perl-Error.noarch 1:0.17020-2.el7     perl-Git.noarch 0:1.8.3.1-23.el7_8    perl-TermReadKey.x86_64 0:2.30-20.el7    perl-Test-Harness.noarch 0:3.28-3.el7   perl-Thread-Queue.noarch 0:3.02-2.el7    perl-

  安装依赖包

[root@slave01 ~]# yum install -y libevent-develLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.comResolving Dependencies--> Running transaction check---> Package libevent-devel.x86_64 0:2.0.21-4.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved===================================================================================================== Package      Arch    Version      Repository   Size=====================================================================================================Installing: libevent-devel    x86_64    2.0.21-4.el7    base    85 kTransaction Summary=====================================================================================================Install 1 PackageTotal download size: 85 kInstalled size: 357 kDownloading packages:libevent-devel-2.0.21-4.el7.x86_64.rpm          | 85 kB 00:00:00  Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : libevent-devel-2.0.21-4.el7.x86_64            1/1 Verifying : libevent-devel-2.0.21-4.el7.x86_64            1/1 Installed: libevent-devel.x86_64 0:2.0.21-4.el7                Complete![root@slave01 ~]# 

  下载源码文件

[root@slave01 ~]# wget 10:35:25-- memcached.org (memcached.org)... 107.170.231.145Connecting to memcached.org (memcached.org)|107.170.231.145|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 543661 (531K) [application/octet-stream]Saving to: ‘memcached-1.6.6.tar.gz’100%[===========================================================>] 543,661  19.9KB/s in 17s 2020-08-11 10:35:43 (30.5 KB/s) - ‘memcached-1.6.6.tar.gz’ saved [543661/543661][root@slave01 ~]# lsmemcached-1.6.6.tar.gz[root@slave01 ~]# 

  解压源码包,检查编译环境

[root@slave01 ~]# tar xf memcached-1.6.6.tar.gz [root@slave01 ~]# cd memcached-1.6.6/[root@slave01 memcached-1.6.6]# lsaclocal.m4 COPYING   itoa_ljust.h  NEWS      stats_prefix.cassoc.c  crawler.c  jenkins_hash.c  openbsd_priv.c   stats_prefix.hassoc.h  crawler.h  jenkins_hash.h  protocol_binary.h   storage.cauthfile.c crc32c.c  LICENSE.bipbuffer README.md     storage.hauthfile.h crc32c.h  linux_priv.c  restart.c     tAUTHORS  daemon.c  logger.c   restart.h     testapp.cbipbuffer.c depcomp   logger.h   sasl_defs.c    thread.cbipbuffer.h doc    m4     sasl_defs.h    timedrun.ccache.c  extstore.c  Makefile.am   scripts     tls.ccache.h  extstore.h  Makefile.in   sizes.c     tls.hChangeLog  freebsd_priv.c memcached.c   slab_automove.c   trace.hcompile  hash.c   memcached_dtrace.d slab_automove_extstore.c util.cconfig.guess hash.h   memcached.h   slab_automove_extstore.h util.hconfig.h.in install-sh  memcached.spec  slab_automove.h   version.m4config.sub items.c   missing    slabs.cconfigure  items.h   murmur3_hash.c  slabs.hconfigure.ac itoa_ljust.c murmur3_hash.h  solaris_priv.c[root@slave01 memcached-1.6.6]# ./configure --prefix=/usr/locat/memcachedchecking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /usr/bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking whether make supports nested variables... yes……省略部分内容……checking for xsltproc... /usr/bin/xsltprocchecking that generated files are newer than configure... doneconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating doc/Makefileconfig.status: creating config.hconfig.status: executing depfiles commands[root@slave01 memcached-1.6.6]#

  编译安装memcached

[root@slave01 memcached-1.6.6]# makemake all-recursivemake[1]: Entering directory `/root/memcached-1.6.6'Making all in docmake[2]: Entering directory `/root/memcached-1.6.6/doc'make all-ammake[3]: Entering directory `/root/memcached-1.6.6/doc'make[3]: Nothing to be done for `all-am'.make[3]: Leaving directory `/root/memcached-1.6.6/doc'make[2]: Leaving directory `/root/memcached-1.6.6/doc'make[2]: Entering directory `/root/memcached-1.6.6'gcc -std=gnu99 -DHAVE_CONFIG_H -I. -DNDEBUG -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -MT memcached-memcached.o -MD -MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f 'memcached.c' || echo './'`memcached.cmv -f .deps/memcached-memcached.Tpo .deps/memcached-memcached.Pogcc -std=gnu99 -DHAVE_CONFIG_H -I. -DNDEBUG -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -MT memcached-hash.o -MD -MP -MF .deps/memcached-hash.Tpo -c -o memcached-hash.o `test -f 'hash.c' || echo './'`hash.cmv -f .deps/memcached-hash.Tpo .deps/memcached-hash.Po……省略部分内容……gcc -std=gnu99 -DHAVE_CONFIG_H -I.  -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -MT timedrun.o -MD -MP -MF .deps/timedrun.Tpo -c -o timedrun.o timedrun.cmv -f .deps/timedrun.Tpo .deps/timedrun.Pogcc -std=gnu99 -g -O2 -pthread -pthread -Wall -Werror -pedantic -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -o timedrun timedrun.o -levent make[2]: Leaving directory `/root/memcached-1.6.6'make[1]: Leaving directory `/root/memcached-1.6.6'[root@slave01 memcached-1.6.6]# make installmake install-recursivemake[1]: Entering directory `/root/memcached-1.6.6'Making install in docmake[2]: Entering directory `/root/memcached-1.6.6/doc'make install-ammake[3]: Entering directory `/root/memcached-1.6.6/doc'make[4]: Entering directory `/root/memcached-1.6.6/doc'make[4]: Nothing to be done for `install-exec-am'. /usr/bin/mkdir -p '/usr/locat/memcached/share/man/man1' /usr/bin/install -c -m 644 memcached.1 '/usr/locat/memcached/share/man/man1'make[4]: Leaving directory `/root/memcached-1.6.6/doc'make[3]: Leaving directory `/root/memcached-1.6.6/doc'make[2]: Leaving directory `/root/memcached-1.6.6/doc'make[2]: Entering directory `/root/memcached-1.6.6'make[3]: Entering directory `/root/memcached-1.6.6' /usr/bin/mkdir -p '/usr/locat/memcached/bin' /usr/bin/install -c memcached '/usr/locat/memcached/bin' /usr/bin/mkdir -p '/usr/locat/memcached/include/memcached' /usr/bin/install -c -m 644 protocol_binary.h '/usr/locat/memcached/include/memcached'make[3]: Leaving directory `/root/memcached-1.6.6'make[2]: Leaving directory `/root/memcached-1.6.6'make[1]: Leaving directory `/root/memcached-1.6.6'[root@slave01 memcached-1.6.6]# ll /usr/locat/memcached/total 0drwxr-xr-x 2 root root 23 Aug 11 10:39 bindrwxr-xr-x 3 root root 23 Aug 11 10:39 includedrwxr-xr-x 3 root root 17 Aug 11 10:39 share[root@slave01 memcached-1.6.6]# 

  启动memcached

Memcached双主模型之repcached_第2张图片

  到此memcached的编译安装就完成了

  3、使用python连接memcached写入数据

[root@slave01 ~]# cat test.py import memcachem = memcache.Client(['127.0.0.1:11211'], debug=True)for i in range(100): m.set("key%d" % i,"v%d" % i)# ret = m.get('key%d' % i)# print ret[root@slave01 ~]#

  提示:以上脚本主要作用是连接到memcache后循环写入100个key;

  运行脚本,然后查看memcached上是否存储的有我们写入的key数据?

[root@slave01 ~]# python test.py[root@slave01 ~]# telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.get key1VALUE key1 0 2v1ENDget k12ENDget key12VALUE key12 0 3v12ENDquitConnection closed by foreign host.[root@slave01 ~]# 

  提示:可以看到运行脚本以后,连接到memcached里是可以取出我们写入到key对应的数据;

  部署 repcached  实验环境说明 
名称 IP地址 端口
master01 192.168.16.196 11211
master02 192.168.16.197

11211

                    在master01上编译安装repcached
[root@master01 ~]# lsmemcached-1.2.8-repcached-2.2.tar.gz[root@master01 ~]# tar xf memcached-1.2.8-repcached-2.2.tar.gz [root@master01 ~]# lsmemcached-1.2.8-repcached-2.2 memcached-1.2.8-repcached-2.2.tar.gz[root@master01 ~]# cd memcached-1.2.8-repcached-2.2/[root@master01 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/repcached --enable-replicationchecking build system type... Invalid configuration `x86_64-unknown-linux-': machine `x86_64-unknown-linux' not recognizedconfigure: error: /bin/sh ./config.sub x86_64-unknown-linux- failed[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:遇到以上错误,上因为没有安装gcc编译导致的,通常在Linux上做编译操作,我们需要先安装好编译环境;

  安装编译环境

[root@master01 memcached-1.2.8-repcached-2.2]# yum groupinstall "development tools" -y 

  再次编译

[root@master01 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/repcached --enable-replicationchecking build system type... x86_64-unknown-linux-gnuchecking host system type... x86_64-unknown-linux-gnuchecking target system type... x86_64-unknown-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /usr/bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables... checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3checking whether gcc and cc understand -c and -o together... yeschecking for a BSD-compatible install... /usr/bin/install -cchecking for libevent directory... configure: error: libevent is required. You can get it from  If it's already installed, specify its path using --with-libevent=/dir/[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:上述错误是没有检查到libevent,解决办法安装依赖包libevent-devel包

  安装依赖包

[root@master01 memcached-1.2.8-repcached-2.2]# yum install libevent-devel -y

  再次检查

[root@master01 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/repcached --enable-replicationchecking build system type... x86_64-unknown-linux-gnuchecking host system type... x86_64-unknown-linux-gnuchecking target system type... x86_64-unknown-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /usr/bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables... checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3checking whether gcc and cc understand -c and -o together... yeschecking for a BSD-compatible install... /usr/bin/install -cchecking for libevent directory... (system)checking for library containing socket... none requiredchecking for library containing gethostbyname... none requiredchecking for library containing mallinfo... none requiredchecking for daemon... yeschecking how to run the C preprocessor... gcc -Echecking for grep that handles long lines and -e... /usr/bin/grepchecking for egrep... /usr/bin/grep -Echecking for ANSI C header files... yeschecking for sys/types.h... yeschecking for sys/stat.h... yeschecking for stdlib.h... yeschecking for string.h... yeschecking for memory.h... yeschecking for strings.h... yeschecking for inttypes.h... yeschecking for stdint.h... yeschecking for unistd.h... yeschecking for stdbool.h that conforms to C99... yeschecking for _Bool... yeschecking for an ANSI C-conforming const... yeschecking malloc.h usability... yeschecking malloc.h presence... yeschecking for malloc.h... yeschecking for struct mallinfo.arena... yeschecking for socklen_t... yeschecking for endianness... littlechecking for mlockall... yeschecking for getpagesizes... nochecking for memcntl... noconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating doc/Makefileconfig.status: creating config.hconfig.status: executing depfiles commands[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:检查编译环境没有问题后,就可以执行编译和安装了;

  编译安装

[root@master01 memcached-1.2.8-repcached-2.2]# make && make install make all-recursivemake[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2'Making all in docmake[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'make[2]: Nothing to be done for `all'.make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2'gcc -DHAVE_CONFIG_H -I. -DNDEBUG -g -O2 -MT memcached-memcached.o -MD -MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f 'memcached.c' || echo './'`memcached.cmemcached.c: In function ‘add_iov’:memcached.c:696:30: error: ‘IOV_MAX’ undeclared (first use in this function)   if (m->msg_iovlen == IOV_MAX ||        ^memcached.c:696:30: note: each undeclared identifier is reported only once for each function it appears inmake[2]: *** [memcached-memcached.o] Error 1make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'make[1]: *** [all-recursive] Error 1make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'make: *** [all] Error 2[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:这个错误是memcached.c中的宏定义问题,处理办法就是把memcached.c中的#if defined(__FreeBSD__) || defined(__APPLE__) 和#endif注释掉即可如下

Memcached双主模型之repcached_第3张图片

  提示:修改了上面的memcached.c 以后,再次编译就没有问题了

  再次编译

[root@master01 memcached-1.2.8-repcached-2.2]# make && make installmake all-recursivemake[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2'Making all in docmake[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'make[2]: Nothing to be done for `all'.make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2'gcc -DHAVE_CONFIG_H -I. -DNDEBUG -g -O2 -MT memcached-memcached.o -MD -MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f 'memcached.c' || echo './'`memcached.cmv -f .deps/memcached-memcached.Tpo .deps/memcached-memcached.Pogcc -DHAVE_CONFIG_H -I. -DNDEBUG -g -O2 -MT memcached-slabs.o -MD -MP -MF .deps/memcached-slabs.Tpo -c -o memcached-slabs.o `test -f 'slabs.c' || echo './'`slabs.cmv -f .deps/memcached-slabs.Tpo .deps/memcached-slabs.Po……省略部分内容……mv -f .deps/replication.Tpo .deps/replication.Pogcc -g -O2 -o memcached-debug memcached.o slabs.o items.o assoc.o thread.o stats.o replication.o -leventmake[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'Making install in docmake[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2/doc'make[2]: Nothing to be done for `install-exec-am'.test -z "/usr/local/repcached/share/man/man1" || /usr/bin/mkdir -p "/usr/local/repcached/share/man/man1" /usr/bin/install -c -m 644 './memcached.1' '/usr/local/repcached/share/man/man1/memcached.1'make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2/doc'make[1]: Entering directory `/root/memcached-1.2.8-repcached-2.2'make[2]: Entering directory `/root/memcached-1.2.8-repcached-2.2'test -z "/usr/local/repcached/bin" || /usr/bin/mkdir -p "/usr/local/repcached/bin" /usr/bin/install -c 'memcached' '/usr/local/repcached/bin/memcached' /usr/bin/install -c 'memcached-debug' '/usr/local/repcached/bin/memcached-debug'make[2]: Nothing to be done for `install-data-am'.make[2]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'make[1]: Leaving directory `/root/memcached-1.2.8-repcached-2.2'[root@master01 memcached-1.2.8-repcached-2.2]# ll /usr/local/repcached/total 0drwxr-xr-x 2 root root 46 Aug 10 23:26 bindrwxr-xr-x 3 root root 17 Aug 10 23:26 share[root@master01 memcached-1.2.8-repcached-2.2]# ll /usr/local/repcached/bin/total 472-rwxr-xr-x 1 root root 233096 Aug 10 23:26 memcached-rwxr-xr-x 1 root root 248008 Aug 10 23:26 memcached-debug[root@master01 memcached-1.2.8-repcached-2.2]# 

  提示:可以看到对应目录下已经有二进制文件产生,到此repcached就编译安装好了;

  验证repcached/bin/memcached是否可执行?

[root@master01 ~]# /usr/local/repcached/bin/memcached -hmemcached 1.2.8repcached 2.2-p   TCP port number to listen on (default: 11211)-U   UDP port number to listen on (default: 11211, 0 is 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-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-R   Maximum number of requests per event    limits the number of requests process for a given con nection    to prevent starvation. default 20-b   Set the backlog queue limit (default 1024)-x  hostname or IP address of peer repcached-X   TCP port number for replication (default: 11212)[root@master01 ~]# 

  启动memcached

Memcached双主模型之repcached_第4张图片

  提示:通过 repcached 安装的 memcached 命令启动 memcache 服务并实现 memcache 主备结构,其中-x 为对方即主 memcache 的 IP,-X 为本地启动的用数据同步的端口;

  在master02上编译安装repcached的过程同master01上一样的;如果你不想那么麻烦的再去编译可以把master01上编译好的二进制文件拷到master02上执行也是可以的

  从master01上拷贝二进制文件到master02上

[root@master01 ~]# scp -r /usr/local/repcached/ master02:/usr/local/memcached.1               100% 4290  1.9MB/s 00:00 memcached               100% 228KB 4.9MB/s 00:00 memcached-debug              100% 242KB 4.9MB/s 00:00 [root@master01 ~]#

  在master02上启动memcached

[root@master02 ~]# /usr/local/repcached/bin/memcached -d -m 2048 -p 11211 -u root -c 2048 -x 192.168.16.196 -X 52113/usr/local/repcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory[root@master02 ~]#

  提示:上述报错主要原因上没有安装libevent-devel,所以在执行memcached时加载共享动态库文件找不到,解决办法安装libevent-devel即可

  在master02上安装依赖包libevent-devel

[root@master02 ~]# yum install -y libevent-devel

  再次启动memcached

Memcached双主模型之repcached_第5张图片

Memcached双主模型之repcached_第6张图片

  提示:在master02上指定master01上的用于复制监听端口,如果master02连接上master01,那么在master02和master01上就不会在监听我们手动指定的用于复制的端口;如果你在master02上还能够看到手动指定的复制端口,说明master01和master02没有建立连接;

  验证:往master01上写入数据看看是否能够同步到master02上呢?

Memcached双主模型之repcached_第7张图片

  提示:可以看到在master01上写数据,在master02上是能够正常读取到的;

  验证在master02上写数据,master01上是否可读取呢?

Memcached双主模型之repcached_第8张图片

  提示:可以看到在master02上写数据,也能够及时的同步到master01上;到此基于repcached双主模型的memcached就搭建好了,我们不管往那个master上写入数据,它都会立刻同步数据到对方到memcached中去;

Memcached双主模型之repcached
文章转载:http://www.shaoqun.com/a/464210.html