自己动手实现Multi-Master Replication
直到今天为止,MySQL依然只支持一个Slave从一个Master复制数据,虽然也可以做到一主多备(M->S),双主复制(M
通过研究源码,可以发现,MySQL管理每个复制通道,都是通过一个Master_info类(sql/rpl_mi.h中定义),start_slave/change_master/stop_slave/show_slave/end_slave这些函数都需要传入一个Master_info指针,这就给我们改造多Master提供了很大的便利,基本只需要为每个复制通道传入相应的Master_info即可。
除了找到函数入口,还需要让语法支持多主,否则CHANGE MASTER TO语句并不能支持多主。我修改了sql_yacc.yy,支持如下语法:
CHANGE MASTER ‘通道标识’ TO,START SLAVE ‘通道标识’,STOP SLAVE ‘通道标识’,SHOW SLAVE ‘通道标识’ STATUS。
这样就可以支持多Master的语法了。
另一个问题是怎么保存多个通道的信息,默认单通道的情况下,用master.info存Master的信息,用relay-log.info存复制应用的情况。所以存储文件的名称也要修改,我的方式是,master.info和relay-log.info在末尾加上通道标识后缀,例如名为”plx”的通道,会存成master.info.plx和relay-log.info.plx。Relay Log因为有序列,所以增加”-通道标识”在序列前。
还有一个问题就是,操作命令都是用通道标识来确定一个通道,那么肯定需要持久化正在用的通道名称,以及建立通道后可以用通道名获取相应的Master_info。于是我新建了一个MASTER_INFO_INDEX类(在sql/rpl_mi.h),里面包含一个通道标识和Master_info指针的对应HASH表,以及持久化需要的IO_CACHE,通过master.info.index这个文件来存已有的通道标识。
命名实例如下:
-rw-rw—- 1 mysql mysql 10 Feb 13 20:40 master.info.index -rw-rw—- 1 mysql mysql 76 Feb 14 17:27 master.info.plx1 -rw-rw—- 1 mysql mysql 71 Feb 14 17:27 master.info.plx2 -rw-rw—- 1 mysql mysql 90 Feb 14 17:25 relay-log.info.plx1 -rw-rw—- 1 mysql mysql 90 Feb 14 17:27 relay-log.info.plx2 -rw-rw—- 1 mysql mysql 160 Feb 14 10:16 mysql-relay-bin-plx1.000011 -rw-rw—- 1 mysql mysql 83765425 Feb 14 17:27 mysql-relay-bin-plx1.000012 -rw-rw—- 1 mysql mysql 106 Feb 14 10:16 mysql-relay-bin-plx1.index -rw-rw—- 1 mysql mysql 160 Feb 14 10:16 mysql-relay-bin-plx2.000014 -rw-rw—- 1 mysql mysql 83455792 Feb 14 17:27 mysql-relay-bin-plx2.000015 -rw-rw—- 1 mysql mysql 106 Feb 14 10:16 mysql-relay-bin-plx2.index
有了多Master以后我们可以做什么呢?下面给两个应用场景。
第一个是一备多的备份。因为我们采用的分库策略,使我们一个集群会有很多个实例,每个实例里面有几个Schema,但是肯定不会重复。例如第一个实例是1~3号Schema。第二个实例就是4~6号Schema,所以binlog应用到一起并不会冲突数据。这是我们测试的在线备份方案。
第二个是跨机房的HA。为了容灾或者加速,很多公司都采用在不同机房部署数据库的方式,所以就涉及到数据同步。为了保证每个机房产生的数据不冲突,一般来说我们采用的是auto_increment_increment,auto_increment_offset这两个参数,可以控制步进。例如双MAster,我们会配置主库是奇数序列的ID,备库是偶数序列的ID,这样切换时就算有少量binlog还未应用,也不会导致数据冲突。跨机房以后,例如两个机房都有双Master,两个机房之间数据又需要同步,以前需要借助第三方脚本或者程序,有了多Master,按如下方式搭建,设置步进为4,就可以保证每个机房有双MAster HA,机房之间数据又可以同步。
已知缺陷:
1. 我还没做reset slave ‘通道标识’命令,就是复制通道还不能重置,只能CHANGE MASTER来改,不是做不了,因为暂时我们没这个需求,等稳定了再考虑这个细节。
2. 数据冲突没有检测。这个是无法解决的,我只是简单的调用了启动Slave的函数来启动多个复制线程,binlog取到本地应用,有数据冲突是不能事先检测的,执行到了才会报出来,可以设置skip-slave-error,对全局有效。其他复制相关的也是全局有效。
最新版patch
已经修改了缺陷1,可以reset slave了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





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

Realizing love animation effects through Java code In the field of programming, animation effects are very common and popular. Various animation effects can be achieved through Java code, one of which is the heart animation effect. This article will introduce how to use Java code to achieve this effect and give specific code examples. The key to realizing the heart animation effect is to draw the heart-shaped pattern and achieve the animation effect by changing the position and color of the heart shape. Here is the code for a simple example: importjavax.swing.

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

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

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.

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.

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
