定义
addShard将分片副本集添加到分片集群。
提示
In
mongosh, this command can also be run through thesh.addShard()helper method.Helper methods are convenient for
mongoshusers, 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 Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
注意
MongoDB Atlas不支持此命令。请参阅修改Atlas分片集群以在Atlas 集群中添加或删除分片。
语法
该命令具有以下语法:
db.adminCommand( { addShard: "<replica_set>/<hostname><:port>", name: "<shard_name>" } )
命令字段
注意
从版本6.2 开始, MongoDB删除了 命令中的maxSize addShard字段。因此:
该命令包含以下字段:
字段 | 类型 | 说明 | |
|---|---|---|---|
| 字符串 | 分片副本集至少一个成员的副本集名称、主机名和端口。任何其他副本集节点主机名都必须以逗号分隔。 例如: | |
| 字符串 | 可选。 分片的名称。 如果未指定,MongoDB 会自动提供一个唯一名称。 |
The addShard command stores shard configuration information in the config database. Always run addShard when using the admin database.
Considerations
存储
添加到分片集群的分片必须为空。
均衡
将分片添加到分片集群时,会影响所有现有分片集合的集群分片之间的数据段平衡。负载均衡器将开始迁移数据段,以便集群实现平衡。有关更多信息,请参阅负载均衡器内部结构。
隐藏成员
重要
You cannot include a hidden member in the seed list provided to addShard.
必需的访问权限
您必须对集群资源执行 addShard 动作才能运行addShard 命令。clusterAdmin 或 clusterManager 内置角色 包含 addShard 动作并授予运行 addShard 命令的特权。
示例
以下命令将副本集添加为分片:
use admin db.runCommand( { addShard: "repl0/mongodb3.example.net:27327"} )
警告
请勿使用localhost作为主机名,除非您的配置服务器也在localhost上运行。