Docs HomeMongoDB Manual

Restore a Replica Set from MongoDB Backups从MongoDB备份恢复副本集

This procedure outlines the process for taking MongoDB data and restoring that data into a new replica set. 此过程概述了获取MongoDB数据并将该数据恢复到新副本集中的过程。Use this approach for seeding test deployments from production backups or as part of disaster recovery.使用此方法从生产备份中播种测试部署,或将其作为灾难恢复的一部分。

Important

You cannot restore a single data set to three new mongod instances and then create a replica set. 不能将单个数据集恢复到三个新的mongod实例,然后再创建副本集。If you copy the data set to each mongod instance and then create the replica set, MongoDB will force the secondaries to perform an initial sync. 如果将数据集复制到每个mongod实例,然后创建副本集,MongoDB将强制辅助执行初始同步。The procedures in this document describe the correct and efficient ways to deploy a restored replica set.本文档中的过程描述了部署恢复的复制副本集的正确而高效的方法。

You can also use mongorestore to restore database files using data created with mongodump. 您还可以使用mongorestore使用mongodump创建的数据来恢复数据库文件。See Back Up and Restore with MongoDB Tools for more information.有关更多信息,请参阅使用MongoDB工具备份和恢复

Restore Database into a Single Node Replica Set将数据库恢复到单节点副本集中

1

Obtain backup MongoDB Database files.获取MongoDB数据库备份文件。

The backup files may come from a file system snapshot. The MongoDB Cloud Manager produces MongoDB database files for stored snapshots and point in time snapshots. 备份文件可能来自文件系统快照MongoDB Cloud Manager存储的快照时间点快照生成MongoDB数据库文件。For Ops Manager, an on-premise solution available in MongoDB Enterprise Advanced, see also the Ops Manager Backup overview.对于Ops Manager,MongoDB Enterprise Advanced中提供的内部部署解决方案,另请参阅Ops Manager备份概述

Considerations for Encrypted Storage Engines加密存储引擎的注意事项
For encrypted storage engines that use AES256-GCM encryption mode, AES256-GCM requires that every process use a unique counter block value with the key.对于使用AES256-GCM加密模式的加密存储引擎AES256-GCM要求每个进程使用唯一的计数器块值和键。For encrypted storage engine configured with AES256-GCM cipher: 对于配置有AES256-GCM密码的加密存储引擎
Restoring from Hot Backup从热备份恢复
Starting in 4.2, if you restore from files taken via "hot" backup (i.e. the mongod is running), MongoDB can detect "dirty" keys on startup and automatically rollover the database key to avoid IV (Initialization Vector) reuse.从4.2开始,如果你从通过“热”备份(即mongod正在运行)获取的文件中恢复,MongoDB可以在启动时检测到“脏”键,并自动滚动数据库键,以避免IV(初始化向量)重用。
Restoring from Cold Backup从冷备份恢复

However, if you restore from files taken via "cold" backup (i.e. the mongod is not running), MongoDB cannot detect "dirty" keys on startup, and reuse of IV voids confidentiality and integrity guarantees.然而,如果您从通过“冷”备份获取的文件中恢复(即mongod没有运行),MongoDB在启动时无法检测到“脏”键,并且IV的重用将失去机密性和完整性保证。

Starting in 4.2, to avoid the reuse of the keys after restoring from a cold filesystem snapshot, MongoDB adds a new command-line option --eseDatabaseKeyRollover. 从4.2开始,为了避免从冷文件系统快照恢复后重用键,MongoDB添加了一个新的命令行选项--eseDatabaseKeyRolloverWhen started with the --eseDatabaseKeyRollover option, the mongod instance rolls over the database keys configured with AES256-GCM cipher and exits.当使用--eseDatabaseKeyRollover选项启动时,mongod实例会滚动使用AES256-GCM密码配置的数据库键并退出。

Tip
  • In general, if using filesystem based backups for MongoDB Enterprise 4.2+, use the "hot" backup feature, if possible.通常,如果MongoDB Enterprise 4.2+使用基于文件系统的备份,请尽可能使用“热”备份功能。
  • For MongoDB Enterprise versions 4.0 and earlier, if you use AES256-GCM encryption mode, do not make copies of your data files or restore from filesystem snapshots ("hot" or "cold").对于MongoDB Enterprise 4.0及更早版本,如果使用AES256-GCM加密模式,请不要复制数据文件或从文件系统快照(“热”或“冷”)进行恢复。
2

Drop the local database if it exists in the backup.如果备份中存在local数据库,请删除该数据库。

If you are restoring from a filesystem backup (or any backup with the local database), drop the local database.如果要从文件系统备份(或使用local数据库的任何备份)进行恢复,请删除local数据库。

Start a standalone mongod using the data files from the backup as the data path.使用备份中的数据文件作为数据路径启动一个独立的mongod

mongod --dbpath /data/db

Drop the local database.删除local数据库。

Connect mongosh to the mongod instance and drop the local database.mongosh连接到mongod实例并删除本地数据库。

use local
db.dropDatabase()

Shut down the standalone.关闭单机版。

3

Start a new single-node replica set.启动新的单节点复制副本集。

Start a mongod instance as a new single-node replica set. mongod实例作为新的单节点副本集启动。Specify the path to the backup data files with --dbpath option and the replica set name with the --replSet option. 使用--dbpath选项指定备份数据文件的路径,使用--replSet选项指定副本集名称。For config server replica set (CSRS), include the --configsvr option. Include any other options as appropriate for your deployment.对于配置服务器副本集(CSRS),包括--configsvr选项。包括适用于您的部署的任何其他选项。

Note

Starting in MongoDB 3.6, if your replica set members are run on different hosts or if you wish remote clients to connect to your instance, you must specify the net.bindIp setting (or --bind_ip).从MongoDB 3.6开始,如果您的副本集成员在不同的主机上运行,或者您希望远程客户端连接到您的实例,则必须指定net.bindIp设置(或--bind_ip)。

Warning

Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证强化网络基础设施

mongod --dbpath /data/db --replSet <replName>
Note

New in version 3.6:

All MongoDB collections have UUIDs by default. 默认情况下,所有MongoDB集合都有UUIDWhen MongoDB restores collections, the restored collections retain their original UUIDs. 当MongoDB恢复集合时,恢复的集合将保留其原始UUID。When restoring a collection where no UUID was present, MongoDB generates a UUID for the restored collection.当恢复一个没有UUID的集合时,MongoDB会为恢复的集合生成一个UUID。

For more information on collection UUIDs, see Collections.有关集合UUID的详细信息,请参阅集合

4

Connect mongosh to the mongod instance.mongosh连接到mongod实例。

From the same machine where one of the mongod is running (in this tutorial, mongodb0.example.net), start mongosh. 在运行其中一个mongod的同一台机器上(在本教程中,mongodb0.example.net),启动mongoshTo connect to the mongod listening to localhost on the default port of 27017, simply issue:要连接到默认端口27017上监听localhost的mongod,只需发出:

mongosh

Depending on your path, you may need to specify the path to the mongosh binary.根据您的路径,您可能需要指定mongosh二进制文件的路径。

If your mongod is not running on the default port, specify the --port option for mongosh.如果您的mongod没有在默认端口上运行,请为mongosh指定--port选项。

5

Initiate the new replica set.启动新的复制副本集。

Use rs.initiate() on one and only one member of the replica set:在副本集中的一个且仅一个成员上使用rs.initiate()

rs.initiate( {
_id : <replName>,
members: [ { _id : 0, host : <host:port> } ]
})

MongoDB initiates a set that consists of the current member and that uses the default replica set configuration.MongoDB启动一个由当前成员组成的集合,该集合使用默认的副本集配置。

Add Members to the Replica Set将成员添加到副本集中

MongoDB provides two options for restoring secondary members of a replica set:MongoDB为恢复副本集的辅助成员提供了两个选项:

Note

If your database is large, initial sync can take a long time to complete. For large databases, it might be preferable to copy the database files onto each host.如果数据库很大,则初始同步可能需要很长时间才能完成。对于大型数据库,最好将数据库文件复制到每个主机上。

Copy Database Files and Restart mongod Instance复制数据库文件并重新启动mongod实例

Use the following sequence of operations to "seed" additional members of the replica set with the restored data by copying MongoDB data files directly.通过直接复制MongoDB数据文件,使用以下操作序列为副本集的其他成员“播种”恢复的数据。

1

Shut down the mongod instance that you restored.关闭已恢复的mongod实例。

Use --shutdown or db.shutdownServer() to ensure a clean shut down.使用--shutdowndb.shutdownServer()确保干净关闭。

2

Copy the primary's data directory to each secondary.将主目录的数据目录复制到每个辅助目录。

Copy the primary's data directory into the dbPath of the other members of the replica set.primary的数据目录复制到副本集其他成员的dbPath中。

3

Start the mongod instance that you restored.启动已恢复的mongod实例。

4

Add the secondaries to the replica set.将辅助映像添加到复制副本集中。

In a mongosh session that is connected to the primary, add the secondaries to the replica set using the rs.add() method. 在连接到primarymongosh会话中,使用rs.add()方法将secondary添加到副本集中。See Deploy a Replica Set for more information about deploying a replica set.有关部署副本集的详细信息,请参阅部署副本集

Update Secondaries using Initial Sync使用初始同步更新辅助项

Use the following sequence of operations to "seed" additional members of the replica set with the restored data using the default initial sync operation.使用以下操作序列,使用默认的初始同步操作为副本集的其他成员“播种”已恢复的数据。

1

Empty the data directory for each prospective replica set member.清空每个潜在复制副本集成员的数据目录。

For example, if the replica set member has a storage.dbPath or --dbpath of /data/db, you must ensure that directory exists and is empty.例如,如果复制副本集成员的storage.dbPath--dbpath/data/db,则必须确保目录存在并且为空。

2

Start each replica set member.启动每个复制副本集成员。

3

Add each prospective member to the replica set.将每个潜在成员添加到复制副本集中。

Connect to the primary using the mongo shell and add each secondary to the replica set using rs.add().使用mongo shell连接到primary,并使用rs.add()将每个secondary添加到副本集中。

When you add a member to the replica set, Initial Sync copies the data from the primary to the new member.将成员添加到副本集中时,初始同步会将数据从primary复制到新成员。