Docs Menu

sh.addShard()

On this page

  • Definition
  • Considerations
  • Example
sh.addShard(<url>)

Adds a shard replica set to a sharded cluster. This method must be run on a mongos instance.

The sh.addShard() method has the following parameter:

Parameter
Type
Description
host
string

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:

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

The sh.addShard() method has the following prototype form:

sh.addShard("<replica_set>/<hostname><:port>")
Warning

Do not use localhost for the hostname unless your config server is also running on localhost.

mongos installed from official .deb and .rpm packages have the bind_ip configuration set to 127.0.0.1 by default.

mongos uses "majority" for the addShard command and its helper sh.addShard().

The sh.addShard() method is a helper for the addShard command. The addShard command has additional options which are not available with this helper.

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.

Important

You cannot include a hidden member in the seed list provided to sh.addShard().

To add a shard, specify the name of the replica set and the hostname of at least one member of the replica set, as a seed. If you specify additional hostnames, all must be members of the same replica set.

The following example adds a replica set named repl0 and specifies one member of the replica set:

sh.addShard("repl0/mongodb3.example.net:27327")
Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.