search
HomeDatabaseMysql Tutorial mariadb-10GTID复制及多源复制

---本文大纲一、什么是GTID二、应用场景三、多线程复制说明四、实现过程五、多源复制原理六、实现过程----------------------------------一、什么是GITD自MySQL

---本文大纲

一、什么是GTID

二、应用场景

三、多线程复制说明

四、实现过程

五、多源复制原理

六、实现过程

----------------------------------

一、什么是GITD

自MySQL 5.6引入的GTID(Global Transaction IDs)使得其复制功能的配置、监控及管理变得更加易于实现,且更加健壮。官方文档在这篇文档里,我们可以知道全局事务 ID 的官方定义是:GTID = source_id:transaction_id

gtid是一个 unique 唯一的表示符,他是由服务器的uuid 全局唯一标识,是由128位的随机符组成,mysql-5.6是依靠server-id和uuid 来标识复制架构中的每一个主机,因为是128位的随机字符串在全局都不会重复,server-id 和uuid以及每一个mysql事物的事物序号组成了唯一的gtid ,自从引进mysql-5.6之后每一个二进制日志当中在每一个事物的首部都会写上gtid 标记,因此gtid使得追踪和比较复制事物变得非常简单而且能够实现从崩溃中快速恢复。尤其是innodb 引擎要想实现高可用功能必须要借助于gtid来实现。

二、应用场景

wKioL1NSKh-RfZsnAAHRN5xIcEc108.bmp

当使用mariadb实现高可用时,GTID是显得尤为重要,例如,在一个高可用环境中,一主多从的模式下,当主库宕机后(写延迟),集群资源管理器可以在节点列表中的多个从库中任选一个提升为主库,而不会影响到业务本身,而其它的从库将会以新提升起来为主库为当前集群中的主库,以后将从这个库上复制同步;虽然这样做完成了“瞬间”业务切换,但可能在主库未宕机之前,多个从库的复制同步落后于主库,这样一来,当主库宕机后,集群资源管理器正好切换到一个与自己事务提交可能不一致的从库(B)上,当有应用读取之前己在宕机的主库中提交的事务时,发现现在的主库没有,这样就会出现事务不一致,而GTID就能很好的解决这个问题。首先当一个从库被提升为一个主库时,那么之前指向原来主库的从库将全部指向新提升的主库,来进行之后的复制,而后,刚提升的主库B会整合所有从库中己完成的的事务,来添补自己缺少的部分,而B从那里知道,缺少的是那一个事务,有那么多的事务,这个事务是那一个呢,这就是通过GTID来标识的,因为GTID标识了来源服务器的标识与第多少个事务(Source_id:transaction_id),每一个改变是事件都会与GTID相关连起来记录于binlog日志中以供其它从库同步,从而做到全局唯一的标识。

注:

在多级复制中GTID是不会改变的。

在GTID中,如果主服务器中有多个数据库,要实现多线程复制是靠I/O复制到从库的中继日志中由多个SQL thread来进行应用于本地的。

三、多线程复制说明

MySQL 5.6之前的版本,同步复制是单线程的,队列的,只能一个一个执行,在5.6里,可以做到多个库之间的多线程复制,例如数据库里,存放着用户表,商品表,价格表,订单表,那么将每个业务表单独放在一个库里,这时就可以做到多线程复制,但一个库里的表,多线程复制是无效的。

注:

每个数据库仅能使用一个线程,复制涉及到多个数据库时多线程复制才有意义;

同一个库的事务复制,就必须按先后顺序复制。

在复制模型中,同一个线程(I/O线程)可以对多个数据库提供服务。

四、实现过程

注:

此处仅是实现GTID复制。

1、资源分配

服务类型版本类型ip地址

OS
Centos6.5x86_64无

Mariadb Master10.0.10-MariaDB-log Source distribution192.168.1.122/24

Mariadb Slave10.0.10-MariaDB-log Source distribution192.168.1.210/24

2、Mariadb Master配置清单

⑴、编辑配置文件/etc/my.cnf

[client] port = 3306 socket = /tmp/maria.sock [mysqld] port = 3306 socket = /tmp/maria.sock skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M thread_concurrency = 4 datadir=/mydata/data log-bin=mysql-bin binlog_format=row server-id=10 log-slave-update =True master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-threads=2 master-verify-checksum=CRC32 slave-sql-verify-checksum=1 binlog-rows-query-log-events=1 report-port=3306 report-host=master.mysql.com [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 128M sort_buffer_size = 128M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout

注解:

binlog-format

二进制日志的格式,有row、statement和mixed几种类型;

当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;

log-slave-updates、report-port和report-host

用于启动GTID及满足附属的其它需求;

master-info-repository和relay-log-info-repository

启用此两项,可用于实现在崩溃时保证二进制及从服务器安全的功能;

sync-master-info

启用之可确保无信息丢失;

slave-paralles-threads

设定从服务器的SQL线程数;0表示关闭多线程复制功能;

binlog-checksum、master-verify-checksum和slave-sql-verify-checksum

启用复制有关的所有校验功能;

binlog-rows-query-log-events

启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度;

log-bin

启用二进制日志,这是保证复制功能的基本前提;

server-id

同一个复制拓扑中的所有服务器的id号必须惟一

log_slave_updates

记录从服务器的更新

⑵、授权复制节点及用户

Statement
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
How do you create and manage user accounts in MySQL?How do you create and manage user accounts in MySQL?Apr 22, 2025 pm 06:05 PM

The steps to create and manage user accounts in MySQL are as follows: 1. Create a user: Use CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password'; 2. Assign permissions: Use GRANTSELECT, INSERT, UPDATEONmydatabase.TO'newuser'@'localhost'; 3. Fix permission error: Use REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost'; then reassign permissions; 4. Optimization permissions: Use SHOWGRA

How does MySQL differ from Oracle?How does MySQL differ from Oracle?Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What are the disadvantages of using MySQL compared to other relational databases?What are the disadvantages of using MySQL compared to other relational databases?Apr 22, 2025 pm 05:49 PM

The disadvantages of MySQL compared to other relational databases include: 1. Performance issues: You may encounter bottlenecks when processing large-scale data, and PostgreSQL performs better in complex queries and big data processing. 2. Scalability: The horizontal scaling ability is not as good as Google Spanner and Amazon Aurora. 3. Functional limitations: Not as good as PostgreSQL and Oracle in advanced functions, some functions require more custom code and maintenance.

How do you perform a JOIN operation in MySQL?How do you perform a JOIN operation in MySQL?Apr 22, 2025 pm 05:41 PM

MySQL supports four JOIN types: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN is used to match rows in two tables and return results that meet the criteria. 2.LEFTJOIN returns all rows in the left table, even if the right table does not match. 3. RIGHTJOIN is opposite to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet the conditions.

How does MySQL's performance compare to other RDBMS under high load?How does MySQL's performance compare to other RDBMS under high load?Apr 22, 2025 pm 05:37 PM

MySQL's performance under high load has its advantages and disadvantages compared with other RDBMSs. 1) MySQL performs well under high loads through the InnoDB engine and optimization strategies such as indexing, query cache and partition tables. 2) PostgreSQL provides efficient concurrent read and write through the MVCC mechanism, while Oracle and Microsoft SQLServer improve performance through their respective optimization strategies. With reasonable configuration and optimization, MySQL can perform well in high load environments.

Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

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.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

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.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

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.

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools