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.

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. Once you are done performing maintenance operations, stop using the directShardOperations role.

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.

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.
  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:

    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:

    [ { 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.

    Tip

    Changing the --shardsvr option will change the port that mongod listens for incoming connections on.

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. If you stop all mongos instances, the applications will not be able to read from the database. If you stop all mongos instances, start a temporary mongos instance that applications cannot access for the data migration procedure.
  3. Use mongodump and mongorestore to migrate the data from the mongos instance to the new replica set.

    Exclude the config database when you run mongorestore. Use the --nsExclude option as shown in this example:

    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.

    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:

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

Learn More了解更多

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