对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

addShard(数据库命令)

addShard

将分片副本集添加到分片集群。

提示

In mongosh, this command can also be run through the sh.addShard() helper method.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

此命令可用于以下环境中托管的部署:

注意

MongoDB Atlas不支持此命令。请参阅修改Atlas分片集群以在Atlas 集群中添加或删除分片。

该命令具有以下语法:

db.adminCommand(
{
addShard: "<replica_set>/<hostname><:port>",
name: "<shard_name>"
}
)

注意

从版本6.2 开始, MongoDB删除了 命令中的maxSize addShard字段。因此:

  • Running addShard with the maxSize field returns an InvalidOptions error.

  • shards 集合中的新文件不再包含 maxSize 字段。

  • 任何预先存在的 maxSize 字段条目都将被忽略。

该命令包含以下字段:

字段
类型
说明

addShard

字符串

分片副本集至少一个成员的副本集名称、主机名和端口。任何其他副本集节点主机名都必须以逗号分隔。 例如:

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

name

字符串

可选。 分片的名称。 如果未指定,MongoDB 会自动提供一个唯一名称。

The addShard command stores shard configuration information in the config database. Always run addShard when using the admin database.

添加到分片集群的分片必须为空。

将分片添加到分片集群时,会影响所有现有分片集合的集群分片之间的数据段平衡。负载均衡器将开始迁移数据段,以便集群实现平衡。有关更多信息,请参阅负载均衡器内部结构

重要

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

您必须对集群资源执行 addShard 动作才能运行addShard 命令。clusterAdminclusterManager 内置角色 包含 addShard 动作并授予运行 addShard 命令的特权。

以下命令将副本集添加为分片:

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

警告

请勿使用localhost作为主机名,除非您的配置服务器也在localhost上运行。