- 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
addShard
when connected to amongos
instance. The command takes the following form when adding a single database instance as a shard:{ addShard: "<hostname><:port>", maxSize: <size>, name: "<shard_name>" }
When adding a replica set as a shard, use the following form:
{ addShard: "<replica_set>/<hostname><:port>", maxSize: <size>, name: "<shard_name>" }
The command contains the following fields:
Field Type Description addShard
string The hostname and port of the mongod
instance 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.maxSize
integer Optional. The maximum size in megabytes of the shard. If you set maxSize
to0
, MongoDB does not limit the size of the shard.name
string Optional. A name for the shard. If this is not specified, MongoDB automatically provides a unique name. The
addShard
command stores shard configuration information in the config database. Always runaddShard
when using theadmin
database.Specify a
maxSize
when you have machines with different disk capacities, or if you want to limit the amount of data on some shards. ThemaxSize
constraint prevents the balancer from migrating chunks to the shard when the value ofmem.mapped
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:
db.adminCommand({addShard: "mongodb0.example.net:27027"})
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:
use admin
db.runCommand( { addShard: "repl0/mongodb3.example.net:27327"} )
You may specify all members in the replica set. All additional hostnames must be members of the same replica set.