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

On this page本页内容

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 versions 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. 对于配置服务器副本集(CSR),包括--configsvr选项。Include any other options as appropriate for your deployment.包括适用于您的部署的任何其他选项。

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 binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. 在绑定到非本地主机(例如,可公开访问的)IP地址之前,请确保已保护您的群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整清单,请参阅Security ChecklistAt minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证强化网络基础设施

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

New in version 3.6:3.6版中新增:

All MongoDB collections have UUIDs by default. 默认情况下,所有MongoDB集合都有UUID。When 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,只需发出以下命令:

mongo

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

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.数据目录复制到副本集其他成员的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. 在连接到主服务器的mongosh会话中,使用rs.add()方法将辅助服务器添加到副本集中。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 andis 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连接到主服务器,并使用rs.add()将每个次服务器添加到副本集中。

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

←  Back Up and Restore with MongoDB ToolsBackup and Restore Sharded Clusters →