Add Shards to a Cluster将分片添加到群集

On this page本页内容

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 Sharded Cluster.如果尚未创建分片群集,请参阅部署分片群集

In production environments, all shards should 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 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.有关详细信息,请参阅moveChunk目录

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.将分片添加到集群时,请始终确保集群有足够的容量支持平衡集群所需的迁移,而不影响合法的生产流量。

Add a Shard to a Cluster将分片添加到群集

You interact with a sharded cluster by connecting to a mongos instance.通过连接到mongos实例,您可以与分片集群进行交互。

  1. In mongosh, connect to the mongos instance. mongosh中,连接到mongos实例。For example, if a mongos is accessible at mongos0.example.net on port 27017, issue the following command:例如,如果在端口27017的mongos0.example.net上可以访问mongos,请发出以下命令:

    mongosh --host mongos0.example.net --port 27017
  2. Add a shard to the cluster using the sh.addShard() method, as shown in the examples below. 使用sh.addShard()方法将分片添加到群集中,如下面的示例所示。Issue sh.addShard() separately for each shard. 分别为每个分片发出<sh.addShard()If the shard is a replica set, specify the name of the replica set and specify a member of the set. 如果分片是副本集,请指定副本集的名称并指定该集的成员。In production deployments, all shards should be replica sets.在生产部署中,所有分片都应该是副本集。

    Note注意
    Optional可选

    You can instead use the addShard database command, which lets you specify a name and maximum size for the shard. 您可以改为使用addShard数据库命令,该命令允许您指定分片的名称和最大大小。If you do not specify these, MongoDB automatically assigns a name and maximum size. 如果未指定这些,MongoDB会自动分配名称和最大大小。To use the database command, see addShard.要使用数据库命令,请参阅addShard

    The following are examples of adding a shard with sh.addShard():以下是使用sh.addShard()添加分片的示例:

    • To add a replica set shard named rs1 with a member running on port 27018 on mongodb0.example.net, issue the following command:要添加名为rs1的副本集分片,其中成员运行在mongodb0.example.net的端口27018上,请发出以下命令:

      sh.addShard( "rs1/mongodb0.example.net:27018" )
    • To add a standalone mongod shard running on port 27018 of mongodb0.example.net, issue the following command:要添加在mongodb0.example.net的端口27018上运行的独立mongod分片,请发出以下命令:

      sh.addShard( "mongodb0.example.net:27018" )
    Note注意

    It might take some time for chunks to migrate to the new shard.迁移到新分片可能需要一些时间。

←  Migrate a Sharded Cluster to Different Hardware将分片群集迁移到不同的硬件Remove Shards from an Existing Sharded Cluster从现有分片群集中删除分片 →