1.repcached介紹shell
使用repcached能夠實現memcached的複製功能:memcached
它是一個單master單slave的方案。可是它的master/slave都是可讀寫的,並且能夠相互同步。若是master down掉,slave偵測到鏈接斷了,它會自動listen而成爲master。若是slave down掉,master也會偵測到鏈接斷了,它會從新listen等待新的slave加入。測試
2.安裝
spa
安裝文件準備
code
libevent-2.0.22-stable.tar.gzorm
memcached-1.4.22.tar.gzip
memcached-1.2.8-repcached-2.2.1.tar.gzget
2.1安裝libevent (memcached依賴libevent)同步
tar –zxvf libevent-2.0.22-stable.tar.gz
it
cd libevent-2.0.22-stable
./configure –prefix=/usr/local
make
make install
測試libevent是否安裝成功:
Ls –al /usr/local/lib | grep libevent
2.2安裝memcached
在配置時須要指定libevent的安裝路徑即./configure–with-libevent=/usr/local
tar xvf memcached-1.4.22.tar.gz
./configure–with-libevent=/usr/local
make
make install
測試memcached是否安裝成功:
ls –al /usr/local/bin/memcached
2.3安裝repcached
tar -zxvf memcached-1.2.8-repcached-2.2.1.tar.gz
cd memcached-1.2.8-repcached-2.2.1
./configure --enable-replication
make
make install
注:
執行make命令,會發生錯誤,進入repcached的解壓縮目錄,打開文件memcached.c,找到以下的定義,去掉 第3行和第5行的#if 塊(#if defined(__FreeBSD__) || defined(__APPLE__)),以下:
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */ #ifndef IOV_MAX #if defined(__FreeBSD__) || defined(__APPLE__) # define IOV_MAX 1024 #endif #endif
2.4啓動master
/usr/local/bin/memcached -p 11211 -v -d -u root
注:該主機ip爲192.168.1.254
2.5啓動slave
/usr/local/bin/ memcached -p 11211 -x 192.168.1.254 -v -d -u root
注:該主機ip爲192.168.1.251
slave的啓動和master相似,只是多了-x參數,指定複製的ip,若是複製端口不是11212,則須要-X參數指定
3.測試
經過telnet 192.168.1.254 11211 鏈接master repcached保存數據
set hello 0 0 5
hello
而後經過telnet 192.168.1.251 11211鏈接slave repcached,確認剛纔經過masterrepcached保存的數據是否已經被複制了:
get hello