Docs Menu
Docs Home
/
MongoDB Manual
/ / /

Convert Self-Managed Sharded Cluster to Replica Set

On this page

  • Before You Begin
  • Convert a Cluster with a Single Shard into a Replica Set
  • Convert a Sharded Cluster into a Replica Set

This tutorial describes the process for converting a sharded cluster to a non-sharded replica set. To convert a replica set into a sharded cluster Convert a Self-Managed Replica Set to a Sharded Cluster. See the Sharding documentation for more information on sharded clusters.

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.

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.

You may now decommission the remaining sharding infrastructure.

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 running, 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. Stop all writes to the sharded cluster. You may 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 on 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.

    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.

Back

Back Up Cluster Metadata