Docs Menu
Docs Home
/ /
/ / /

Convert Replica Set to an Embedded Config Shard

Starting in MongoDB 8.0, you can configure a config server to store your application data in addition to the usual sharded cluster metadata. A config server that stores application data is called a config shard or embedded config server.

Converting your replica set into a sharded cluster with an embedded config shard can reduce:

  • The number of nodes required in your deployment.

  • Complexity for maintaining one-shard clusters.

Starting in MongoDB 8.3, you can convert a replica set directly into a sharded cluster with an embedded config shard.

Previous releases require you to first convert the replica set into a sharded cluster with a dedicated config server replica set, then convert that into one with an embedded config shard.

If access control is enabled, the transitionFromDedicatedConfigServer command requires the transitionFromDedicatedConfigServer authorization action for the cluster.

The clusterManager role has the transitionFromDedicatedConfigServer authorization action and can be assigned to a user.

Starting in MongoDB 8.3, replica sets that were previously sharded clusters cannot be converted back into replica sets.

The conversion of a sharded cluster into a replica set preserves sharding metadata from its prior deployment, including a shard identity document, which blocks it from again becoming a sharded cluster. If you attempt a self-managed conversion back into a sharded cluster, MongoDB returns an error.

To convert such replica sets into sharded clusters, contact Technical Support.

The following example converts a self-managed replica set to a config shard that contains a pre-existing user data from the replica set.

1

Perform a rolling restart on the replica set. Start each node as a config server in maintenance mode:

  1. Stop each node, beginning with the secondaries. To shut down a server, use the db.shutdownServer() method.

    db.shutdownServer()
  2. Restart the node as a config shard in maintenance mode.

    mongod --config /etc/mongodb.conf --configsvr \
    --replicaSetConfigShardMaintenanceMode \
    --configsvr
    Starts the mongod instance as a config server.
    --replicaSetConfigShardMaintenanceMode
    Enables the config shard maintenance mode, which disables some startup checks and allows you to convert a server into an embedded config shard.
  3. Wait for the node to rejoin the replica set before restarting the next.

2

Use the rs.status() method to identify the new primary node. If you have a large replica set, use the db.aggregate() method to narrow your search.

db.aggregate( [
{ $documents: rs.status().members },
{ $match: { stateStr: "PRIMARY" } },
{ $project: { _id: 1, name: 1 }
] )
[ { _id: 3, name: "192.0.2.3:27017" } ]
3

Connect to the primary node and reconfigure it to operate as an embedded config shard:

  1. Use the rs.conf() method to get the current configuration and store it in a variable:

    var conf = rs.conf()
  2. Set the configsvr field:

    conf.configsvr = true
  3. Increment the version field:

    conf.version += 1
  4. Reconfigure the node:

    rs.reconfig(conf)
  5. Wait for secondaries to replicate the change. You can check the status by running an aggregation pipeline on the member documents:

    db.aggregate( [
    { $documents: rs.status().members },
    { $group: {
    _id: null,
    allConfigSvr: {
    $min: { $eq: ["$configsvr", true] }
    }
    } }
    ] )
    { _id: null, allConfigSvr: true }

    When allConfigSvr shows true, it indicates that the reconfiguation has propagated to all nodes in the cluster.

4

Perform a rolling upgrade to restart the replica set as a config server:

  1. Stop each node, beginning with the secondaries. To shut down a server, use the db.shutdownServer() method.

    db.shutdownServer()
  2. Restart the node as a config server, no longer in maintenance mode:

    mongod --config /etc/mongodb.conf --configsvr
5

Start the mongos router:

mongos --config /etc/mongodb.conf
6

Connect to mongos and run the transitionFromDedicatedConfigServer command to add the config shard to the cluster.

db.adminCommand( { transitionFromDedidcatedConfigServer: 1 } )
7

Update your application to connect to the mongos router instead of the replica set members.

Back

Config Shard

On this page