Docs HomeMongoDB Manual

sh.addShard()

Definition定义

sh.addShard(<url>)

Adds a shard replica set to a sharded cluster. This method must be run on a mongos instance.将分片复制集添加到分片集群中。此方法必须在mongos实例上运行。

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the addShard command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

The sh.addShard() method has the following parameter:sh.addShard()方法具有以下参数:

Parameter参数Type类型Description描述
hoststringThe 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()方法具有以下原型形式:

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

Do not use localhost for the hostname unless your config server is also running on localhost.除非您的配置服务器也在localhost上运行,否则不要使用localhost作为主机名。

mongos installed from official .deb and .rpm packages have the bind_ip configuration set to 127.0.0.1 by default.默认情况下,从官方.deb.rpm包安装的mongosbind_ip配置设置为127.0.0.1

mongos uses "majority" for the addShard command and its helper sh.addShard().addShard命令及其助手sh.addShard()使用"majority"

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 Balancer Internals for more information.平衡器将开始迁移块,以便集群实现平衡。有关更多信息,请参阅平衡器内部

Hidden Members隐藏的成员

Important

You cannot include a hidden member in the seed list provided to sh.addShard().在提供给sh.addShard()的种子列表中不能包含隐藏成员

Example实例

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:以下示例添加了一个名为repl0的副本集,并指定了该副本集的一个成员:

sh.addShard("repl0/mongodb3.example.net:27327")