- Reference >
- Database Commands >
- Sharding Commands >
- addShard
addShard¶
On this page
Definition¶
-
addShard¶ Adds either a database instance or a replica set to a sharded cluster. The optimal configuration is to deploy shards across replica sets.
Run
addShardwhen connected to amongosinstance. The command takes the following form when adding a single database instance as a shard:When adding a replica set as a shard, use the following form:
The command contains the following fields:
Field Type Description addShardstring The hostname and port of the mongodinstance to be added as a shard. To add a replica set as a shard, specify the name of the replica set and the hostname and port of a member of the replica set.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.
Changed in version 2.6: Chunk migrations can have an impact on disk space. Starting in MongoDB 2.6, the source shard automatically archives the migrated documents by default. For details, see moveChunk directory.
Examples¶
The following command adds the database instance running on
port 27027 on the host mongodb0.example.net as a shard:
Warning
Do not use localhost for the hostname unless your
configuration server is also running on
localhost.
The following command adds a replica set as a shard:
You may specify all members in the replica set. All additional hostnames must be members of the same replica set.