memcached复制-repcached

1.前言

由于memcached把数据都放到内存里,因此性能是极高的,正因为如此,不可避免会造成数据丢失,repcached就派上用场了,它可以实现memcached的主从复制

2.安装repcached

下面采用memcached和repcached一起安装的方式

$ cd usr/local/src
$ wget https://downloads.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz
$ tar -zxvf memcached-1.2.8-repcached-2.2.1.tar.gz
$ cd memcached-1.2.8-repcached-2.2.1
$ ./configure --enable-replication --program-transform-name=s/memcached/repcached/
error: 'IOV_MAX' undeclared (first use in this function) #centos编译会报这个错,需要修改代码
$ vi memcached.c
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
#if defined(__FreeBSD__) || defined(__APPLE__) 
#define IOV_MAX 1024
#endif
#endif
$ make && make install

3.测试复制

为了方便,下面的测试是在同一台机器上开启两个端口来进行主从复制的

$ memcached -p 11211 -d -v #-d表示守护进程 -v是打印日志
replication: listen
replication: accept

#按下Ctrl+c,在本机添加从服务器,-x是主cache的ip
$ memcached -p 11212 -d -x 192.168.56.10 -v
replication: connect (peer=192.168.56.10:11212)
replication: marugoto copying
replication: start

#分别连接测试主从同步
$ telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set myname 0 0 6
pigfly
STORED
quit
Connection closed by foreign host.
$ telnet localhost 11212
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get myname
VALUE myname 0 6
pigfly #从cache已经能获取到主cache的数据了,说明主复制到从没有问题
END
set age 0 0 2
23
STORED
quit
Connection closed by foreign host.
$ telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get age
VALUE age 0 2
23 #这里主cache也拿到了从cache的数据,说明从复制到主也没问题
END
quit
Connection closed by foreign host.
$ telnet localhost 11212
Trying ::1...
Connected to localhost.
Escape character is '^]'.
delete age
DELETED
quit
Connection closed by foreign host.
$ telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get age #删除也是同步的
END

4.总结

简单配置几下就可以实现memcached的备份,是不是很开森?!考虑下面的一些情况:

  • repcached只支持memcached1.2.x,好几年没更新过了
  • 当有一台memcache宕机,repcached不会自动帮你切换到另外一台,要手动切换,考虑用缓存代理+主从复制

 

    原文作者:memcached
    原文地址: https://www.cnblogs.com/justlikeheaven/p/7778259.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞