To maintain up-to-date copies of the shared data set, secondary members of a replica set sync or replicate data from a source member. 为了维护共享数据集的最新副本,副本集的辅助成员会同步或复制源成员的数据。MongoDB uses two forms of data synchronization: initial sync to populate new members with the full data set, and replication to apply ongoing changes to the entire data set.MongoDB使用两种形式的数据同步:初始同步用完整的数据集填充新成员,复制将正在进行的更改应用于整个数据集。
Initial Sync初始同步
Initial sync copies all the data from the source member of the replica set to a destination member. See Initial Sync Source Selection for more information on source member selection criteria.初始同步将所有数据从副本集的源成员复制到目标成员。有关源成员选择条件的更多信息,请参阅初始同步源选择。
The local database stores the oplog data that the initial sync process uses. Ensure the destination member has enough space in the local database to store the oplog data for the initial sync process to complete.local数据inventory储初始同步过程使用的oplog数据。确保目标成员在本地数据库中有足够的空间来存储oplog数据,以便完成初始同步过程。
Note
During the initial sync, MongoDB truncates the oplog on the destination member. This oplog truncation can impact processes, such as change streams, that depend on oplog data.在初始同步期间,MongoDB截断目标成员上的oplog。这种oplog截断会影响依赖oplog数据的流程,如变更流。
You can specify the preferred initial sync source using the 您可以使用initialSyncSourceReadPreference parameter. initialSyncSourceReadPreference参数指定首选的初始同步源。This parameter can only be specified when starting the 此参数只能在启动mongod.mongod时指定。
Starting in MongoDB 5.2, initial syncs can be logical or file copy based.从MongoDB 5.2开始,初始同步可以是基于逻辑或文件副本的。
Logical Initial Sync Process逻辑初始同步过程
When you perform a logical initial sync, MongoDB:当您执行逻辑初始同步时,MongoDB:
Clones all databases except the local database. To clone, the克隆除mongodscans every collection in each source member database and inserts all data into its own copies of these collections.local数据库之外的所有数据库。为了克隆,mongod扫描每个源成员数据库中的每个集合,并将所有数据插入到这些集合的自己的副本中。Builds all collection indexes as the documents are copied for each collection.在为每个集合复制文档时构建所有集合索引。Pulls newly added oplog records during the data copy. Ensure that the destination member has enough disk space in the在数据复制过程中提取新添加的oplog记录。确保目标成员在localdatabase to temporarily store these oplog records for the duration of this data copy stage.local数据库中有足够的磁盘空间,以便在此数据复制阶段期间临时存储这些oplog记录。Applies all changes to the data set. Using the oplog from the source member, the将所有更改应用于数据集。使用源成员的oplog,mongodupdates its data set to reflect the current state of the replica set.mongod更新其数据集以反映副本集的当前状态。
When the initial sync finishes, the member transitions from 当初始同步完成时,成员将从STARTUP2 to SECONDARY.STARTUP2转换为SECONDARY。
To perform an initial sync, see Resync a Member of a Self-Managed Replica Set.要执行初始同步,请参阅重新同步自我管理副本集的成员。
File Copy Based Initial Sync基于文件复制的初始同步
Available in MongoDB Enterprise only.仅在MongoDB Enterprise中可用。
File copy based initial sync runs the initial sync process by copying and moving files on the file system. This sync method can be faster than logical initial sync.基于文件复制的初始同步通过复制和移动文件系统上的文件来运行初始同步过程。此同步方法可能比逻辑初始同步更快。
Important
File copy based initial sync may cause inaccurate counts基于文件副本的初始同步可能会导致计数不准确
After file copy based initial sync completes, if you run the 在基于文件复制的初始同步完成后,如果在没有查询谓词的情况下运行count() method without a query predicate, the count of documents returned may be inaccurate.count()方法,则返回的文档计数可能不准确。
A 没有查询谓词的count method without a query predicate looks like this: db.<collection>.count().count方法如下:db.<collection>.count()。
To learn more, see Inaccurate Counts Without Query Predicate.要了解更多信息,请参阅没有查询谓词的不准确计数。
Enable File Copy Based Initial Sync启用基于文件复制的初始同步
To enable file copy based initial sync, set the 要启用基于文件复制的初始同步,请根据初始同步的目标成员将initialSyncMethod parameter to fileCopyBased on the destination member for the initial sync. This parameter can only be set at startup.initialSyncMethod参数设置为fileCopyBased。此参数只能在启动时设置。
Behavior行为
File copy based initial sync replaces the 同步时,基于文件副本的初始同步将目标成员的local database of the destination member with the local database of the source member when syncing.local数据库替换为源成员的local数据。
Limitations局限性
During a file copy based initial sync:在基于文件副本的初始同步期间:You cannot run backups on either the source member or the destination member.您无法在源成员或目标成员上运行备份。You cannot write to the您无法写入目标成员上的localdatabase on the destination member.local数据库。
You can only run an initial sync from one source member at a time.一次只能从一个源成员运行初始同步。When using the encrypted storage engine, MongoDB uses the source member key to encrypt the destination.使用加密存储引擎时,MongoDB使用源成员键对目标进行加密。
Initial Sync on NVMe ClustersNVMe群集上的初始同步
You must perform an initial sync on clusters that use the local Non-Volatile Memory Express (NVMe) SSD storage option, including if you're using Atlas auto-scaling. 您必须在使用本地非易失性存储器快速(NVMe)SSD存储选项的群集上执行初始同步,包括使用Atlas自动扩展的群集。Atlas NVMe clusters auto-scale to the next higher tier when 90% of the storage space is full. An initial sync takes longer to complete compared to subsequent syncs, and reduces the performance of the primary from which the data is read.当90%的存储空间已满时,Atlas NVMe集群会自动扩展到下一个更高的层。与后续同步相比,初始同步需要更长的时间才能完成,并降低了从primary中读取数据的性能。
Fault Tolerance容错
If a destination member performing initial sync encounters a persistent network error during the sync process, the destination member restarts the initial sync process from the beginning.如果执行初始同步的目标成员在同步过程中遇到持续的网络错误,目标成员将从头开始重新启动初始同步过程。
A destination member performing initial sync can attempt to resume the sync process if interrupted by a temporary network error, collection drop, or collection rename.如果因临时网络错误、集合删除或集合重命名而中断,执行初始同步的目标成员可以尝试恢复同步过程。
By default, the destination member tries to resume initial sync for 24 hours. 默认情况下,目标成员会尝试在24小时内恢复初始同步。You can use the 您可以使用initialSyncTransientErrorRetryPeriodSeconds server parameter to control the amount of time the destination member attempts to resume initial sync. initialSyncTransientErrorRetryPeriodSeconds服务器参数来控制目标成员尝试恢复初始同步的时间量。If the destination member cannot successfully resume the initial sync process during the configured time period, it selects a new healthy source member from the replica set and restarts the initial synchronization process from the beginning.如果目标成员在配置的时间段内无法成功恢复初始同步过程,它将从副本集中选择一个新的健康源成员,并从头开始重新启动初始同步过程。
The secondary attempts to restart the initial sync up to 次要尝试重新启动初始同步最多10 times before returning a fatal error.10次,然后返回致命错误。
Initial Sync Source Selection初始同步源选择
Initial sync source selection depends on the value of the 初始同步源选择取决于mongod startup parameter initialSyncSourceReadPreference:mongod启动参数initialSyncSourceReadPreference的值:
For对于初始initialSyncSourceReadPreferenceset toprimary(default ifchainingAllowedis disabled), select the primary as the source member.initialSyncSourceReadPreference置为主(如果禁用chainingAllowed,则默认设置),请选择primary作为源成员。If the primary is unavailable or unreachable, log an error and periodically check for primary availability.如果主服务器不可用或无法访问,请记录错误并定期检查主服务器的可用性。For对于initialSyncSourceReadPreferenceset toprimaryPreferred(default for voting replica set members), attempt to select the primary as the source member. If the primary is unavailable or unreachable, perform sync source member selection from the remaining replica set members.initialSyncSourceReadPreference设置为primaryPreferred(投票副本集成员的默认设置),尝试选择主成员作为源成员。如果主副本不可用或无法访问,请从其余副本集成员中执行同步源成员选择。For all other supported read modes, perform sync source member selection from the destination members.对于所有其他支持的读取模式,请从目标成员中执行同步源成员选择。
Members performing initial source member selection make two passes through the list of all replica set members:执行初始源成员选择的成员对所有副本集成员的列表进行两次遍历:
First Pass第一轮
The member applies the following criteria to each replica set member when making the first pass for selecting a initial source member:在首次选择初始源成员时,该成员对每个副本集成员应用以下标准:
The source member must be in the源成员必须处于PRIMARYorSECONDARYreplication state.PRIMARY或SECONDARY复制状态。The source member must be online and reachable.源成员必须联机且可访问。If如果initialSyncSourceReadPreferenceissecondaryorsecondaryPreferred, the source member must be a secondary.initialSyncSourceReadPreference是secondary或secondaryPreferred,则源成员必须是secondary。The source member must be源成员必须可见。visible.The source member must be within源成员必须在主目录上最新oplog条目的30seconds of the newest oplog entry on the primary.30秒内。If the member如果成员构建索引,则源成员必须构建索引。builds indexes, the source member must build indexes.If the member如果成员在副本集选举中投票,则源成员也必须投票。votesin replica set elections, the source member must also vote.If the member is not a如果该成员不是延迟成员,则源成员不得被延迟。delayed member, the source member must not be delayed.If the member is a如果该成员是延迟成员,则源成员必须具有较短的配置延迟。delayed member, the source member must have a shorter configured delay.The source member must be faster than the current best sync source.源成员必须比当前最佳同步源快。
If no candidate source member remains after the first pass, the member performs a second pass with relaxed criteria. See Sync Source Selection (Second Pass).如果第一次通过后没有候选源成员,则该成员将使用放宽的标准执行第二次通过。请参阅同步源选择(第二次)。
Second Pass第二轮
The member applies the following criteria to each replica set member when making the second pass for selecting a initial source member:在第二次选择初始源成员时,该成员对每个副本集成员应用以下标准:
The source member must be in the源成员必须处于PRIMARYorSECONDARYreplication state.PRIMARY或SECONDARY复制状态。The source member must be online and reachable.源成员必须联机且可访问。If如果initialSyncSourceReadPreferenceissecondary, the source member must be a secondary.initialSyncSourceReadPreference是secondary,则源成员必须是secondary。If the member如果成员构建索引,则源成员必须构建索引。builds indexes, the source member must build indexes.The source member must be faster than the current best sync source.源成员必须比当前最佳同步源快。
If the destination member cannot select a source member after two passes, it logs an error and waits 如果目标成员在两次通过后无法选择源成员,则会记录一个错误并等待1 second before restarting the selection process. 1秒,然后再重新启动选择过程。The secondary 辅助mongod can restart the initial sync source selection process up to 10 times before exiting with an error.mongod最多可以重新启动初始同步源选择过程10次,然后退出并出现错误。
Oplog Window操作日志窗口
The oplog window must be long enough so that a destination member can fetch any new oplog entries that occur between the start and end of the Logical Initial Sync Process. oplog窗口必须足够长,以便目标成员可以获取逻辑初始同步过程开始和结束之间出现的任何新oplog条目。If the window isn't long enough, there is a risk that some entries may fall off the 如果窗口不够长,那么在目标成员应用之前,一些条目可能会从oplog before the destination member can apply them.oplog中掉下来。
It is recommended that you size the 建议您调整oplog for additional time to fetch any new oplog entries. This allows for changes that may occur during initial syncs.oplog的大小,以便有更多时间获取任何新的oplog条目。这允许在初始同步期间发生更改。
For more information, see Oplog Size.有关更多信息,请参阅Oplog大小。
Replication复制
Destination members replicate data continuously after the initial sync. Destination members copy the oplog from the source member and apply these operations in an asynchronous process.目标成员在初始同步后连续复制数据。目标成员从源成员复制oplog,并在异步进程中应用这些操作。
Destination members automatically change their source member as needed based on changes in the ping time and state of other members' replication. See Replication Sync Source Selection for more information on source member selection criteria.目标成员根据ping时间和其他成员复制状态的变化,根据需要自动更改其源成员。有关源成员选择标准的详细信息,请参阅复制同步源选择。
Streaming Replication流式复制
Source members send a continuous stream of oplog entries to their destination members. Streaming replication mitigates replication lag in high-load and high-latency networks. It also:源成员向其目标成员发送源源不断的oplog条目。流式复制减轻了高负载和高延迟网络中的复制延迟。它还:
Reduces staleness for reads from secondaries.减少从中学阅读的过时性。Reduces risk of losing write operations with w: 1 due to primary failover.降低了由于主故障转移而丢失w:1写入操作的风险。Reduces latency on write operations with通过w: "majority"and w: >1 (that is, any write concern that requires waiting for replication).w: "majority"和w: >1(即需要等待复制的任何写入关注)减少写入操作的延迟。
Use the 使用oplogFetcherUsesExhaust startup parameter to disable streaming replication and using the older replication behavior. oplogFetcherUsesExhaust启动参数禁用流式复制并使用旧的复制行为。Set the 仅当源成员存在任何资源限制,或者您希望限制MongoDB对网络带宽的使用以进行复制时,才将oplogFetcherUsesExhaust parameter to false only if there are any resource constraints on the source member or if you wish to limit MongoDB's usage of network bandwidth for replication.oplogFetcherUsesExhaust参数设置为false。
Multithreaded Replication多线程复制
MongoDB applies write operations in batches using multiple threads to improve concurrency. MongoDB groups batches by document ID (WiredTiger) and simultaneously applies each group of operations using a different thread. MongoDB always applies write operations to a given document in their original write order.MongoDB使用多线程批量应用写操作以提高并发性。MongoDB按文档ID(WiredTiger)对批处理进行分组,并使用不同的线程同时应用每组操作。MongoDB总是按照原始写入顺序对给定文档应用写入操作。
Read operations that target secondaries and are configured with a read concern level of 如果读取发生在正在应用复制批处理的辅助服务器上,则针对辅助服务器并配置为从数据的WiredTiger快照读取"local" or "majority" read from a WiredTiger snapshot of the data if the read takes place on a secondary where replication batches are being applied."local"或"majority"读取关注级别的读取操作。
Reading from a snapshot guarantees a consistent view of the data, and allows the read to occur simultaneously with the ongoing replication without the need for a lock. As a result, secondary reads requiring these read concern levels no longer need to wait for replication batches to be applied, and can be handled as they are received.从快照读取保证了数据视图的一致性,并允许读取与正在进行的复制同时进行,而不需要锁。因此,需要这些读取关注级别的辅助读取不再需要等待应用复制批,并且可以在收到复制批时进行处理。
Flow Control流量控制
Administrators can limit the rate at which the primary applies its writes with the goal of keeping the 管理员可以限制主应用其写入的速率,目的是将大多数提交的延迟保持在可配置的最大值majority committed lag under a configurable maximum value flowControlTargetLagSeconds.flowControlTargetLagSeconds以下。
By default, flow control is 默认情况下,启用流控制。enabled.
For more information, see Flow Control.有关详细信息,请参阅流量控制。
Replication Sync Source Selection复制同步源选择
Replication source member selection depends on the replica set 复制源成员的选择取决于副本集chaining setting:chaining设置:
With chaining enabled (default), perform source member selection from the destination members.启用链接(默认)后,从目标成员中执行源成员选择。With chaining disabled, select the primary as the source member. If the primary is unavailable or unreachable, log an error and periodically check for primary availability.禁用链接后,选择primary作为源成员。如果primary不可用或无法访问,请记录错误并定期检查主服务器的可用性。
Members performing replication source member selection make two passes through the list of all replica set members:执行复制源成员选择的成员对所有副本集成员的列表进行两次遍历:
First Pass第一轮
The member applies the following criteria to each replica set member when making the first pass for selecting a source member:在首次选择源成员时,该成员对每个副本集成员应用以下标准:
The source member must be in the源成员必须处于PRIMARYorSECONDARYreplication state.PRIMARY或SECONDARY复制状态。The source member must be online and reachable.源成员必须联机且可访问。The source member must have newer oplog entries than the member. That is, the source member must be ahead of the member.源成员必须具有比该成员更新的oplog条目。也就是说,源成员必须在成员之前。The source member must be源成员必须可见。visible.The source member must be within源成员必须在主目录上最新oplog条目的30seconds of the newest oplog entry on the primary.30秒内。If the member如果成员构建索引,则源成员必须构建索引。builds indexes, the source member must build indexes.If the member如果成员在副本集选举中投票,则源成员也必须投票。votesin replica set elections, the source member must also vote.If the member is not a如果该成员不是延迟成员,则源成员不得被延迟。delayed member, the source member must not be delayed.If the member is a如果该成员是延迟成员,则源成员必须具有较短的配置延迟。delayed member, the source member must have a shorter configured delay.The source member must be faster than the current best sync source.源成员必须比当前最佳同步源快。
If no candidate source members remain after the first pass, the member performs a second pass with relaxed criteria. See the Sync Source Selection (Second Pass).如果第一次通过后没有候选源成员,则该成员将使用放宽的标准执行第二次通过。请参阅同步源选择(第二次)。
Second Pass第二轮
The member applies the following criteria to each replica set member when making the second pass for selecting a source member:在第二次选择源成员时,该成员对每个副本集成员应用以下标准:
The source member must be in the源成员必须处于PRIMARYorSECONDARYreplication state.PRIMARY或SECONDARY复制状态。The source member must be online and reachable.源成员必须联机且可访问。If the member如果成员构建索引,则源成员必须构建索引。builds indexes, the source member must build indexes.The source member must be faster than the current best sync source.源成员必须比当前最佳同步源快。
If the member cannot select a sync source after two passes, it logs an error and waits 如果成员在两次通过后无法选择同步源,则会记录一个错误并等待1 second before restarting the selection process.1秒,然后再重新启动选择过程。
The number of times a source member can be changed per hour is configurable by setting the 通过设置maxNumSyncSourceChangesPerHour parameter.maxNumSyncSourceChangesPerHour参数,可以配置源成员每小时可以更改的次数。
Note
The startup parameter 选择初始同步源成员时,启动参数initialSyncSourceReadPreference takes precedence over the replica set's settings.chainingAllowed setting when selecting an initial sync source member. initialSyncSourceReadPreference优先于副本集的settings.chainingAllowed设置。After a replica set member successfully performs initial sync, it defers to the value of 副本集成员成功执行初始同步后,在选择源成员时,它将遵循chainingAllowed when selecting a source member.chainingAllowed的值。
See Initial Sync Source Selection for more information on initial sync source selection.有关初始同步源选择的更多信息,请参阅初始同步源的选择。