Database Manual / Self-Managed Deployments / Administration / Backup Methods / Restore Sharded Clusters

Restore a Self-Managed Sharded Cluster恢复自我管理的分片群集

This procedure restores a sharded cluster from an existing backup snapshot, such as Logical Volume Manager (LVM) snapshots. 此过程从现有的备份快照(如逻辑卷管理器(LVM)快照)还原分片群集。The source and target sharded cluster must have the same number of shards. For information on creating LVM snapshots for all components of a sharded cluster, see Back Up a Self-Managed Sharded Cluster with File System Snapshots.源和目标分片集群必须具有相同数量的分片。有关为分片集群的所有组件创建LVM快照的信息,请参阅使用文件系统快照备份自管理分片集群

Note

To use mongodump and mongorestore as a backup strategy for sharded clusters, see Back Up a Self-Managed Sharded Cluster with a Database Dump.要使用mongodump和mongorestore作为分片集群的备份策略,请参阅使用数据库转储备份自管理分片集群

Sharded clusters can also use one of the following coordinated backup and restore processes, which maintain the atomicity guarantees of transactions across shards:分片集群还可以使用以下协调的备份和恢复过程之一,以维护跨分片事务的原子性保证:

Considerations注意事项

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密码配置的数据库键并退出。

Before You Begin开始之前

Starting in MongoDB 8.0, you can use the directShardOperations role to perform maintenance operations that require you to execute commands directly against a shard.从MongoDB 8.0开始,您可以使用directShardOperations角色执行维护操作,这些操作要求您直接对分片执行命令。

Warning

Running commands using the directShardOperations role can cause your cluster to stop working correctly and may cause data corruption. 使用directShardOperations角色运行命令可能会导致集群停止正常工作,并可能导致数据损坏。Only use the directShardOperations role for maintenance purposes or under the guidance of MongoDB support. 仅将directShardOperations角色用于维护目的或在MongoDB支持的指导下使用。Once you are done performing maintenance operations, stop using the directShardOperations role.完成维护操作后,停止使用directShardOperations角色。

A. (Optional) Review Replica Set Configurations(可选)查看副本集配置

This procedure initiates a new replica set for the Config Server Replica Set (CSRS) and each shard replica set using the default configuration. To use a different replica set configuration for your restored CSRS and shards, you must reconfigure the replica set(s).此过程使用默认配置为配置服务器副本集(CSRS)和每个分片副本集启动一个新的副本集。要为还原的CSRS和分片使用不同的副本集配置,您必须重新配置副本集。

If your source cluster is running correctly and is accessible, connect a mongo shell to the primary replica set member in each replica set. Next, run rs.conf() to view the replica configuration document.如果源集群运行正常且可访问,请将mongo shell连接到每个副本集中的主副本集成员。接下来,运行rs.conf()查看副本配置文档。

If you cannot access one or more components of the source sharded cluster, please reference any existing internal documentation to reconstruct the configuration requirements for each shard replica set and the config server replica set.如果您无法访问源分片集群的一个或多个组件,请参考任何现有的内部文档,以重建每个分片副本集和配置服务器副本集的配置要求。

B. Prepare the Target Host for Restoration准备目标主机进行恢复

Storage Space Requirements存储空间要求
Ensure the target host hardware has sufficient open storage space for the restored data. If the target host contains existing sharded cluster data that you want to keep, ensure that you have enough storage space for both the existing data and the restored data.确保目标主机硬件有足够的开放存储空间来存储还原的数据。如果目标主机包含要保留的现有分片群集数据,请确保有足够的存储空间来存储现有数据和还原的数据。
LVM RequirementsLVM要求
For LVM snapshots, you must have at least one LVM managed volume group and a logical volume with enough free space for the extracted snapshot data.对于LVM快照,您必须至少有一个LVM管理的卷组和一个具有足够可用空间用于提取快照数据的逻辑卷。
MongoDB Version RequirementsMongoDB版本要求

Ensure the target host and source host have the same MongoDB Server version. To check the version of MongoDB available on a host machine, run mongod --version from the terminal or shell.确保目标主机和源主机具有相同的MongoDB Server版本。要检查主机上可用的MongoDB版本,请从终端或shell运行mongod --version

For complete documentation on installation, see Install MongoDB.有关安装的完整文档,请参阅安装MongoDB

Shut Down Running MongoDB Processes关闭正在运行的MongoDB进程

If restoring to an existing cluster, shut down the mongod or mongos process on the target host.如果恢复到现有集群,请关闭目标主机上的mongodmongos进程。

For hosts running mongos, connect a mongo shell to the mongos and run db.shutdownServer() from the admin database:对于运行mongos的主机,将mongoshell连接到mongos,并从admin数据库运行db.shutdownServer()

use admin
db.shutdownServer()

For hosts running a mongod, connect a mongo shell to the mongod and run db.hello():对于运行mongod的主机,将mongo shell连接到mongod并运行db.hello()

Prepare Data Directory准备数据目录

Create a directory on the target host for the restored database files. Ensure that the user that runs the mongod has read, write, and execute permissions for all files and subfolders in that directory:在目标主机上为还原的数据库文件创建一个目录。确保运行mongod的用户对该目录中的所有文件和子文件夹都具有读取、写入和执行权限:

sudo mkdir /path/to/mongodb
sudo chown -R mongodb:mongodb /path/to/mongodb
sudo chmod -R 770 /path/to/mongodb

Substitute /path/to/mongodb with the path to the data directory you created. On RHEL / CentOS, Amazon Linux, and SUSE, the default username is mongod./path/to/mongodb替换为您创建的数据目录的路径。在RHEL/CentOS、Amazon Linux和SUSE上,默认用户名是mongod

Prepare Log Directory准备日志目录

Create a directory on the target host for the mongod log files. 在目标主机上为mongod日志文件创建一个目录。Ensure that the user that runs the mongod has read, write, and execute permissions for all files and subfolders in that directory:确保运行mongod的用户对该目录中的所有文件和子文件夹都具有读取、写入和执行权限:

sudo mkdir /path/to/mongodb/logs
sudo chown -R mongodb:mongodb /path/to/mongodb/logs
sudo chmod -R 770 /path/to/mongodb/logs

Substitute /path/to/mongodb/logs with the path to the log directory you created. On RHEL / CentOS, Amazon Linux, and SUSE, the default username is mongod./path/to/mongodb/logs替换为您创建的日志目录的路径。在RHEL/CentOS、Amazon Linux和SUSE上,默认用户名是mongod

Create Configuration File创建配置文件

This procedure assumes starting a mongod with a configuration file.此过程假设使用配置文件启动mongod

Create the configuration file in your preferred location. Ensure that the user that runs the mongod has read and write permissions on the configuration file:在首选位置创建配置文件。确保运行mongod的用户对配置文件具有读写权限:

sudo touch /path/to/mongod.conf
sudo chown mongodb:mongodb /path/to/mongodb/mongod.conf
sudo chmod 644 /path/to/mongodb/mongod.conf

On RHEL / CentOS, Amazon Linux, and SUSE, the default username is mongod.在RHEL/CentOS、Amazon Linux和SUSE上,默认用户名是mongod

Open the configuration file in your preferred text editor and modify at it as required by your deployment. 在首选文本编辑器中打开配置文件,并根据部署需要进行修改。Alternatively, if you have access to the original configuration file for the mongod, copy it to your preferred location on the target host.或者,如果您可以访问mongod的原始配置文件,请将其复制到目标主机上的首选位置。

Important

Validate that your configuration file includes the following settings:验证配置文件是否包含以下设置:

  • storage.dbPath must be set to the path to your preferred data directory.必须设置为首选数据目录的路径。
  • systemLog.path must be set to the path to your preferred log directory必须设置为首选日志目录的路径
  • net.bindIp must include the IP address of the host machine.必须包含主机的IP地址。
  • replication.replSetName has the same value across each member in any given replica set.在任何给定的副本集中,每个成员的值都是相同的。
  • sharding.clusterRole has the same value across each member in any given replica set.在任何给定的副本集中,每个成员的值都是相同的。
  • You must also specify the same startup options for your new deployment that were specified in the snapshot.您还必须为新部署指定与快照中指定的启动选项相同的启动选项

C. Restore Config Server Replica Set还原配置服务器副本集

1

Restore the CSRS primary mongod data files.还原CSRS主mongod数据文件。

Select the tab that corresponds to your preferred backup method:选择与首选备份方法对应的选项卡:

LVM Snapshot
  1. Mount the LVM snapshot on the target host machine. The specific steps for mounting an LVM snapshot depends on your LVM configuration.在目标主机上装载LVM快照。安装LVM快照的具体步骤取决于LVM配置。

    The following example assumes an LVM snapshot created using the Create a Snapshot step in the Back Up and Restore a Self-Managed Deployment with Filesystem Snapshots procedure.以下示例假设使用“使用文件系统快照备份和还原自管理部署”过程中的“创建快照”步骤创建LVM快照。

    lvcreate --size 250GB --name mongod-datafiles-snapshot vg0
    gzip -d -c mongod-datafiles-snapshot.gz | dd o/dev/vg0/mongod-datafiles-snapshot
    mount /dev/vg0/mongod-datafiles-snapshot /snap/mongodb

    This example may not apply to all possible LVM configurations. Refer to the LVM documentation for your system for more complete guidance on LVM restoration.此示例可能不适用于所有可能的LVM配置。有关LVM恢复的更完整指导,请参阅系统的LVM文档。

  2. Copy the mongod data files from the snapshot mount to the data directory created in B. Prepare the Target Host for Restoration:mongod数据文件从快照装载复制到B中创建的数据目录。准备目标主机进行还原:

    cp -a /snap/mongodb/path/to/mongodb /path/to/mongodb

    The -a option recursively copies the contents of the source path to the destination path while preserving folder and file permissions.-a选项递归地将源路径的内容复制到目标路径,同时保留文件夹和文件权限。

  3. Comment out or omit the following configuration file settings:注释掉或省略以下配置文件设置:

    #replication:
    # replSetName: myCSRSName
    #sharding:
    # clusterRole: configsvr

    To start the mongod using a configuration file, specify the --config option in the command line specifying the full path to the configuration file.要使用配置文件启动mongod,请在命令行中指定--config选项,指定配置文件的完整路径。

    mongod --config /path/to/mongodb/mongod.conf

    If you are restoring from a namespace-filtered snapshot, specify the --restore option.如果要从namespace-filtered快照进行还原,请指定--restore选项。

    mongod --config /path/to/mongod/mongod.conf --restore

    If you have mongod configured to run as a system service, start it using the recommended process for your system service manager.如果您已将mongod配置为作为系统服务运行,请使用系统服务管理器推荐的流程启动它。

    After the mongod starts, connect to it using the mongo shell.mongod启动后,使用mongo shell连接到它。

Other Backup Files (NOT mongodump)其他备份文件(非mongodump
  1. Make the data files stored in your selected backup medium accessible on the host. This may require mounting the backup volume, opening the backup in a software utility, or using another tool to extract the data to disk. Refer to the documentation for your preferred backup tool for instructions on accessing the data contained in the backup.使存储在所选备份介质中的数据文件在主机上可访问。这可能需要装载备份卷,在软件实用程序中打开备份,或使用其他工具将数据提取到磁盘。有关访问备份中包含的数据的说明,请参阅首选备份工具的文档。
  2. Copy the mongod data files from the backup data location to the data directory created in B. Prepare the Target Host for Restoration:mongod数据文件从备份数据位置复制到B中创建的数据目录。准备目标主机进行还原:

    cp -a /backup/mongodb/path/to/mongodb /path/to/mongodb

    The -a option recursively copies the contents of the source path to the destination path while preserving folder and file permissions.-a选项递归地将源路径的内容复制到目标路径,同时保留文件夹和文件权限。

  3. Comment out or omit the following configuration file settings:注释掉或省略以下配置文件设置:

    #replication:
    # replSetName: myCSRSName
    #sharding:
    # clusterRole: configsvr
  4. To start the mongod using a configuration file, specify the --config option in the command line specifying the full path to the configuration file.要使用配置文件启动mongod,请在命令行中指定--config选项,指定配置文件的完整路径。

    mongod --config /path/to/mongodb/mongod.conf

    If restoring from a namespace-filtered snapshot, also specify the --restore option.如果从命名空间筛选的快照进行还原,请同时指定--restore选项。

    mongod --config /path/to/mongod/mongod.conf --restore

    Note

    Cloud Manager or Ops Manager Only仅限云管理器或运维管理器

    If performing a manual restoration of a Cloud Manager or Ops Manager backup, you must specify the disableLogicalSessionCacheRefresh server parameter prior to startup.如果执行Cloud Manager或Ops Manager备份的手动还原,则必须在启动前指定disableLogicalSessionCacheRefresh服务器参数。

    mongod --config /path/to/mongodb/mongod.conf \
    --setParameter disableLogicalSessionCacheRefresh=true

    If you have mongod configured to run as a system service, start it using the recommended process for your system service manager.如果您已将mongod配置为作为系统服务运行,请使用系统服务管理器推荐的流程启动它。

    After the mongod starts, connect to it using the mongo shell.mongod启动后,使用mongo shell连接到它。

2

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

Use db.dropDatabase() to drop the local database:使用db.dropDatabase()删除local数据库:

use local
db.dropDatabase()
3

Insert the filtered file list into the local database.将筛选后的文件列表插入本地数据库。

This step is only required if you are restoring from a namespace-filtered snapshot.仅当您从命名空间筛选的快照进行还原时,才需要此步骤。

For each shard, locate the filtered file list with the following name format: <shardRsID>-filteredFileList.txt. This file contains a list of JSON objects with the following format:对于每个分片,使用以下名称格式找到筛选后的文件列表:<shardRsID>-filteredFileList.txt。此文件包含以下格式的JSON对象列表:

{
"filename":"file1",
"ns":"sampleDb1.sampleCollection1",
"uuid": "3b241101-e2bb-4255-8caf-4136c566a962"
}

Add each JSON object from each shard file to a new db.systems.collections_to_restore collection in your local database. 将每个分片文件中的每个JSON对象添加到local数据库中的新db.systems.collections_to_restore集合中。You can ignore entries with empty ns or uuid fields. When inserting entries, the uuid field must be inserted as type UUID().您可以忽略nsuuid字段为空的条目。插入条目时,uuid字段必须以UUID()类型插入。

4

Run the filtered file restore command.运行筛选文件还原命令。

This step is only required if you are restoring from a namespace-filtered snapshot.仅当您从命名空间筛选的快照进行还原时,才需要此步骤。

After inserting all entries, run the following commands:插入所有条目后,运行以下命令:

use admin
db.runCommand({"_configsvrRunRestore":1})
5

For any planned or completed shard hostname or replica set name changes, update the metadata in config.shards.对于任何计划或已完成的分片主机名或副本集名称更改,请更新config.shards中的元数据。

You can skip this step if all of the following are true:如果以下所有条件都为真,则可以跳过此步骤:

  • No shard member host machine hostname has or will change during this procedure.在此过程中,没有分片成员主机主机名已经或将要更改。
  • No shard replica set name has or will change during this procedure.在此过程中,没有或不会更改分片副本集名称。

Issue the following find() method on the shards collection in the Config Database. 配置数据库中的shards集合发出以下find()方法。Replace <shardName> with the name of the shard. <shardName>替换为分片的名称。By default the shard name is its replica set name. 默认情况下,分片名称是其副本集名称。If you added the shard using the addShard command and specified a custom name, you must specify that name to <shardName>.如果您使用addShard命令添加了分片并指定了自定义namename<shardName>

use config
db.shards.find( { "_id" : "<shardName>" } )

This operation returns a document that resembles the following:此操作返回一个类似于以下内容的文档:

{
"_id" : "shard1",
"host" : "myShardName/alpha.example.net:27018,beta.example.net:27018,charlie.example.net:27018",
"state" : 1
}

Important

The _id value must match the shardName value in the _id : "shardIdentity" document on the corresponding shard. _id值必须与相应分片上_id : "shardIdentity"文档中的shardName值匹配。When restoring the shards later in this procedure, validate that the _id field in shards matches the shardName value on the shard.稍后在此过程中恢复分片时,请验证分片中的_id字段是否与shards上的shardName值匹配。

Use the updateOne() method to update the hosts string to reflect the planned replica set name and hostname list for the shard. 使用updateOne()方法更新hosts字符串,以反映分片的计划副本集名称和主机名列表。For example, the following operation updates the host connection string for the shard with "_id" : "shard1":例如,以下操作使用"_id" : "shard1"更新分片的host连接字符串:

db.shards.updateOne(
{ "_id" : "shard1" },
{ $set : { "host" : "myNewShardName/repl1.example.net:27018,repl2.example.net:27018,repl3.example.net:27018" } }
)

Repeat this process until all shard metadata accurately reflects the planned replica set name and hostname list for each shard in the cluster.重复此过程,直到所有分片元数据准确反映集群中每个分片的计划副本集名称和主机名列表。

Note

If you do not know the shard name, issue the find() method on the shards collection with an empty filter document {}:如果您不知道分片名称,请在shards集合上使用空的筛选器文档{}发出find()方法:

use config
db.shards.find({})

Each document in the result set represents one shard in the cluster. 结果集中的每个文档代表集群中的一个分片。For each document, check the host field for a connection string that matches the shard in question, i.e. a matching replica set name and member hostname list. 对于每个文档,检查host字段中是否有与所讨论的分片匹配的连接字符串,即匹配的副本集名称和成员主机名列表。Use the _id of that document in place of <shardName>.使用该文档的_id代替<shardName>

6

Restart the mongod as a new single-node replica set.mongod重新启动为新的单节点副本集。

Shut down the mongod. Uncomment or add the following configuration file options:关掉mongod。取消注释或添加以下配置文件选项:

replication:
replSetName: myNewCSRSName
sharding:
clusterRole: configsvr

If you want to change the replica set name, you must update the replSetName field with the new name before proceeding.如果要更改副本集名称,则必须在继续之前用新名称更新replSetName字段。

Start the mongod with the updated configuration file:使用更新的配置文件启动mongod

mongod --config /path/to/mongodb/mongod.conf

If you have mongod configured to run as a system service, start it using the recommended process for your system service manager.如果您已将mongod配置为作为系统服务运行,请使用系统服务管理器推荐的流程启动它。

After the mongod starts, connect to it using the mongo shell.mongod启动后,使用mongo shell连接到它。

7

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

Initiate the replica set using rs.initiate() with the default settings.使用默认设置的rs.initiate()启动副本集。

rs.initiate()

Once the operation completes, use rs.status() to check that the member has become the primary.操作完成后,使用rs.status()检查成员是否已成为主成员

8

Add additional replica set members.添加其他副本集成员。

For each replica set member in the CSRS, start the mongod on its host machine. 对于CSRS中的每个副本集成员,在其主机上启动mongodOnce you have started up all remaining members of the cluster successfully, connect a mongo shell to the primary replica set member. 成功启动集群的所有剩余成员后,将mongo shell连接到主副本集成员。From the primary, use the rs.add() method to add each member of the replica set. 从主副本开始,使用rs.add()方法添加副本集的每个成员。Include the replica set name as the prefix, followed by the hostname and port of the member's mongod process:将副本集名称作为前缀,后跟成员mongod进程的主机名和端口:

rs.add("config2.example.net:27019")
rs.add("config3.example.net:27019")

If you want to add the member with specific replica member configuration settings, you can pass a document to rs.add() that defines the member hostname and any members settings your deployment requires.如果要添加具有特定副本member配置设置的成员,可以将一个文档传递给rs.add(),该文档定义了成员主机名和部署所需的任何members设置。

rs.add(
{
"host" : "config2.example.net:27019",
priority: <int>,
votes: <int>,
tags: <int>
}
)

Each new member performs an initial sync to catch up to the primary. Depending on factors such as the amount of data to sync, your network topology and health, and the power of each host machine, initial sync may take an extended period of time to complete.每个新成员执行rs.add()初始同步以赶上主成员。根据要同步的数据量、网络拓扑和健康状况以及每台主机的功率等因素,初始同步可能需要很长时间才能完成。

The replica set may elect a new primary while you add additional members. 在添加其他成员时,副本集可能会选择一个新的主副本。Use rs.status() to identify which member is the current primary. 使用rs.status()确定哪个成员是当前的主成员。You can only run rs.add() from the primary.您只能从主服务器运行rs.add()

9

Configure any additional required replication settings.配置任何其他所需的复制设置。

The rs.reconfig() method updates the replica set configuration based on a configuration document passed in as a parameter. rs.reconfig()方法根据作为参数传入的配置文档更新副本集配置。You must run reconfig() against the primary member of the replica set.您必须对副本集的主要成员运行reconfig()

Reference the original configuration file output of the replica set as identified in step A. Review Replica Set Configurations and apply settings as needed.参考步骤A中标识的副本集的原始配置文件输出。查看副本集配置并根据需要应用设置。

D. Restore Each Shard Replica Set还原每个分片副本集

1

Restore the shard primary mongod data files.恢复分片主mongod数据文件。

Select the tab that corresponds to your preferred backup method:选择与首选备份方法对应的选项卡:

LVM Snapshot
  1. Mount the LVM snapshot on the target host machine. The specific steps for mounting an LVM snapshot depends on your LVM configuration.在目标主机上装载LVM快照。安装LVM快照的具体步骤取决于LVM配置。

    The following example assumes an LVM snapshot created using the Create a Snapshot step in the Back Up and Restore a Self-Managed Deployment with Filesystem Snapshots procedure.以下示例假设使用“使用文件系统快照备份和还原自管理部署”过程中的“创建快照”步骤创建LVM快照。

    lvcreate --size 250GB --name mongod-datafiles-snapshot vg0
    gzip -d -c mongod-datafiles-snapshot.gz | dd o/dev/vg0/mongod-datafiles-snapshot
    mount /dev/vg0/mongod-datafiles-snapshot /snap/mongodb

    This example may not apply to all possible LVM configurations. Refer to the LVM documentation for your system for more complete guidance on LVM restoration.此示例可能不适用于所有可能的LVM配置。有关LVM恢复的更完整指导,请参阅系统的LVM文档。

  2. Copy the mongod data files from the snapshot mount to the data directory created in B. Prepare the Target Host for Restoration:mongod数据文件从快照装载复制到B中创建的数据目录。准备目标主机进行还原:

    cp -a /snap/mongodb/path/to/mongodb /path/to/mongodb

    The -a option recursively copies the contents of the source path to the destination path while preserving folder and file permissions.-a选项递归地将源路径的内容复制到目标路径,同时保留文件夹和文件权限。

  3. Comment out or omit the following configuration file settings:注释掉或省略以下配置文件设置

    #replication:
    # replSetName: myShardName
    #sharding:
    # clusterRole: shardsvr

    To start the mongod using a configuration file, specify the --config option in the command line specifying the full path to the configuration file:要使用配置文件启动mongod,请在命令行中指定--config选项,指定配置文件的完整路径:

    mongod --config /path/to/mongodb/mongod.conf

    If you're restoring from a snapshot with a namespace filter, specify the --restore option.如果要使用命名空间筛选器从快照还原,请指定--restore选项。

    mongod --config /path/to/mongod/mongod.conf --restore

    If you have mongod configured to run as a system service, start it using the recommended process for your system service manager.如果您已将mongod配置为作为系统服务运行,请使用系统服务管理器推荐的流程启动它。

    After the mongod starts, connect to it using the mongo shell.mongod启动后,使用mongo shell连接到它。

Other Backup Files (NOT mongodump)其他备份文件(非mongodump
  1. Make the data files stored in your selected backup medium accessible on the host. This may require mounting the backup volume, opening the backup in a software utility, or using another tool to extract the data to disk. Refer to the documentation for your preferred backup tool for instructions on accessing the data contained in the backup.使存储在所选备份介质中的数据文件在主机上可访问。这可能需要装载备份卷,在软件实用程序中打开备份,或使用其他工具将数据提取到磁盘。有关访问备份中包含的数据的说明,请参阅首选备份工具的文档。
  2. Copy the mongod data files from the backup data location to the data directory created in B. Prepare the Target Host for Restoration:mongod数据文件从备份数据位置复制到B中创建的数据目录。准备目标主机进行还原:

    cp -a /backup/mongodb/path/to/mongodb /path/to/mongodb

    The -a option recursively copies the contents of the source path to the destination path while preserving folder and file permissions.-a选项递归地将源路径的内容复制到目标路径,同时保留文件夹和文件权限。

  3. Comment out or omit the following configuration file settings:注释掉或省略以下配置文件设置:

    #replication:
    # replSetName: myShardName
    #sharding:
    # clusterRole: shardsvr
  4. To start the mongod using a configuration file, specify the --config option in the command line specifying the full path to the configuration file:要使用配置文件启动mongod,请在命令行中指定--config选项,指定配置文件的完整路径:

    mongod --config /path/to/mongodb/mongod.conf

    Note

    Cloud Manager or Ops Manager Only仅限云管理器或运维管理器

    If performing a manual restoration of a Cloud Manager or Ops Manager backup, you must specify the disableLogicalSessionCacheRefresh server parameter prior to startup:如果执行Cloud Manager或Ops Manager备份的手动还原,则必须在启动前指定disableLogicalSessionCacheRefresh服务器参数:

    mongod --config /path/to/mongodb/mongod.conf \
    --setParameter disableLogicalSessionCacheRefresh=true

    If you have mongod configured to run as a system service, start it using the recommended process for your system service manager.如果您已将mongod配置为作为系统服务运行,请使用系统服务管理器推荐的流程启动它。

    After the mongod starts, connect to it using the mongo shell.mongod启动后,使用mongo shell连接到它。

2

Create a temporary user with the __system role.创建一个具有__system角色的临时用户。

During this procedure you will modify documents in the admin.system.version collection. 在此过程中,您将修改admin.system.version集合中的文档。For clusters enforcing authentication, only the __system role grants permission to modify this collection. 对于强制身份验证的集群,只有__system角色授予修改此集合的权限。You can skip this step if the cluster does not enforce authentication.如果集群不强制身份验证,则可以跳过此步骤。

Warning

The __system role entitles its holder to take any action against any object in the database. __system角色赋予其持有者对数据库中的任何对象采取任何行动的权利。This procedure includes instructions for removing the user created in this step. Do not keep this user active beyond the scope of this procedure.此过程包括删除此步骤中创建的用户的说明。请勿让此用户在本程序范围之外保持活动状态。

Consider creating this user with the clientSource authentication restriction configured such that only the specified hosts can authenticate as the privileged user.考虑使用配置的clientSource身份验证限制创建此用户,以便只有指定的主机可以作为权限用户进行身份验证。

  1. Authenticate as a user with the userAdmin role on the admin database or userAdminAnyDatabase role:使用admin数据库上的userAdmin角色或userAdminAnyDatabase角色作为用户进行身份验证:

    use admin
    db.auth("myUserAdmin","mySecurePassword")
  2. Create a user with the __system role:创建一个具有__system角色的用户:

    db.createUser(
    {
    user: "mySystemUser",
    pwd: "<replaceMeWithAStrongPassword>",
    roles: [ "__system" ]
    }
    )

    Passwords should be random, long, and complex to ensure system security and to prevent or delay malicious access.密码应该是随机的、长的和复杂的,以确保系统安全,防止或延迟恶意访问。

  3. Authenticate as the privileged user:以权限用户身份进行身份验证:

    db.auth("mySystemUser","<replaceMeWithAStrongPassword>")
3

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

Use db.dropDatabase() to drop the local database:使用db.dropDatabase()删除local数据库:

use local
db.dropDatabase()
4

Remove the minOpTimeRecovery document from the admin.system.versions collection.admin.system.versions集合中删除minOpTimeRecovery文档。

To update the sharding internals, issue the following deleteOne() method on the system.version collection in the admin database:要更新分片内部,请在admin数据库中的system.version集合上发出以下deleteOne()方法:

use admin
db.system.version.deleteOne( { _id: "minOpTimeRecovery" } )

Note

The system.version collection is an internal, system collection. You should only modify it when when given specific instructions like these.system.version集合是一个内部系统集合。只有在收到这样的特定指示时,您才应该修改它。

5

Optional: For any CSRS hostname or replica set name changes, update shard metadata in each shard's identity document.可选:对于任何CSRS主机名或副本集名称更改,请更新每个分片标识文档中的分片元数据。

You can skip this step if all of the following are true:如果以下所有条件都为真,则可以跳过此步骤:

  • The hostnames for any CSRS host did not change during this procedure.在此过程中,任何CSRS主机的主机名都没有更改。
  • The CSRS replica set name did not change during this procedure.在此过程中,CSRS副本集名称没有更改。

The system.version collection on the admin database contains metadata related to the shard, including the CSRS connection string. If either the CSRS name or any member hostnames changed while restoring the CSRS, you must update this metadata.admin数据库上的system.version集合包含与分片相关的元数据,包括CSRS连接字符串。如果在还原CSRS时更改了CSRS名称或任何成员主机名,则必须更新此元数据。

Issue the following find() method on the system.version collection in the admin database:admin数据库中的system.version集合发出以下find()方法:

use admin
db.system.version.find( {"_id" : "shardIdentity" } )

The find() method returns a document that resembles the following:find()方法返回一个类似于以下内容的文档:

{
"_id" : "shardIdentity",
"clusterId" : ObjectId("2bba123c6eeedcd192b19024"),
"shardName" : "shard1",
"configsvrConnectionString" : "myCSRSName/alpha.example.net:27019,beta.example.net:27019,charlie.example.net:27019" }

The following updateOne() method updates the document such that the host string represents the most current CSRS connection string:以下updateOne()方法更新文档,使host字符串表示最新的CSRS连接字符串:

db.system.version.updateOne(
{ "_id" : "shardIdentity" },
{ $set :
{ "configsvrConnectionString" : "myNewCSRSName/config1.example.net:27019,config2.example.net:27019,config3.example.net:27019"}
}
)

Important

The shardName value must match the _id value in the shards collection on the CSRS. Validate that the metadata on the CSRS match the metadata for the shard. shardName值必须与CSRS上分片集合中的_id值匹配。验证CSRS上的元数据是否与分片的元数据匹配。Refer to substep 3 in the C. Restore Config Server Replica Set portion of this procedure for instructions on viewing the CSRS metadata.有关查看CSRS元数据的说明,请参阅本过程中C.还原配置服务器副本集部分的子步骤3。

6

Restart the mongod as a new single-node replica set.mongod重新启动为新的单节点副本集。

Shut down the mongod. Uncomment or add the following configuration file options:关掉mongod。取消注释或添加以下配置文件选项:

replication:
replSetName: myNewShardName
sharding:
clusterRole: shardsvr

If you want to change the replica set name, you must update the replSetName field with the new name before proceeding.如果要更改副本集名称,则必须在继续之前用新名称更新replSetName字段。

Start the mongod with the updated configuration file:使用更新的配置文件启动mongod

mongod --config /path/to/mongodb/mongod.conf

If you have mongod configured to run as a system service, start it using the recommended process for your system service manager.如果您已将mongod配置为作为系统服务运行,请使用系统服务管理器推荐的流程启动它。

After the mongod starts, connect to it using the mongo shell.mongod启动后,使用mongo shell连接到它。

7

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

Initiate the replica set using rs.initiate() with the default settings.使用默认设置的rs.initiate()启动副本集。

rs.initiate()

Once the operation completes, use rs.status() to check that the member has become the primary.操作完成后,使用rs.status()检查成员是否已成为主成员

8

Add additional replica set members.添加其他副本集成员。

For each replica set member in the shard replica set, start the mongod on its host machine. Once you have started up all remaining members of the cluster successfully, connect a mongo shell to the primary replica set member. 对于分片副本集中的每个副本集成员,在其主机上启动mongod。成功启动集群的所有剩余成员后,将mongo shell连接到主副本集成员。From the primary, use the rs.add() method to add each member of the replica set. 从主副本开始,使用rs.add()方法添加副本集的每个成员。Include the replica set name as the prefix, followed by the hostname and port of the member's mongod process:将副本集名称作为前缀,后跟成员mongod进程的主机名和端口:

rs.add("repl2.example.net:27018")
rs.add("repl3.example.net:27018")

If you want to add the member with specific replica member configuration settings, you can pass a document to rs.add() that defines the member hostname and any members settings your deployment requires.如果要添加具有特定副本member配置设置的成员,可以将一个文档传递给rs.add(),该文档定义了成员主机名和部署所需的任何members设置。

rs.add(
{
"host" : "repl2.example.net:27018",
priority: <int>,
votes: <int>,
tags: <int>
}
)

Each new member performs an initial sync to catch up to the primary. Depending on factors such as the amount of data to sync, your network topology and health, and the power of each host machine, initial sync may take an extended period of time to complete.每个新成员执行初始同步以赶上主成员。根据要同步的数据量、网络拓扑和健康状况以及每台主机的功率等因素,初始同步可能需要很长时间才能完成。

The replica set may elect a new primary while you add additional members. Use rs.status() to identify which member is the current primary. 在添加其他成员时,副本集可能会选择一个新的主副本。使用rs.status()确定哪个成员是当前的主成员。You can only run rs.add() from the primary.您只能从主服务器运行rs.add()

9

Configure any additional required replication settings.配置任何其他所需的复制设置。

The rs.reconfig() method updates the replica set configuration based on a configuration document passed in as a parameter. rs.reconfig()方法根据作为参数传入的配置文档更新副本集配置。You must run reconfig() against the primary member of the replica set.您必须对副本集的主要成员运行reconfig()

Reference the original configuration file output of the replica set as identified in step A. Review Replica Set Configurations and apply settings as needed.参考步骤“A. 查看副本集配置并根据需要应用设置”中标识的副本集的原始配置文件输出。

10

Remove the temporary privileged user.删除临时权限用户。

For clusters enforcing authentication, remove the privileged user created earlier in this procedure:对于强制身份验证的集群,请删除此过程前面创建的权限用户:

  1. Authenticate as a user with the userAdmin role on the admin database or userAdminAnyDatabase role:使用admin数据库上的userAdmin角色或userAdminAnyDatabase角色作为用户进行身份验证:

    use admin
    db.auth("myUserAdmin","mySecurePassword")
  2. Delete the privileged user:删除权限用户:

    db.removeUser("mySystemUser")

E. Restart Each 重启每个mongos

Restart each mongos in the cluster.重新启动集群中的每个mongos

mongos --config /path/to/config/mongos.conf

Include all other command line options as required by your deployment.包括部署所需的所有其他命令行选项。

If the CSRS replica set name or any member hostname changed, update the mongos configuration file setting sharding.configDB with updated configuration server connection string:如果CSRS副本集名称或任何成员主机名更改,请使用更新的配置服务器连接字符串更新mongos配置文件设置sharding.configDB

sharding:
configDB: "myNewCSRSName/config1.example.net:27019,config2.example.net:27019,config3.example.net:27019"

F. Validate Cluster Accessibility验证群集可访问性

Connect a mongo shell to one of the mongos processes for the cluster. Use sh.status() to check the overall cluster status. 将mongo shell连接到集群的mongos进程之一。使用sh.status()检查整个集群状态。If sh.status() indicates that the balancer is not running, use sh.startBalancer() to restart the balancer. 如果sh.status()指示平衡器未运行,请使用sh.startBalancer()重新启动平衡器。[1]

To confirm that all shards are accessible and communicating, insert test data into a temporary sharded collection. Confirm that data is being split and migrated between each shard in your cluster. 为了确认所有分片都是可访问和可通信的,请将测试数据插入到临时分片集合中。确认数据正在集群中的每个分片之间进行分割和迁移。You can connect a mongo shell to each shard primary and use db.collection.find() to validate that the data was sharded as expected.您可以将mongo shell连接到每个主分片,并使用db.collection.find()验证数据是否按预期分片。

[1] Starting in MongoDB 6.0.3, automatic chunk splitting is not performed. This is because of balancing policy improvements. 从MongoDB 6.0.3开始,不执行自动块分割。这是因为平衡了政策的改进。Auto-splitting commands still exist, but do not perform an operation.In MongoDB versions earlier than 6.0.3, sh.startBalancer() also enables auto-splitting for the sharded cluster.自动拆分命令仍然存在,但不会执行任何操作。在早于6.0.3的MongoDB版本中,sh.startBalancer()还启用了分片集群的自动拆分。