一、repcached特点:

    1、互相同步

    2、只支持单主单从

    3、没有角色抢占功能(故障恢复后只能作为从服务器加入复制)

    

二、安装依赖(主和从服务器都要安装)

yum install linevent-devel


三、安装repcache

[root@master ~]# wget  -O /usr/local/src
[root@master ~]# cd /usr/local/src
[root@master src]# tar -zxvf memcached-1.2.8-repcached-2.2.tar.gz
[root@master src]# cd memcached-1.2.8-repcached-2.2
[root@master memcached-1.2.8-repcached-2.2]# ./configure --enable-replication --program-transform-name=s/memcached/repcached/
[root@master memcached-1.2.8-repcached-2.2]# make && make install
[root@master memcached-1.2.8-repcached-2.2]# cd ~


四、启动repcache(主和从服务器)


4.1、主服务器

[root@master ~]# repcached -p 11211 -v -d -u root
[root@master ~]# replication: listen

master上面的消息表示repcache的master已经启动,处于复制的监听状态,等待从服务器的连接。当slave服务器启动之后master服务器会提示“replication: accept”


4.2、从服务器

[root@slave ~]# repcached -p 11211 -x 192.168.1.20 -v -d -u root
[root@slave ~]# replication: connect (peer=192.168.1.20:11212)
replication: marugoto copying
replication: start

slave上面的消息表示已经开始了复制


五、repcache的参数说明

[root@master ~]# repcached -help
memcached 1.2.8
repcached 2.2
-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 is INDRR_ANY
-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 is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c <num>      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 <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)
-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
-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 <ip_addr>  hostname or IP address of peer repcached
-X <num>      TCP port number for replication (default: 11212)


六、测试

6.1、测试主写从读&从写主读


6.1.1、主写

[root@master ~]# telnet 192.168.1.20 11211
Trying 192.168.1.20...
Connected to 192.168.1.20.
Escape character is '^]'.
set name 0 0 5
world
STORED


6.1.2、从写

[root@slave ~]# telnet 192.168.1.21 11211
Trying 192.168.1.21...
Connected to 192.168.1.21.
Escape character is '^]'.
set age 0 0 5
myage
STORED


6.1.3、从读

[root@slave ~]# telnet 192.168.1.21 11211
Trying 192.168.1.21...
Connected to 192.168.1.21.
Escape character is '^]'.
get name
VALUE name 0 5
world
END
get age
VALUE age 0 5
myage
END


6.1.4、主读

[root@master ~]# telnet 192.168.1.20 11211
Trying 192.168.1.20...
Connected to 192.168.1.20.
Escape character is '^]'.
get age
VALUE age 0 5
myage
END
get name
VALUE name 0 5
world
END

不管是在master上还是从上写数据,都可以同步到另一台repcache服务器


6.2、测试master故障


6.2.1、杀掉master的repcached进程

[root@master ~]# ps -ef |grep repcached
root     17488     1  0 15:03 ?        00:00:00 repcached -p 11211 -v -d -u root
root     17528 16031  0 15:19 pts/2    00:00:00 grep repcached
[root@master ~]# kill 17488
[root@master ~]# replication: cleanup start
replication: close
replication: cleanup complete


这时从服务器会提示:

[root@slave ~]# replication: close
replication: listen

表示slave服务器接管了master的角色并等待slave来连接,即从服务器变为主服务器


6.2.2、恢复主服务器的repcached进程

[root@master ~]# repcached -p 11211 -x 192.168.1.21 -v -d -u root
[root@master ~]# replication: connect (peer=192.168.1.21:11212)
replication: marugoto copying
replication: start

注意:主服务器恢复时必须以从服务器的角色恢复,即必须使用-x指定主的ip。


主服务器会提示

replication: accept
replication: marugoto start
replication: marugoto 2
replication: marugoto owari

故障及恢复的过程介绍完了,下面验证故障恢复后的数据是否完整


6.3、查看故障恢复后的主和从服务器的数据


6.3.1、查看主服务器的数据

[root@slave ~]# telnet 192.168.1.21 11211
Trying 192.168.1.21...
Connected to 192.168.1.21.
Escape character is '^]'.
get name
VALUE name 0 5
world
END
get age
VALUE age 0 5
myage
END


6.3.2、查看从服务器的数据

[root@master ~]# telnet 192.168.1.20 11211
Trying 192.168.1.20...
Connected to 192.168.1.20.
Escape character is '^]'.
get name
VALUE name 0 5
world
END
get age
VALUE age 0 5
myage
END