- Reference >
- Database Commands >
- Sharding Commands >
- addShard
addShard¶
On this page
Definition¶
-
addShard¶ Adds a shard replica set to a sharded cluster.
Run
addShardwhen connected to amongosinstance. The command takes the following form to add a shard replica set:The command contains the following fields:
Field Type Description addShardstring The replica set name, hostname, and port of at least one member of the shard’s replica set. Any additional replica set member hostnames must be comma separated. For example:
maxSizeinteger Optional. The maximum size in megabytes of the shard. If you set maxSizeto0, MongoDB does not limit the size of the shard.namestring Optional. A name for the shard. If this is not specified, MongoDB automatically provides a unique name. The
addShardcommand stores shard configuration information in the config database. Always runaddShardwhen using theadmindatabase.Specify a
maxSizewhen you have machines with different disk capacities, or if you want to limit the amount of data on some shards. ThemaxSizeconstraint prevents the balancer from migrating chunks to the shard when thetotalSizereturned from runninglistDatabaseson the shard exceeds the value ofmaxSize.
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 Cluster Balancer for more information.
Chunk migrations can have an impact on disk space, as the source shard automatically archives the migrated documents by default. For details, see moveChunk directory.
Examples¶
The following command adds a replica set as a shard:
Warning
Do not use localhost for the hostname unless your
config server is also running on
localhost.