Memcache master-slave Replication

Source: Internet
Author: User
Tags php session haproxy

Repcached Introduction

Repcached is a memcached replication function developed by Japanese people. It is a single master single slave solution, but its Master/Slave can be read and written and can be synchronized with each other, if the master node breaks down and slave detects that the connection is disconnected, it will automatically listen and become the master node. If the Server Load balancer breaks down, the master node will also detect the connection disconnection, it will relisten and wait for the new slave to join.

1. Install

Install the memcached service on both servers, and note that the libevent version is: libevent-1.4.13,: http://www.monkey.org /~ Provos/libevent-1.4.13-stable.tar.gz

There are two repcached methods:

Method 1: Download the corresponding repcached version

# 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. Download the corresponding patch version

# 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. Start:

Start master

#/Usr/local/bin/memcached-D-M 64-P 12000-u root-l 0.0.0.0-P/tmp/memcached. PID

Replication: Listen (Master listener)

Start 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

After the master instance is started normally, the master instance enters the accept state.

Start script

Master

#! /Bin/bash

# Memcached service.

# Chkconfig: 345 35 75

# Description: A mem cache servercase "$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"

;;

Esacexit 0

 

Slave

#! /Bin/bash

# Memcached service.

# Chkconfig: 345 35 75

# Description: A mem cache servercase "$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"

;;

Esacexit 0

 

4. test:

Master operations

# Telnet 192.168.3.100 11211

# Set key1 0 0 3

Test

View slave

# Telnet 192.168.3.101 11213

# Get key1

If test is displayed normally, the repcached configuration is successful.

5. application:

Cache redundancy can be implemented to avoid data loss caused by the down of the cache server.

Note: If the master is down, slave takes over and becomes the master, then the master of the down machine can only enable slave and switch roles between them to maintain the replication function. In other words, the master has no preemption function. You can use LVS to configure automatic switching.

Configure PHP session to use memcache

[Session]

; Handler used to store/retrieve data.

Http://php.net/session.save-handler

; Session. save_handler = filessession. save_handler = "memcache"

Memcache. hash_strategy = "consistent"

Session. save_path = "TCP: // 192.168.3.1: 12000"

 

Haproxy configuration file

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

# Quietdefaults

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 addresslisten 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

Effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.