Convert Sharded Cluster to Replica Set

This tutorial describes the process for converting a shardedcluster to a non-sharded replica set. To convert a replicaset into a sharded clusterConvert a Replica Set to a Sharded Cluster. See theSharding documentation for more information on shardedclusters.

Convert a Cluster with a Single Shard into a Replica Set

In the case of a sharded cluster with only one shard, that shardcontains the full data set. Use the following procedure to convert thatcluster into a non-sharded replica set:

  • Reconfigure the application to connect to the primary member of thereplica set hosting the single shard that system will be the new replicaset.

  • Remove the —shardsvroption from your mongod.

Tip

Changing the —shardsvroption will change the port that mongod listens forincoming connections on.

The single-shard cluster is now a non-sharded replica set thatwill accept read and write operations on the data set.

You may now decommission the remaining sharding infrastructure.

Convert a Sharded Cluster into a Replica Set

Use the following procedure to transition from a sharded clusterwith more than one shard to an entirely new replica set.

  • With the sharded cluster running, deploy a new replicaset in addition to your sharded cluster. Thereplica set must have sufficient capacity to hold all of the datafiles from all of the current shards combined. Do not configure theapplication to connect to the new replica set until the datatransfer is complete.

  • Stop all writes to the sharded cluster. You may reconfigureyour application or stop all mongos instances. If youstop all mongos instances, the applications will not beable to read from the database. If you stop all mongosinstances, start a temporary mongos instance on thatapplications cannot access for the data migration procedure.

  • Use mongodump and mongorestore to migratethe data from the mongos instance to the newreplica set.

Note

Not all collections on all databases are necessarilysharded. Do not solely migrate the sharded collections. Ensure thatall databases and all collections migrate correctly.

  • Reconfigure the application to use the non-sharded replicaset instead of the mongos instance.

The application will now use the un-sharded replica set forreads and writes. You may now decommission the remaining unusedsharded cluster infrastructure.