- Sharding >
- Sharded Cluster Tutorials >
- Sharded Cluster Deployment Tutorials >
- Add Shards to a Cluster
Add Shards to a Cluster¶
On this page
You add shards to a sharded cluster after you create the cluster or anytime that you need to add capacity to the cluster. If you have not created a sharded cluster, see Deploy a Sharded Cluster.
When adding a shard to a cluster, you should always ensure that the cluster has enough capacity to support the migration without affecting legitimate production traffic.
In production environments, all shards should be replica sets.
Add a Shard to a Cluster¶
You interact with a sharded cluster by connecting to a mongos
instance.
From a
mongoshell, connect to themongosinstance. For example, if amongosis accessible atmongos0.example.neton port27017, issue the following command: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.Optional
You can instead use the
addSharddatabase command, which lets you specify a name and maximum size for the shard. If you do not specify these, MongoDB automatically assigns a name and maximum size. To use the database command, seeaddShard.The following are examples of adding a shard with
sh.addShard():To add a shard for a replica set named
rs1with a member running on port27017onmongodb0.example.net, issue the following command:Changed in version 2.0.3.
For MongoDB versions prior to 2.0.3, you must specify all members of the replica set. For example:
To add a shard for a standalone
mongodon port27017ofmongodb0.example.net, issue the following command:
Note
It might take some time for chunks to migrate to the new shard.