Add Shards to a Cluster

You add shards to a sharded cluster after you create the clusteror any time that you need to add capacity to the cluster. If you have notcreated a sharded cluster, see Deploy a Sharded Cluster.

In production environments, all shards should be replica sets.

Considerations

Balancing

When you add a shard to a sharded cluster, you affect the balance ofchunks among the shards of a cluster for all existing shardedcollections. The balancer will begin migrating chunks so that thecluster will achieve balance. See Cluster Balancer formore information.

Changed in version 2.6: Chunk migrations can have an impact on disk space. Starting inMongoDB 2.6, the source shard automatically archives the migrateddocuments by default. For details, see moveChunk directory.

Capacity Planning

When adding a shard to a cluster, always ensure that the cluster hasenough capacity to support the migration required for balancing thecluster without affecting legitimate production traffic.

Add a Shard to a Cluster

You interact with a sharded cluster by connecting to a mongosinstance.

  • From a mongo shell, connect to the mongosinstance. For example, if a mongos is accessible atmongos0.example.net on port 27017, issue the followingcommand:
  1. mongo --host mongos0.example.net --port 27017
  • Add a shard to the cluster using the sh.addShard()method, as shown in the examples below. Issue sh.addShard()separately for each shard. If the shard is a replica set, specify thename of the replica set and specify a member of the set. Inproduction deployments, all shards should be replica sets.

Optional

You can instead use the addShard databasecommand, which lets you specify a name and maximum size for theshard. If you do not specify these, MongoDB automatically assignsa name and maximum size. To use the database command, seeaddShard.

The following are examples of adding a shard withsh.addShard():

  • To add a replica set shard named rs1 with a memberrunning on port 27018 on mongodb0.example.net, issue thefollowing command:
  1. sh.addShard( "rs1/mongodb0.example.net:27018" )
  • To add a standalone mongod shard running on port27018 of mongodb0.example.net, issue the following command:
  1. sh.addShard( "mongodb0.example.net:27018" )

Note

It might take some time for chunks tomigrate to the new shard.