Memcache主从复制

repcached介绍
repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步,如果 master坏掉, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉, master也会侦测到连接断,它就会重新 listen等待新的 slave加入.

1.安装
在两台服务器上分别安装memcached服务,另注本文libevent的版本为:libevent-1.4.13,下载地址:http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
repcached有两种方式:
方式一、下载对应的repcached版本
#wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
#tar zxf memcached-1.2.8-repcached-2.2.tar.gz
#cd memcached-1.2.8-repcached-2.2

2.下载对应patch版本
#wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz
#gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1
#./configure –enable-replication
# make
# make install

3.启动:
启动master
#/usr/local/bin/memcached -d -m 64 -p 12000 -u root -l 0.0.0.0 -P /tmp/memcached.pid
replication: listen (master监听)

启动salve
#/usr/local/bin/memcached -d -m 64 -p 12000 -u root -l 0.0.0.0 -x 192.168.3.100 -X 11212 -P /tmp/memcached.pid
replication: connect (peer=192.168.3.100:11212)
replication: marugoto copying
replication: start

启动正常后,master将accept。

启动脚本

MASTER

#!/bin/bash
# memcached service.
# chkconfig: 345 35 75
# description: a mem cache server
case "$1" in
start)
/usr/local/bin/memcached -d -m 64 -p 12000 -u root -l 0.0.0.0 -P /tmp/memcached.pid
echo "memcached start ok"
;;
restart)
ps aux |grep /usr/local/memcached/bin/memcached|grep -v grep|awk '{print $2}'|awk '{printf("%s ",$1)}'|xargs kill -9
/usr/local/bin/memcached  -d -m 64 -p 12000 -u root -l 0.0.0.0 -P /tmp/memcached.pid
echo "memcached restart ok"
;;
stop)
ps aux |grep /usr/local/bin/memcached|grep -v grep|awk '{print $2}'|awk '{printf("%s ",$1)}'|xargs kill -9
echo "memcached stop ok"
;;
esac
exit 0

 

SLAVE

#!/bin/bash
# memcached service.
# chkconfig: 345 35 75
# description: a mem cache server
case "$1" in
start)
/usr/local/bin/memcached -d -m 64 -p 12000 -u root -l 0.0.0.0 -x 192.168.3.100 -X 11212 -P /tmp/memcached.pid
echo "memcached start ok"
;;
restart)
ps aux |grep /usr/local/memcached/bin/memcached|grep -v grep|awk '{print $2}'|awk '{printf("%s ",$1)}'|xargs kill -9
/usr/local/bin/memcached -d -m 64 -p 12000 -u root -l 0.0.0.0 -x 192.168.3.100 -X 11212 -P /tmp/memcached.pid
echo "memcached restart ok"
;;
stop)
ps aux |grep /usr/local/bin/memcached|grep -v grep|awk '{print $2}'|awk '{printf("%s ",$1)}'|xargs kill -9
echo "memcached stop ok"
;;
esac
exit 0

 

4.测试:
操作master
#telnet 192.168.3.100 11211
#set key1 0 0 3
test

查看slave
#telnet 192.168.3.101 11213
#get key1
如果正常显示test,则表示repcached配置成功

5.应用:
可以实现cache冗余,避免因cache服务器down掉而导致数据丢失。

注意:如果master down机,slave接管并成为master,这时down机的master只能启用slave,他们之间互换角色,才能保持复制功能。换句话说,master没有抢占功能。可以通过LVS来配置成自动切换

配置PHP session使用MEMCACHE

[Session]
; Handler used to store/retrieve data.
http://php.net/session.save-handler
;session.save_handler = files
session.save_handler = "memcache"
memcache.hash_strategy = "consistent"
session.save_path = "tcp://192.168.3.1:12000"

 

HAPROXY 配置文件

global
        log 127.0.0.1   local0
        #log 127.0.0.1  local0
        #log 127.0.0.1  local1 notice
        #log loghost    local0 info
        maxconn 4096
        chroot /opt/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet
defaults
        log     global
        #mode   http
        #option httplog
        option  dontlognull
        retries 3
#       redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
      
listen  MEMCACHED 0.0.0.0:12000
        mode tcp           
        option  persist              
        balance roundrobin           
        server  MEMCACHE1 192.168.3.100:12000 check inter 2000 fall 3
        server  MEMCACHE2 192.168.3.101:12000 check inter 2000 fall 3
listen  MYSQLD 0.0.0.0:3306
        mode tcp
        option  persist
        balance roundrobin
        server  MYSQL1 192.168.3.100:3306 check inter 2000 fall 3
#       server  MYSQL2 192.168.3.101:3306 check inter 2000 fall 3
listen  HTTP 0.0.0.0:80
        mode http
        option  forwardfor
        option httplog
       #option  httpchk /index.html
        stats   uri     /haproxy-stats                              
        stats auth admin:xxxxxx
        option  persist
        balance roundrobin
        cookie  SERVERID insert indirect nocache
        server  WEB1 192.168.3.100:80 cookie server01 check inter 2000 fall 3
        server  WEB2 192.168.3.101:80 cookie server02 check inter 2000 fall 3
        capture cookie PHPSESSION len 32
        srvtimeout      20000
        option  httpclose               # disable keep-alive
        option  checkcache              # block response if set-cookie & cacheable
        rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address
listen  HTTPS 0.0.0.0:443                                                                                                       
        mode tcp
        option ssl-hello-chk                                                                                                                  
        option  persist                                                                                                            
        balance roundrobin                                                                                                         
        server  WEB1 192.168.3.100:443 check inter 2000 fall 3                                                                  
        server  WEB2 192.168.3.101:443 check inter 2000 fall 3  
       
        #errorloc       502      http://192.168.114.58/error502.html
        #errorfile      503     /etc/haproxy/errors/503.http

效果
点击查看原图
点击查看原图


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向良玉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值