addShard
Definition
addShard
- Adds a shard replica set to a sharded cluster.
Run addShard
when connected to a mongos
instance. The command takes the following form to add a shardreplica set:
- { addShard: "<replica_set>/<hostname><:port>", maxSize: <size>, name: "<shard_name>" }
The command contains the following fields:
FieldTypeDescriptionaddShard
stringThe replica set name, hostname, and port of at least one member ofthe shard’s replica set. Any additional replica set member hostnamesmust be comma separated. For example:
- <replica_set>/<hostname><:port>,<hostname><:port>, ...
maxSize
integerOptional. The maximum size in megabytes of the shard. If you set maxSize
to 0
, MongoDB does not limit the size of the shard.name
stringOptional. A name for the shard. If this is not specified, MongoDBautomatically provides a unique name.
The addShard
command stores shard configurationinformation in the config database. Always runaddShard
when using the admin
database.
Specify a maxSize
when you have machines with different diskcapacities, or if you want to limit the amount of data on someshards. The maxSize
constraint prevents the balancerfrom migrating chunks to the shard when the totalSize
returnedfrom running listDatabases
on the shard exceeds thevalue of maxSize
.
Considerations
Balancing
When you add a shard to a sharded cluster, you affect the balance ofchunks among the shards of a cluster for all existing shardedcollections. The balancer will begin migrating chunks so that thecluster will achieve balance. See Cluster Balancer formore information.
Changed in version 2.6: Chunk migrations can have an impact on disk space. Starting inMongoDB 2.6, the source shard automatically archives the migrateddocuments by default. For details, see moveChunk directory.
Hidden Members
Important
You cannot include a hidden member in the seed list provided toaddShard
.
Examples
The following command adds a replica set as a shard:
- use admin
- db.runCommand( { addShard: "repl0/mongodb3.example.net:27327"} )
Warning
Do not use localhost
for the hostname unless yourconfig server is also running onlocalhost
.