使用repcached可以实现memcached的复制功能:
它是一个单master但slave的方案。但是它的master/slave都是可读写的,而且可以相互同步。
如果master down掉,slave侦测到连接断了,它会自动listen而成为master。如果slave down掉,master也会侦测到连接断了,它会重新listen等待新的slave加入。
下载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 --enable-replication --program-transform-name=s/memcached/repcached/
进入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 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