Database Manual / Self-Managed Deployments / Deploy and Manage Self-Managed Sharded Clusters / Administration

Convert Self-Managed Sharded Cluster to Replica Set将自管理分片群集转换为副本集

This tutorial describes how to convert a sharded cluster to a non-sharded replica set. 本教程介绍如何将分片集群转换为非分片副本集To convert a replica set into a sharded cluster, see Convert a Self-Managed Replica Set to a Sharded Cluster. 要将副本集转换为分片群集,请参阅将自管理副本集转换成分片群集See the Sharding documentation for more information on sharded clusters.有关分片集群的更多信息,请参阅分片文档。

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. Only use the directShardOperations role for maintenance purposes or under the guidance of MongoDB support.使用directShardOperations角色运行命令可能会导致集群停止正常工作,并可能导致数据损坏。仅将directShardOperations角色用于维护目的或在MongoDB支持的指导下使用。 Once you are done performing maintenance operations, stop using the directShardOperations role.完成维护操作后,停止使用directShardOperations角色。

Version Compatibility版本兼容性

The steps in this tutorial require MongoDB 6.0 or later.本教程中的步骤需要MongoDB 6.0或更高版本。

Authorization授权

The fsync and fsyncUnlock commands require the fsync authorization action, which can be assigned through the hostManager role or a custom role.fsyncfsyncUnlock命令需要fsync授权操作,该操作可以通过hostManager角色或自定义角色分配。

Schedule the Cluster Conversion安排集群转换

Convert the cluster when chunk migrations, resharding, and schema transformations aren't typically performed.在通常不执行块迁移、重新分级和模式转换时转换集群。

Disable the Balancer and Lock the Cluster禁用平衡器并锁定集群

To disable the balancer and lock the cluster:要禁用平衡器并锁定群集,请执行以下操作:

  1. Connect mongosh to a mongos instance in the sharded cluster.mongosh连接到分片集群中的mongos实例。
  2. To stop the balancer, run:要停止平衡器,请运行:

    sh.stopBalancer()
  3. To verify the balancer is disabled, run the following command and ensure the output is false:要验证平衡器是否已禁用,请运行以下命令并确保输出为false

    sh.getBalancerState()
  4. To lock the sharded cluster, which prevents database writes, run:要锁定分片集群以防止数据库写入,请运行:

    db.getSiblingDB( "admin" ).fsyncLock()
  5. To confirm the lock, run:要确认锁定,请运行:

    db.getSiblingDB( "admin" ).aggregate( [
    { $currentOp: { } },
    { $facet: {
    "locked": [
    { $match: { $and: [
    { fsyncLock: { $exists: true } }
    ] } }
    ],
    "unlocked": [
    { $match: { fsyncLock: { $exists: false } } }
    ]
    } },
    { $project: {
    "fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
    "fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
    } }
    ] )
  6. Ensure the output shows fsyncLocked is true, which means the cluster is locked:确保输出显示fsyncLockedtrue,这意味着集群已锁定:

    [ { fsyncLocked: true }, { fsyncUnlocked: false } ]

Convert a Cluster with a Single Shard into a Replica Set将具有单个分片的群集转换为副本集

In the case of a sharded cluster with only one shard, that shard contains the full data set. Use the following procedure to convert that cluster into a non-sharded replica set:对于只有一个分片的分片集群,该分片包含完整的数据集。使用以下过程将该群集转换为非分片副本集

  1. Reconfigure the application to connect to the primary member of the replica set hosting the single shard that system will be the new replica set.重新配置应用程序以连接到承载单个分片的副本集的主要成员,该系统将成为新的副本集。
  2. Remove the --shardsvr option from your mongod.mongod中删除--shardsvr选项。

    Tip

    Changing the --shardsvr option will change the port that mongod listens for incoming connections on.更改--shardsvr选项将更改mongod监听传入连接的端口。

The single-shard cluster is now a non-sharded replica set that will accept read and write operations on the data set.单分片集群现在是一个非分片副本集,将接受对数据集的读写操作。

Convert a Sharded Cluster into a Replica Set将分片群集转换为副本集

Use the following procedure to transition from a sharded cluster with more than one shard to an entirely new replica set.使用以下过程从具有多个分片的分片集群转换到一个全新的副本集

  1. With the sharded cluster locked and the balancer disabled, deploy a new replica set in addition to your sharded cluster. 在分片集群锁定且平衡器禁用的情况下,除了分片集群外,还部署一个新的副本集The replica set must have sufficient capacity to hold all of the data files from all of the current shards combined. Do not configure the application to connect to the new replica set until the data transfer is complete.副本集必须具有足够的容量来保存来自所有当前分片的所有数据文件。在数据传输完成之前,不要将应用程序配置为连接到新的副本集。
  2. Reconfigure your application or stop all mongos instances. 重新配置应用程序或停止所有mongos实例。If you stop all mongos instances, the applications will not be able to read from the database. 如果停止所有mongos实例,应用程序将无法从数据库中读取。If you stop all mongos instances, start a temporary mongos instance that applications cannot access for the data migration procedure.如果停止所有mongos实例,请启动一个应用程序无法访问的临时mongos示例,以便进行数据迁移过程。
  3. Use mongodump and mongorestore to migrate the data from the mongos instance to the new replica set.使用mongodumpmongorestore将数据从mongos实例迁移到新的副本集

    Exclude the config database when you run mongorestore. Use the --nsExclude option as shown in this example:运行mongorestore时排除config数据库。使用--nsExclude选项,如下例所示:

    mongorestore --nsExclude="config.*" <connection-string> /data/backup

    Note

    Not all collections on all databases are necessarily sharded. Do not solely migrate the sharded collections. Ensure that all databases and all collections migrate correctly.并非所有数据库上的所有集合都必须分片。不要只迁移分片集合。确保所有数据库和所有集合都正确迁移。

  4. Reconfigure the application to use the non-sharded replica set instead of the mongos instance.重新配置应用程序以使用非分片副本集而不是mongos实例。

    After you convert the sharded cluster to a replica set, update the connection string used by your applications to the connection string for your replica set. Then, restart your applications.将分片集群转换为副本集后,将应用程序使用的连接字符串更新为副本集的连接字符串。然后,重新启动应用程序。

The application will now use the un-sharded replica set for reads and writes. You may now decommission the remaining unused sharded cluster infrastructure.应用程序现在将使用非分片副本集进行读取和写入。现在,您可以停用剩余的未使用的分片集群基础架构。

Next Steps后续步骤

After you convert the sharded cluster to a replica set, perform the following steps to unlock the cluster:将分片集群转换为副本集后,执行以下步骤解锁集群:

  1. To unlock the cluster and allow database writes to resume, run:要解锁群集并允许恢复数据库写入,请运行:

    db.getSiblingDB( "admin" ).fsyncLock()
  2. To confirm the unlock, run:要确认解锁,请运行:

    db.getSiblingDB("admin").aggregate( [
    { $currentOp: { } },
    { $facet: {
    "locked": [
    { $match: { $and: [
    { fsyncLock: { $exists: true } }
    ] } } ],
    "unlocked": [
    { $match: { fsyncLock: { $exists: false } } }
    ]
    } },
    { $project: {
    "fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
    "fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
    } }
    ] )
  3. Ensure the output shows fsyncLocked is false, which means the cluster is unlocked:确保输出显示fsyncLockedfalse,这意味着集群已解锁:

    [ { fsyncLocked: false }, { fsyncUnlocked: true } ]

Learn More了解更多

Manage Sharded Cluster Balancer管理分片集群平衡器