简体   繁体   English

数据库中的主从复制

[英]Master Slave Replication in Databases

As per How To Set Up Replication in MySQL ,根据如何在 MySQL 中设置复制

Once the replica instance has been initialized, it creates two threaded processes.一旦副本实例被初始化,它就会创建两个线程进程。 The first, called the IO thread, connects to the source MySQL instance and reads the binary log events line by line, and then copies them over to a local file on the replica's server called the relay log.第一个称为 IO 线程,连接到源 MySQL 实例并逐行读取二进制日志事件,然后将它们复制到副本服务器上称为中继日志的本地文件。 The second thread, called the SQL thread, reads events from the relay log and then applies them to the replica instance as fast as possible.第二个线程,称为 SQL 线程,从中继日志中读取事件,然后尽快将它们应用到副本实例。

Isn't it contradictory to the theory of master-slave database replication in which the master copies data to the slaves?是不是和master复制数据到slave的主从数据库复制理论相矛盾呢?

"master-slave database replication in which the master copies data to the slaves" - it's just a concept - data from a leader is copied to followers. “主从数据库复制,其中主将数据复制到从属” - 这只是一个概念 - 来自领导者的数据被复制到追随者。 There are many options how this could be done.如何做到这一点有很多选择。 Some of those are the write ahead log replication, blocks replication, rows replication.其中一些是预写日志复制、块复制、行复制。

Another interesting approach is to use a replication system completely separate from the storage.另一种有趣的方法是使用完全独立于存储的复制系统。 An example for this would be Bucardo - replication system for PostgreSQL.一个例子是 Bucardo - PostgreSQL 的复制系统。 In that case nighter leader or follower actually do work.在那种情况下,更晚的领导者或追随者实际上是在工作。

Reliability.可靠性。 (A mini-history of MySQL's efforts.) (MySQL 努力的小历史。)

When a write occurs on the Primary, N+1 extra actions occur:当主节点上发生写入时,会发生 N+1 个额外操作:

  • One write to the binlog -- this is to allow for any Replicas that happen to be offline (for any reason);一次写入二进制日志——这是为了允许任何碰巧离线的副本(出于任何原因); they can come back later and request data from this file.他们可以稍后回来并从此文件中请求数据。 (Also see sync_binlog ) (另见sync_binlog
  • N network writes, one per Replica. N 次网络写入,每个 Replica 一次。 These are to get the data to the Replicas ASAP.这些是为了尽快将数据发送到副本。

Normally, if you want more than a few Replicas, you can "fan out" through several levels, thereby allowing for an unlimited number of Replicas.通常,如果您想要多个副本,您可以通过多个级别“扇出”,从而允许无限数量的副本。 (10 per level would give you 1000 Replicas in 3 layers.) (每级 10 个将在 3 层中为您提供 1000 个副本。)

The product called Orchestrator carries this to an extra level -- the binlog is replicated to an extra server and the network traffic occurs from there.名为 Orchestrator 的产品将这一点提升到了一个额外的水平——二进制日志被复制到一个额外的服务器上,网络流量从那里发生。 This offloads the Primary.这会卸载主节点。 (Booking.com uses it to handle literally hundreds of replicas.) (Booking.com 使用它来处理数以百计的复制品。)

On the Replica's side the two threads were added 20 years ago because of the following scenario:在 Replica 方面,由于以下情况,20 年前添加了两个线程:

  • The Replica is busy doing one query at a time.副本正忙于一次执行一个查询。
  • It gets busy with some long query (say an ALTER)它忙于一些长查询(比如 ALTER)
  • Lots of activity backs up on the Primary大量活动在主节点上备份
  • The Primary dies.初级会死。

Now the Replica finishes the Alter, but does not have anything else to work on, so it is very "behind" and will take extra time to "catch up" once the Primary comes back online.现在 Replica 完成了 Alter,但没有其他工作要做,所以它非常“落后”,一旦 Primary 重新上线,将需要额外的时间来“赶上”。

Hence, the 2-thread Replica "helps" keep things in sync, but it is still not fully synchronous.因此,2 线程副本“帮助”保持同步,但它仍然不是完全同步的。

Later there was "semi-synchronous" replication and multiple SQL threads in the Replica (still a single I/O thread).后来有“半同步”复制和副本中的多个 SQL 线程(仍然是单个 I/O 线程)。

Finally, InnoDB Cluster and Galera became available to provide [effectively] synchronous replication.最后,InnoDB Cluster 和 Galera 可用于提供 [有效] 同步复制。 But they come with other costs.但它们伴随着其他成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM