Resync a Member of a Replica Set重新同步副本集的成员

On this page本页内容

A replica set member becomes "stale" when its replication process falls so far behind that the primary overwrites oplog entries the member has not yet replicated. 当副本集成员的复制过程落后太多,以至于primary覆盖了该成员尚未复制的oplog条目时,副本集成员就会变得“过时”。The member cannot catch up and becomes "stale." 成员无法赶上并变得“过时”。When this occurs, you must completely resynchronize the member by removing its data and performing an initial sync.发生这种情况时,必须通过删除其数据并执行初始同步来完全重新同步该成员。

This tutorial addresses both resyncing a stale member and creating a new member using seed data from another member, both of which can be used to restore a replica set member. 本教程介绍了重新同步过时成员和使用来自另一个成员的种子数据创建新成员,这两个都可以用于恢复副本集成员。When syncing a member, choose a time when the system has the bandwidth to move a large amount of data. 同步成员时,请选择系统有带宽移动大量数据的时间。Schedule the synchronization during a time of low usage or during a maintenance window.在低使用率或维护时段安排同步。

MongoDB provides two options for performing an initial sync:MongoDB为执行初始同步提供了两个选项:

Procedures过程

Note注意

To prevent changing the write quorum, never rotate more than one replica set member at a time.要防止更改写入仲裁,请不要一次旋转多个副本集成员。

Automatically Sync a Member自动同步成员

Warning警告

During initial sync, mongod removes the contents of the dbPath directory.在初始同步期间,mongod删除dbPath目录的内容。

This procedure relies on MongoDB's regular process for Replica Set Syncing. 此过程依赖于MongoDB的副本集同步常规过程。This stores the current data on the member. 这将存储成员的当前数据。For an overview of MongoDB initial sync process, see the Replica Set Syncing section.有关MongoDB初始同步过程的概述,请参阅副本集同步部分。

Initial sync operations can impact the other members of the set and create additional traffic to the primary. 初始同步操作可能会影响集合中的其他成员,并为主要成员创建额外的流量。The syncing member requires another member of the set that is accessible and up to date.同步成员需要集合中另一个可访问且最新的成员。

If the instance has no data, you can follow the Add Members to a Replica Set or Replace a Replica Set Member procedure to add a new member to a replica set.如果实例没有数据,可以按照将成员添加到副本集替换副本集成员过程向副本集添加新成员。

You can also force a mongod that is already a member of the set to perform an initial sync by restarting the instance without the contents of the dbPath directory:您还可以通过重新启动实例而不使用dbPath目录的内容来强制已经是集合成员的mongod执行初始同步:

  1. Stop the member's mongod instance. 停止成员的mongod实例。To ensure a clean shutdown, use the db.shutdownServer() method from mongosh or on Linux systems, the mongod --shutdown option.要确保完全关闭,请使用mongosh或Linux系统中的db.shutdownServer()方法,mongod-shutdown选项。
  2. (Optional) Make a backup of all data and sub-directories from the member's dbPath directory. (可选)备份成员dbPath目录中的所有数据和子目录。If a full backup is not required, consider backing up just the diagnostic.data directory to preserve potentially-useful troubleshooting data in the event of an issue. 如果不需要完整备份,请考虑仅备份diagnostic.data目录,以在出现问题时保留可能有用的故障排除数据。See Full Time Diagnostic Data Capture for more information.有关详细信息,请参阅全职诊断数据捕获
  3. Delete all data and sub-directories from the member's dbPath directory.从成员的dbPath目录中删除所有数据和子目录。
  4. Restart the mongod process.重新启动mongod进程

At this point, the mongod performs an initial sync. 此时,mongod执行初始同步。The length of the initial sync process depends on the size of the database and the network latency between members of the replica set.初始同步过程的长度取决于数据库的大小和副本集成员之间的网络延迟。

Sync by Copying Data Files from Another Member通过从其他成员复制数据文件进行同步

This approach "seeds" a new or stale member using the data files from an existing member of the replica set. 这种方法使用副本集现有成员的数据文件“种子”新成员或过时成员。The data files must be sufficiently recent to allow the new member to catch up with the oplog. 数据文件必须足够新,以允许新成员赶上oplogOtherwise the member would need to perform an initial sync.否则,成员将需要执行初始同步。

Copy the Data Files复制数据文件

You can capture the data files as either a snapshot or a direct copy. 您可以将数据文件捕获为快照或直接副本。However, in most cases you cannot copy data files from a running mongod instance to another because the data files will change during the file copy operation.然而,在大多数情况下,您不能将数据文件从一个正在运行的mongod实例复制到另一个实例,因为数据文件在文件复制操作期间会发生更改。

Important重要

If copying data files, ensure that your copy includes the content of the local database.如果复制数据文件,请确保副本包含local数据库的内容。

You cannot use a mongodump backup for the data files: only a snapshot backup. 不能对数据文件使用mongodump备份:只能使用快照备份。For approaches to capturing a consistent snapshot of a running mongod instance, see the MongoDB Backup Methods documentation.有关捕获正在运行的mongod实例的一致快照的方法,请参阅MongoDB备份方法文档。

Sync the Member同步成员

After you have copied the data files from the "seed" source, start the mongod instance with a new members[n]._id and allow it to apply all operations from the oplog until it reflects the current state of the replica set. 从“种子”源复制数据文件后,使用新members[n]._id启动mongod实例,并允许它应用oplog中的所有操作,直到它反映副本集的当前状态。To see the current status of the replica set, use rs.printSecondaryReplicationInfo() or rs.status().要查看副本集的当前状态,请使用rs.printSecondaryReplicationInfo()rs.status()

←  Force a Member to Become PrimaryConfigure Replica Set Tag Sets →