In simple terms MySQL master-slave replication

Overview

Replication MySQL ( MySQL master-slave replication What) is a?

Why replicate what the master-slave principle and its implementation?

MySQL master-slave replication concepts

Refers to the replication master MySQL data can be copied from a master node to a MySQL database server from one or more nodes. MySQL default asynchronous replication, so that node would not have to access the main server from updating update their data, the data can be performed on a remote connection, you can copy all database master database or specific database from the node, or a specific table .

The main purpose of MySQL master-slave replication

l separate read and write

In development work, sometimes meet a sql statement needs to lock tables, result in temporary service can not be used to read, this will affect the existing business, the use of master-slave replication, so that the main library is responsible for writing, reading from the library responsible, so even if the main library scene appeared lock table by reading from the library can guarantee the normal operation of the business.

l backup data in real time, when a node fails the system can be easily failover

l HA HA

l Schema Extensions

With the increase in business traffic system, if it is to deploy a stand-alone database, it will lead to I / O access frequency is too high. With the master-slave replication, increase the number of data storage nodes, the load distribution from the plurality of nodes, reducing single disk I / O access frequency, a single machine to improve I / O performance.

MySQL master-slave form

A master-slave

From a multi-master, to improve the performance of the system to read
img
a master from a master and a plurality of the most common master-slave architecture is simple to implement and effective, not only the HA may be implemented, but also separate read and write, thereby enhancing concurrent clusters ability.

A multi-master (from 5.7 to support the start) from

img
You may be a multi-master from the plurality mysql database backup to the relatively good performance of a storage server.

Dual master replication

Dual-master replication, i.e. replication mutual shots from each of both master master, and a slave of another server. Such changes made by either party, will apply to the other party by copying the database.

Cascading replication
img
cascade copy mode, the slave portion of the master node is not connected to data synchronization, but is connected from the node. If there are too many because the master node from the node, the loss will be part of the performance for replication, then we can make three to five connecting node from the master node, the other nodes connected to a two or three from the node, so that not only you can ease the pressure of the master node, and no negative impact on data consistency.

MySQL master-slave replication principle

Relates to the master copy from MySQL three threads running in a master node (log dump thread), the remaining two (I / O thread, SQL thread) running from the node, as shown below:

img
l The primary binary log dump thread

When connecting the master node from the master node creates a log dump thread for bin-log of content transmission. In a read operation in the bin-log, bin-log on the master node will lock this thread, when the reading is completed, even before the launch to the node, the lock will be released.

l from node I / O threads

When executed from the node start slaveafter the command, the node creates an I / O thread is used to connect the master node, the request to update the master library bin-log. After the update I / O thread receives binlog dump process to the master node, stored in a local relay-log.

l from node SQL thread

SQL thread is responsible for reading the content relay log in, resolve into concrete action and execution, and ultimately ensure the consistency of data from the master.

For each master-slave connection, it requires three processes to complete. When there are multiple nodes master node from the master node to a binary log dump process from each node to build a current connection, and from each node has its own I / O processes, SQL processes. When the two threads from the master node by taking Kura updating and implementation into separate tasks, so that data synchronization task, without compromising the performance of the read operation. For example, if the node is not running from this time I / O process can quickly get updates from the master node, although the process has not been executed SQL. If you stop the service from the node before SQL execution process, at least I / O processes have been pulled from the master node to the latest changes and saved in the local relay log, when the service up again, you can complete synchronization of data.

要实施复制,首先必须打开Master 端的binary log(bin-log)功能,否则无法实现。

因为整个复制过程实际上就是Slave 从Master 端获取该日志然后再在自己身上完全顺序的执行日志中所记录的各种操作。如下图所示:

img
复制的基本过程如下:

从节点上的I/O 进程连接主节点,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容;主节点接收到来自从节点的I/O请求后,通过负责复制的I/O进程根据请求信息读取指定日志指定位置之后的日志信息,返回给从节点。返回信息中除了日志所包含的信息之外,还包括本次返回的信息的bin-log file 的以及bin-log position;从节点的I/O进程接收到内容后,将接收到的日志内容更新到本机的relay log中,并将读取到的binary log文件名和位置保存到master-info 文件中,以便在下一次读取的时候能够清楚的告诉Master“我需要从某个bin-log 的哪个位置开始往后的日志内容,请发给我”;Slave 的 SQL线程检测到relay-log 中新增加了内容后,会将relay-log的内容解析成在祝节点上实际执行过的操作,并在本数据库中执行。

MySQL 主从复制模式

MySQL 主从复制默认是异步的模式。MySQL增删改操作会全部记录在binary log中,当slave节点连接master时,会主动从master处获取最新的bin log文件。并把bin log中的sql relay。

l 异步模式(mysql async-mode)

异步模式如下图所示,这种模式下,主节点不会主动push bin log到从节点,这样有可能导致failover的情况下,也许从节点没有即时地将最新的bin log同步到本地。
img

l 半同步模式(mysql semi-sync)

这种模式下主节点只需要接收到其中一台从节点的返回信息,就会commit;否则需要等待直到超时时间然后切换成异步模式再提交;这样做的目的可以使主从数据库的数据延迟缩小,可以提高数据安全性,确保了事务提交后,binlog至少传输到了一个从节点上,不能保证从节点将此事务更新到db中。性能上会有一定的降低,响应时间会变长。如下图所示:
img

半同步模式不是mysql内置的,从mysql 5.5开始集成,需要master 和slave 安装插件开启半同步模式。

l 全同步模式

全同步模式是指主节点和从节点全部执行了commit并确认才会向客户端返回成功。

binlog记录格式
MySQL 主从复制有三种方式:基于SQL语句的复制(statement-based replication,SBR),基于行的复制(row-based replication,RBR),混合模式复制(mixed-based replication,MBR)。对应的binlog文件的格式也有三种:STATEMENT,ROW,MIXED。

l Statement-base Replication (SBR)就是记录sql语句在bin log中,Mysql 5.1.4 及之前的版本都是使用的这种复制格式。优点是只需要记录会修改数据的sql语句到binlog中,减少了binlog日质量,节约I/O,提高性能。缺点是在某些情况下,会导致主从节点中数据不一致(比如sleep(),now()等)。

l Row-based Relication(RBR)是mysql master将SQL语句分解为基于Row更改的语句并记录在bin log中,也就是只记录哪条数据被修改了,修改成什么样。优点是不会出现某些特定情况下的存储过程、或者函数、或者trigger的调用或者触发无法被正确复制的问题。缺点是会产生大量的日志,尤其是修改table的时候会让日志暴增,同时增加bin log同步时间。也不能通过bin log解析获取执行过的sql语句,只能看到发生的data变更。

l Mixed-format Replication(MBR),MySQL NDB cluster 7.3 和7.4 使用的MBR。是以上两种模式的混合,对于一般的复制使用STATEMENT模式保存到binlog,对于STATEMENT模式无法复制的操作则使用ROW模式来保存,MySQL会根据执行的SQL语句选择日志保存方式。

GTID复制模式
@ 在传统的复制里面,当发生故障,需要主从切换,需要找到binlog和pos点,然后将主节点指向新的主节点,相对来说比较麻烦,也容易出错。在MySQL 5.6里面,不用再找binlog和pos点,我们只需要知道主节点的ip,端口,以及账号密码就行,因为复制是自动的,MySQL会通过内部机制GTID自动找点同步。

@ 多线程复制(基于库),在MySQL 5.6以前的版本,slave的复制是单线程的。一个事件一个事件的读取应用。而master是并发写入的,所以延时是避免不了的。唯一有效的方法是把多个库放在多台slave,这样又有点浪费服务器。在MySQL 5.6里面,我们可以把多个表放在多个库,这样就可以使用多线程复制。

Based on the principle GTID replication for the
master node update data will produce GTID before the transaction, recorded in the log with binlog. From bin log node I / O thread change, and writes to the local relay log in. SQL thread gets GTID from the relay log in, and then compare whether local binlog record (so MySQL slaves must be open binary log). If you have recorded, with the GTID transaction has been executed, the node will be ignored. If there is no record of the transaction will be executed GTID from the relay log from the node, and recorded bin log. In the parsing process will determine whether there is a primary key, if not all the scanning by the secondary index, if there is to use.

to sum up

Mysql Mysql master-slave replication is highly available, high-performance foundation, with this foundation, mysql deployment will become simple, flexible and diverse, which can respond flexibly adjusted according to different business scenarios.

Published 107 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/belongtocode/article/details/102793447