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.使用此方法从生产备份中播种测试部署,或将其作为灾难恢复的一部分。
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将数据库恢复到单节点副本集中
Obtain backup MongoDB Database files.获取MongoDB数据库备份文件。
The backup files may come from a file system snapshot. The MongoDB Cloud Manager备份文件可能来自文件系统快照。MongoDB Cloud Manager为存储的快照和时间点快照生成MongoDB数据库文件。 produces MongoDB database files for stored snapshots and point in time snapshots.
For Ops Manager, an on-premise solution available in MongoDB Enterprise Advanced对于Ops Manager,MongoDB Enterprise Advanced, see also the Ops Manager Backup overview.
中提供的内部部署解决方案,另请参阅Ops Manager备份概述。
Considerations for Encrypted Storage Engines加密存储引擎的注意事项For encrypted storage engines that use对于使用AES256-GCM加密模式的加密存储引擎,AES256-GCM
encryption mode,AES256-GCM
requires that every process use a unique counter block value with the key.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从4.2开始,如果你从通过“热”备份(即mongod
is running), MongoDB can detect "dirty" keys on startup and automatically rollover the database key to avoid IV (Initialization Vector) reuse.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从4.2开始,为了避免从冷文件系统快照恢复后重用键,MongoDB添加了一个新的命令行选项--eseDatabaseKeyRollover
.--eseDatabaseKeyRollover
。When started with the当使用--eseDatabaseKeyRollover
option, themongod
instance rolls over the database keys configured withAES256-GCM
cipher and exits.--eseDatabaseKeyRollover
选项启动时,mongod
实例会滚动使用AES256-GCM密码配置的数据库键并退出。
TipIn 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对于MongoDB Enterprise 4.0及更早版本,如果使用AES256-GCM
encryption mode, do not make copies of your data files or restore from filesystem snapshots ("hot" or "cold").AES256-GCM
加密模式,请不要复制数据文件或从文件系统快照(“热”或“冷”)进行恢复。
Drop the local
database if it exists in the backup.如果备份中存在local
数据库,请删除该数据库。
local
database if it exists in the backup.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
using the data files from the backup as the data path.mongod --dbpath /data/db
Drop the local
database.删除local
数据库。
local
database.Connect 将mongosh
to the mongod
instance and drop the local
database.mongosh
连接到mongod
实例并删除本地数据库。
use local
db.dropDatabase()
Shut down the standalone.关闭单机版。
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 对于配置服务器副本集(CSRS),包括--configsvr
option. Include any other options as appropriate for your deployment.--configsvr
选项。包括适用于您的部署的任何其他选项。
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 从MongoDB 3.6开始,如果您的副本集成员在不同的主机上运行,或者您希望远程客户端连接到您的实例,则必须指定net.bindIp
setting (or --bind_ip
).net.bindIp
设置(或--bind_ip
)。
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>
New in version 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的详细信息,请参阅集合。
Connect mongosh
to the mongod
instance.将mongosh
连接到mongod
实例。
mongosh
to the mongod
instance.From the same machine where one of the 在运行其中一个mongod
is running (in this tutorial, mongodb0.example.net
), start mongosh
. mongod
的同一台机器上(在本教程中,mongodb0.example.net
),启动mongosh
。To 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
选项。
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为恢复副本集的辅助成员提供了两个选项:
Manually copy the database files手动复制数据库文件to each data directory.到每个数据目录。Allow initial sync允许初始同步to distribute data automatically.以自动分发数据。
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
实例
mongod
InstanceUse the following sequence of operations to "seed" additional members of the replica set with the restored data by copying MongoDB data files directly.通过直接复制MongoDB数据文件,使用以下操作序列为副本集的其他成员“播种”恢复的数据。
Shut down the mongod
instance that you restored.关闭已恢复的mongod
实例。
mongod
instance that you restored.Use 使用--shutdown
or db.shutdownServer()
to ensure a clean shut down.--shutdown
或db.shutdownServer()
确保干净关闭。
Start the mongod
instance that you restored.启动已恢复的mongod
实例。
mongod
instance that you restored.Add the secondaries to the replica set.将辅助映像添加到复制副本集中。
In a 在连接到primary的mongosh
session that is connected to the primary, add the secondaries to the replica set using the rs.add()
method. mongosh
会话中,使用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.使用以下操作序列,使用默认的初始同步操作为副本集的其他成员“播种”已恢复的数据。
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
,则必须确保目录存在并且为空。
Add each prospective member to the replica set.将每个潜在成员添加到复制副本集中。
Connect to the primary using the 使用mongo shell连接到primary,并使用mongo
shell and add each secondary to the replica set using rs.add()
.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复制到新成员。