Add Shards to a Cluster
On this page
You add shards to a sharded cluster after you create the cluster or any time that you need to add capacity to the cluster. If you have not created 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 of chunks among the shards of a cluster for all existing sharded collections. The balancer will begin migrating chunks so that the cluster will achieve balance. See Balancer Internals for more information.
Capacity Planning
When adding a shard to a cluster, always ensure that the cluster has enough capacity to support the migration required for balancing the cluster without affecting legitimate production traffic.
Add a Shard to a Cluster
You interact with a sharded cluster by connecting to a mongos instance.
-
In
mongosh, connect to themongosinstance. For example, if amongosis accessible atmongos0.example.neton port27017, issue the following command:mongosh --host mongos0.example.net --port 27017
-
Add a shard to the cluster using the
sh.addShard()method, as shown in the examples below. Issuesh.addShard()separately for each shard. If the shard is a replica set, specify the name of the replica set and specify a member of the set. In production deployments, all shards should be replica sets.Note
The following are examples of adding a shard with
sh.addShard():-
To add a replica set shard named
rs1with a member running on port27018onmongodb0.example.net, issue the following command:sh.addShard( "rs1/mongodb0.example.net:27018" )
-
To add a standalone
mongodshard running on port27018ofmongodb0.example.net, issue the following command:sh.addShard( "mongodb0.example.net:27018" )
Note
It might take some time for chunks to migrate to the new shard.
-