直到今天为止,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了。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software