You add shards to a sharded cluster after you create the cluster or any time that you need to add capacity to the cluster. 您可以在创建集群后或需要向集群添加容量时向分片集群添加分片。If you have not created a sharded cluster, see Deploy a Self-Managed Sharded Cluster.如果您尚未创建分片集群,请参阅部署自我管理的分片集群。
All shards must be replica sets.所有分片都必须是副本集。
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.有关更多信息,请参阅平衡器内部。
Capacity Planning容量规划
When adding a shard to a cluster, always ensure that the cluster has enough capacity to support the migration required for balancing the cluster without affecting legitimate production traffic.在向集群添加分片时,始终确保集群有足够的容量来支持平衡集群所需的迁移,而不会影响合法的生产流量。
DDL OperationsDDL操作
If you add a shard while your cluster executes a DDL operation (operation that modifies a collection such as 如果在集群执行DDL操作(修改reshardCollection), the operation that adds a shard only executes after the concurrent DDL operation finishes.reshardCollection等集合的操作)时添加分片,则添加分片的操作仅在并发DDL操作完成后执行。
Add a Shard to a Cluster将分片添加到集群
You interact with a sharded cluster by connecting to a 通过连接到mongos instance.mongos实例,您可以与分片集群进行交互。
In在mongosh, connect to themongosinstance.mongosh中,连接到mongos实例。For example, if a例如,如果mongosis accessible atmongos0.example.neton port27017, issue the following command:mongos可以在端口27017的mongos0.example.net上访问,请发出以下命令:mongosh --host mongos0.example.net --port 27017Add a shard replica set to the cluster using the使用sh.addShard()method, as shown in the example below.sh.addShard()方法将分片副本集添加到集群中,如下例所示。Issue为每个分片分别发出sh.addShard()separately for each shard. Specify the name of the replica set and a member of the set.sh.addShard()。指定副本集的名称和该集的一个成员。Note
Optional可选的You can instead use the您可以使用addSharddatabase command, which lets you specify a name and maximum size for the shard. If you do not specify these, MongoDB automatically assigns a name and maximum size. To use the database command, seeaddShard.addShard数据库命令,该命令允许您指定分片的名称和最大大小。如果你不指定这些,MongoDB会自动分配一个名称和最大大小。要使用数据库命令,请参阅addShard。The following example illustrates adding a shard with以下示例说明了如何使用sh.addShard():sh.addShard()添加分片:To add a shard replica set named要添加一个名为rs1with a member running on port27018onmongodb0.example.net, issue the following command:rs1的分片副本集,其成员在mongodb0.example.net的27018端口上运行,请发出以下命令:sh.addShard( "rs1/mongodb0.example.net:27018" )