Navigation

Convert a Shard Standalone to a Shard Replica Set

Starting in MongoDB 3.6, all shards must be replica sets. Before you can upgrade your 3.4 sharded cluster to version 3.6, you must convert any shards that are running as standalone mongod instances to replica set shards.

This tutorial describes the process for converting a shard standalone into a shard replica set. The procedure is specific to a shard standalone. To convert just a standalone to a replica set (i.e. not a part of any sharded cluster), see Convert a Standalone to a Replica Set instead.

Procedure

Important

The following procedure converts a standalone shard to a single-member replica set shard. The procedure assumes that the single member runs on the same host and port as before.

  1. Shut down the shard standalone mongod instance.

  2. Restart the shard instance with the --replSet option to specify the name of the new replica set. Ensure that the name is distinct (for instance, you could use the shard name as the replica set name); in particular, shard replica sets must not use the same name as the config server replica set.

    The other options can remain the same.

    For example, the following command starts a standalone instance as a member of a new replica set named shardA. The other options stay the same as before; e.g. --dbpath uses the standalone’s existing database path of /srv/mongodb/db0 and --port is the same as before:

    mongod --port 27018 --dbpath /srv/mongodb/db0 --shardsvr --replSet shardA --bind_ip localhost,<ip address of the mongod host>
    

    For more information on configuration options, see Configuration File Options and the mongod manual page.

  3. Connect a mongo shell to the shard mongod instance.

  4. Use rs.initiate() to initiate the new replica set:

    rs.initiate()
    

    The replica set is now operational. To view the replica set configuration, use rs.conf(). To check the status of the replica set, use rs.status().

  5. Disconnect from the instance.

  6. Connect a mongo shell to one of the sharded cluster’s mongos instances and retrieve the shard information:

    var myShard = db.getSiblingDB("config").shards.findOne( { _id: "<name>"} )
    

    Replace <name> with the name of the shard. The <name> of the shard is separate from the shard replica set name (unless you are using the shard name as the replica set name). To retrieve the name of the shard, see the shards section in the results from the sh.status() method. For example, if the result of sh.status() includes the following shards section, the name of the two shards are "shard0000" and "shard0001" respectively:

    shards:
          {  "_id" : "shard0000",  "host" : "mongodb1.example.net:27018",  "state" : 1 }
          {  "_id" : "shard0001",  "host" : "mongodb2.example.net:27018",  "state" : 1 }
    
  7. Update the host information with the replica set information:

    myShard.host = "<replica-set>/<member>"
    

    Replace <replica-set> with the name of the replica set. Replace <member> with the replica set member. For example shardA/mongodb1.example.net:27018.

  8. Save the information.

    db.getSiblingDB("config").shards.save(myShard, { writeConcern: { w: "majority" } } )
    
  9. Repeat for the next standalone shard in the sharded cluster. Ensure that you use a distinct name for each shard replica set.

  10. Once you have finished converting shard standalone instances to shard replica sets, force the members of sharded cluster to update their knowledge of other shards’ connection strings by restarting all members of the sharded cluster:

    • config server replica sets
    • mongos instances
    • shard replica sets

Additional Information

To add members to this replica set, use the rs.add() method. For more information on adding members to a replica set, see Add Members to a Replica Set.

To convert a non-shard standalone to a non-shard replica set, see Convert a Standalone to a Replica Set instead.