使用repcached可以实现memcached的复制功能:
它是一个单master但slave的方案。但是它的master/slave都是可读写的,而且可以相互同步。
如果master down掉,slave侦测到连接断了,它会自动listen而成为master。如果slave down掉,master也会侦测到连接断了,它会重新listen等待新的slave加入。
安装
Ubuntu下安装
下载repcached的源代码压缩包,解压缩:
<span style="font-family: Arial, Helvetica, sans-serif;">tar zxvf memcached-1.2.8-repcached-2.2.1.tar.gz</span>
cd memcached-1.2.8-repcached-2.2.1
执行configure,关于configure的参数,第一个选项是使复制有效的选项,第二个选项指明了安装命令的名字是repcached,而不是memcached。这样设置可以在保留memcached的基础上安装repcached。
./configure --enable-replication --program-transform-name=s/memcached/repcached/
执行make命令,会发生错误;
进入repcached的解压缩目录,打开文件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
在Ubuntu下,去掉 #if 块:
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
# define IOV_MAX 1024
#endif
执行make:
make
执行 make install:
sudo make install
安装完成。
测试
首先使用11211号端口启动作为主服务器的repcached:
parallels@ubuntu:~$ repcached -p 11211 -v
replication: listen
replication:accept
(到slave启动后,输出 replication:accept)
parallels@ubuntu:~$ repcached -p 11212 -x localhost -v
replication: connect (peer=127.0.0.1:11212)
replication: marugoto copying
replication: start
parallels@ubuntu:~$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set hello 0 0 5
world
STORED
get hello
VALUE hello 0 5
world
END
parallels@ubuntu:~$ telnet localhost 11212
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get hello
VALUE hello 0 5
world
END
也可以对11212端口的slave repcached进行写处理,这些数据也可以从11211端口的master repcached读取出来。