Docs HomeMongoDB Manual

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覆盖了该成员尚未复制的操作日志条目时,该成员就会变得“过时”。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提供了两种执行初始同步的选项:

  • Restart the mongod with an empty data directory and let MongoDB's normal initial syncing feature restore the data. 用一个空的数据目录重新启动mongod,让MongoDB的正常初始同步功能恢复数据。This is the more simple option but may take longer to replace the data.这是一个更简单的选项,但替换数据可能需要更长的时间。

    See Automatically Sync a Member.请参阅自动同步成员

  • Restart the machine with a copy of a recent data directory from another member in the replica set. 使用副本集中另一个成员的最近数据目录的副本重新启动计算机。This procedure can replace the data more quickly but requires more manual steps.此过程可以更快地替换数据,但需要更多的手动步骤。

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

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中的db.shutdownServer()方法,或者在Linux系统上使用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. The length of the initial sync process depends on the size of the database and the network latency between members of the replica set.此时,mongod执行初始同步。初始同步进程的长度取决于数据库的大小和副本集成员之间的网络延迟。

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()