addShard

Definition

Run addShard when connected to a mongosinstance. The command takes the following form to add a shardreplica set:

  1. { addShard: "<replica_set>/<hostname><:port>", maxSize: <size>, name: "<shard_name>" }

The command contains the following fields:

FieldTypeDescriptionaddShardstringThe 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:

  1. <replica_set>/<hostname><:port>,<hostname><:port>, ...

maxSizeintegerOptional. The maximum size in megabytes of the shard. If you set maxSizeto 0, MongoDB does not limit the size of the shard.namestringOptional. 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:

  1. use admin
  2. db.runCommand( { addShard: "repl0/mongodb3.example.net:27327"} )

Warning

Do not use localhost for the hostname unless yourconfig server is also running onlocalhost.