Convert a Shard Standalone to a Shard Replica Set

Starting in MongoDB 3.6, all shards must be replica sets. Before youcan upgrade your 3.4 sharded cluster to version 3.6, you must convertany shards that are running as standalone mongodinstances to replica set shards.

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

Procedure

Important

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

  • Shut down the shard standalonemongod instance.

  • Restart the shard instance with the —replSet option to specify the name of the new replica set. Ensurethat the name is distinct (for instance, you could use the shardname as the replica set name); in particular,shard replica sets must not use the same name as the config serverreplica set.

The other options can remain the same.

For example, the following command starts a standalone instance as amember of a new replica set named shardA. The other optionsstay the same as before; e.g. —dbpathuses the standalone’s existing database path of /srv/mongodb/db0and —port is the same as before:

  1. 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, seeConfiguration File Options and the mongodmanual page.

  1. rs.initiate()

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

  • Disconnect from the instance.

  • Connect a mongo shell to one of the sharded cluster’smongos instances and retrieve the shard information:

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

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

  1. shards:
  2. { "_id" : "shard0000", "host" : "mongodb1.example.net:27018", "state" : 1 }
  3. { "_id" : "shard0001", "host" : "mongodb2.example.net:27018", "state" : 1 }
  • Update the host information with the replica set information:
  1. myShard.host = "<replica-set>/<member>"

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

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

  • Once you have finished converting shard standalone instances toshard replica sets, force the members of sharded cluster to updatetheir knowledge of other shards’ connection strings by restarting allmembers 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, seeAdd Members to a Replica Set.

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