Home Database Mysql Tutorial Repcached实现Memcached主从复制功能

Repcached实现Memcached主从复制功能

Jun 07, 2016 pm 02:55 PM
memcached copy accomplish

Repcached 实现Memcached 主从复制功能 工作原理 repcached实现了memcached复制的功能,它是一个单master单slave的方案,但master/slave都是可读写的,而且可以相互同步,如果master坏掉slave侦测到连接断了,它会自动listen而成为master,这时坏掉的master

Repcached实现Memcached主从复制功能



工作原理


repcached实现了memcached复制的功能,它是一个单master单slave的方案,但master/slave都是可读写的,而且可以相互同步,如果master坏掉slave侦测到连接断了,它会自动listen而成为master,这时坏掉的master只能启用为slave,它们之间互换角色,才能保持复制功能,换句话说,master没有抢占功能;而如果slave坏掉,master也会侦测到连接断,它就会重新listen等待新的slave加入。

 

应用场景


用memcached做session共享或其它服务时会存在memcached的单点故障,如果memcached宕机,那么整个系统用户无法登陆(session)。

基于这种情况,采用repcached做memcached的主从冗余。

 

Repcached下载地址


http://sourceforge.net/projects/repcached/files/repcached/

 

Repcached安装方式


Repcached有两种安装方式:

    1.补丁版本安装  
    先安装相应版本的memcached,然后对应版本的Repcached补丁。

    2.整合版本安装  
    直接安装整合版本的memcached

 

方式一:补丁版本安装

1. 安装Memcache,相关安装方法可以参见博文:  
http://ultrasql.blog.51cto.com/9591438/1632179

2. 下载对应的repcached版本补丁安装文件:  
假设安装的memcahced版本为1.2.8,下载针对该版本最新的补丁:    

wget http://softlayer-dal.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/repcached-2.2.1-1.2.8.patch.gz    
gzip -cd ../repcached-2.2.1-1.2.8.patch.gz | patch -p1    
./configure --enable-replication    
make && make install
Copy after login

方式二:整合版本安装

1. 安装libevent:

cd /tmp    
wget http://downloads.sourceforge.net/levent/libevent-2.0.22-stable.tar.gz    
tar zxvf libevent-2.0.22-stable.tar.gz    
cd libevent-2.0.22-stable    
./configure --prefix=/usr/local/lib    
make && make install
Copy after login

2. 将libevent的库文件添加到动态库中:

vi /etc/ld.so.conf
Copy after login


在最后添加如下行:
/usr/local/lib //此处为要添加的libevent库目录
重新加载动态lib库

ldconfig
Copy after login


注意:如果无此步骤,在启动memcached时,会提示看不到libevent的库文件。

3. 测试libevent是否安装成功:

ls -al /usr/lib | grep libevent-
Copy after login

4. 创建启动帐号:

groupadd memcached    
useradd -g memcached memcached
Copy after login

5. 创建PID进程目录并修改所有者:

mkdir /var/run/memcached    
chown -R memcached.memcached /var/run/memcached
Copy after login

6. 安装整合memcached-repcached包:

cd /tmp    
wget http://softlayer-dal.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz    
cp memcached-1.2.8-repcached-2.2.1.tar.gz /usr/local    
cd /usr/local    
tar zxvf memcached-1.2.8-repcached-2.2.1.tar.gz    
mv memcached-1.2.8-repcached-2.2.1 memcached    
cd memcached    
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/lib --enable-replication --enable-64bit
Copy after login


注意:默认memcached单个进程只支持到2G内存,需要更大内存支持的话,需要打开64位支持,编译的时候加参数:
--enable-64bit


make && make install
Copy after login
Copy after login


提示编译出错:

make all-recursive    
make[1]: Entering directory `/usr/local/memcached'    
Making all in doc    
make[2]: Entering directory `/usr/local/memcached/doc'    
make[2]: Nothing to be done for `all'.    
make[2]: Leaving directory `/usr/local/memcached/doc'    
make[2]: Entering directory `/usr/local/memcached'    
gcc -DHAVE_CONFIG_H -I. -DNDEBUG -m64 -g -O2 -MT memcached-memcached.o -MD     
MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f     
memcached.c' || echo './'`memcached.c    
memcached.c: In function ‘add_iov’:    
memcached.c:697: error: ‘IOV_MAX’ undeclared (first use in this function)    
memcached.c:697: error: (Each undeclared identifier is reported only once    
memcached.c:697: error: for each function it appears in.)    
make[2]: *** [memcached-memcached.o] Error 1    
make[2]: Leaving directory `/usr/local/memcached'    
make[1]: *** [all-recursive] Error 1    
make[1]: Leaving directory `/usr/local/memcached'    
make: *** [all] Error 2
Copy after login


解决方案:

vi memcached.c
Copy after login


将下面的几行

/* FreeBSD 4.x doesn't have IOV_MAX exposed. */    
#ifndef IOV_MAX    
#if defined(__FreeBSD__) || defined(__APPLE__)    
# define IOV_MAX 1024    
#endif    
#endif
Copy after login


修改为

/* FreeBSD 4.x doesn't have IOV_MAX exposed. */    
#ifndef IOV_MAX    
# define IOV_MAX 1024    
#endif
Copy after login


重新编译和安装:

make && make install
Copy after login
Copy after login


查看帮助:

./bin/memcached -h
Copy after login


memcached 1.2.8    
repcached 2.2.1    
-p <num> TCP port number to listen on (default: 11211)    
-U <num> UDP port number to listen on (default: 11211, 0 is off)    
-s <file> unix socket path to listen on (disables network support)    
-a <mask> access mask for unix socket, in octal (default 0700)    
-l <ip_addr> interface to listen on, default is INDRR_ANY    
-d run as a daemon    
-r maximize core file limit    
-u <username> assume identity of <username> (only when run as root)    
-m <num> max memory to use for items in megabytes, default is 64 MB    
-M return error on memory exhausted (rather than removing items)    
-c <num> max simultaneous connections, default is 1024    
-k lock down all paged memory. Note that there is a    
limit on how much memory you may lock. Trying to    
allocate more than that would fail, so be sure you    
set the limit correctly for the user you started    
the daemon with (not for -u <username> user;    
under sh this is done with 'ulimit -S -l NUM_KB').    
-v verbose (print errors/warnings while in event loop)    
-vv very verbose (also print client commands/reponses)    
-h print this help and exit    
-i print memcached and libevent license    
-P <file> save PID in <file>, only used with -d option    
-f <factor> chunk size growth factor, default 1.25    
-n <bytes> minimum space allocated for key+value+flags, default 48    
-R Maximum number of requests per event    
limits the number of requests process for a given con nection    
to prevent starvation. default 20    
-b Set the backlog queue limit (default 1024)    
-x <ip_addr> hostname or IP address of peer repcached    
-X <num:num> TCP port number for replication. <listen:connect> (default: 11212)
Copy after login


修改memcached目录所有者:

cd ..    
chown -R memcached.memcached memcached
Copy after login

配置主从复制



参数说明:

-x 设置从哪个IP上进行同步。

-X 指定数据同步的端口。

Memcached默认服务端口是11211,默认同步监听端口是11212。

启动Master

/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.51 -p 11211 -u memcached -c 1024 -x 192.168.11.52 -X 11212 -P /var/run/memcached/memcached-rep.pid
Copy after login

查看监听端口:

netstat -tupln | grep memcached
Copy after login
Copy after login
tcp 0 0 192.168.11.51:11211 0.0.0.0:* LISTEN 18634/memcached
tcp 0 0 192.168.11.51:11212 0.0.0.0:* LISTEN 18634/memcached
udp 0 0 192.168.11.51:11211 0.0.0.0:* 18634/memcached
Copy after login

启动Slave

/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.52 -p 11211 -u memcached -c 1024 -x 192.168.11.51 -X 11212 -P /var/run/memcached/memcached-rep.pid
Copy after login

说明:-x 192.168.11.51用于同步的Master的IP地址。

查看监听端口:

netstat -tupln | grep memcached
Copy after login
Copy after login
tcp 0 0 192.168.11.52:11211 0.0.0.0:* LISTEN 24262/memcached
udp 0 0 192.168.11.52:11211 0.0.0.0:* 24262/memcached
Copy after login

验证数据同步



在Master创建数据:

[root@test01 bin]# telnet 192.168.11.51 11211
Copy after login
Copy after login
Trying 192.168.11.51...
Connected to 192.168.11.51.
Escape character is '^]'.
get key1
END
set key1 0 0 2
aa
STORED
quit
Connection closed by foreign host.
Copy after login

在Slave获取数据:

[root@test02 bin]# telnet 192.168.11.52 11211
Copy after login
Copy after login
Trying 192.168.11.52...
Connected to 192.168.11.52.
Escape character is '^]'.
get key1
END
get key1
VALUE key1 0 2
aa
END
quit
Connection closed by foreign host.
Copy after login

在Slave创建数据:

[root@test02 bin]# telnet 192.168.11.52 11211
Copy after login
Copy after login
Trying 192.168.11.52...
Connected to 192.168.11.52.
Escape character is '^]'.
get key2
END
set key2 0 0 3
b
STORED
get key2
VALUE key2 0 3
b
END
quit
Connection closed by foreign host.
Copy after login

在Master获取数据:

[root@test01 bin]# telnet 192.168.11.51 11211
Copy after login
Copy after login
Trying 192.168.11.51...
Connected to 192.168.11.51.
Escape character is '^]'.
get key2
VALUE key2 0 3
b
END
quit
Connection closed by foreign host.
Copy after login

 

Memcached高可用


启动Master和Slave时不要加-l参数指定监听地址,否则keepalived无法监听VIP的地址。

然后配置上keepalived就可以作为高可用了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy lyrics from QQ Music How to copy lyrics How to copy lyrics from QQ Music How to copy lyrics Mar 12, 2024 pm 08:22 PM

We users should be able to understand the diversity of some functions when using this platform. We know that the lyrics of some songs are very well written. Sometimes we even listen to it several times and feel that the meaning is very profound. So if we want to understand the meaning of it, we want to copy it directly and use it as copywriting. However, if we want to use it, we still need to You just need to learn how to copy lyrics. I believe that everyone is familiar with these operations, but it is indeed a bit difficult to operate on a mobile phone. So in order to give you a better understanding, today the editor is here to help you. A good explanation of some of the above operating experiences. If you also like it, come and take a look with the editor. Don’t miss it.​

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

How to implement exact division operation in Golang How to implement exact division operation in Golang Feb 20, 2024 pm 10:51 PM

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

PS copy layer shortcut key PS copy layer shortcut key Feb 23, 2024 pm 02:34 PM

In the PS copy layer shortcut keys, we can know that if you want to copy a layer when using PS, you can use the shortcut key [Ctrl+J] for quick copying. This introduction to the shortcut keys for copying layers can tell you the specific operation method. The following is the detailed content, so take a look. PS copy layer shortcut key answer: [Ctrl+J] Specific method: 1. Open the image in PS and select the layer that needs to be copied. 2. Press [Ctrl+J] on the keyboard at the same time to complete the copy of the layer. Other copying methods: 1. After opening the image, press and hold the layer and move the [New Layer] icon downwards. 2. After moving to the icon, let go. 3. The layer copy is completed.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

See all articles