Docs HomeMongoDB Manual

Manage Sharded Cluster Balancer管理分片化群集平衡器

Changed in version 6.16.1版更改.

This page describes common administrative procedures related to balancing. 本页介绍与平衡相关的常见管理程序。For an introduction to balancing, see Sharded Cluster Balancer. 有关平衡的介绍,请参阅分片集群平衡器For lower level information on balancing, see Balancer Internals.有关平衡的较低级别信息,请参阅平衡器内部

The balancer process has moved from the mongos instances to the primary member of the config server replica set.平衡器进程已从mongos实例移动到配置服务器副本集的主要成员。

Check the Balancer State检查平衡器状态

sh.getBalancerState() checks if the balancer is enabled (i.e. that the balancer is permitted to run). sh.getBalancerState() does not check if the balancer is actively migrating data.sh.getBalancerState()检查平衡器是否已启用(即是否允许平衡器运行)。sh.getBalancerState()不检查平衡器是否正在主动迁移数据。

To see if the balancer is enabled in your sharded cluster, issue the following command, which returns a boolean:要查看您的分片集群中是否启用了平衡器,请发出以下命令,该命令返回一个布尔值:

sh.getBalancerState()

You can also see if the balancer is enabled using sh.status(). 您还可以使用sh.status()查看平衡器是否已启用。The currently-enabled field indicates whether the balancer is enabled, while the currently-running field indicates if the balancer is currently running.currently-enabled用字段指示平衡器是否已启用,而currently-running字段指示平衡器当前是否正在运行。

Check if Balancer is Running检查平衡器是否正在运行

To see if the balancer process is active in your cluster:要查看平衡器进程在集群中是否处于活动状态,请执行以下操作:

  1. Connect to any mongos in the cluster using the mongosh shell.使用mongosh shell连接到集群中的任何mongos
  2. Use the following operation to determine if the balancer is running:使用以下操作来确定平衡器是否正在运行:

    sh.isBalancerRunning()

Configure Default Range Size配置默认范围大小

The default range size for a sharded cluster is 128 megabytes. 分片集群的默认范围大小为128兆字节。In most situations, the default size is appropriate for splitting and migrating chunks. 在大多数情况下,默认大小适用于分割和迁移块。For information on how range size affects deployments, see details, see Range Size.有关范围大小如何影响部署的信息,请参阅详细信息,请参阅范围大小

Changing the default range size affects ranges that are processes during migrations and auto-splits but does not retroactively affect all ranges.更改默认范围大小会影响迁移和自动拆分过程中的进程范围,但不会追溯到所有范围。

Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改

To configure default range size, see Modify Range Size in a Sharded Cluster.要配置默认范围大小,请参阅修改分片群集中的范围大小

Schedule the Balancing Window安排平衡窗口

In some situations, particularly when your data set grows slowly and a migration can impact performance, it is useful to ensure that the balancer is active only at certain times. 在某些情况下,特别是当数据集增长缓慢且迁移可能影响性能时,确保平衡器仅在特定时间处于活动状态是非常有用的。The following procedure specifies the activeWindow, which is the timeframe during which the balancer will be able to migrate chunks:以下过程指定activeWindow,这是平衡器能够迁移块的时间段:

1

Connect to mongos using mongosh.使用mongosh连接到mongos

You can connect to any mongos in the cluster.您可以连接到集群中的任何mongos

2

Switch to the Config Database.切换到配置数据库

Issue the following command to switch to the config database.发出以下命令以切换到配置数据库。

use config
3

Ensure that the balancer is not stopped.确保平衡器没有停止。

The balancer will not activate in the stopped state. 平衡器在stopped状态下不会激活。To ensure that the balancer is not stopped, use sh.startBalancer(), as in the following:要确保平衡器没有停止,请使用sh.startBalancer(),如下所示:

sh.startBalancer()

The balancer will not start if you are outside of the activeWindow timeframe.如果您在activeWindow时间范围之外,平衡器将不会启动。

Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改

In MongoDB versions earlier than 6.1, sh.startBalancer() also enables auto-splitting for the sharded cluster.在6.1之前的MongoDB版本中,sh.startBalancer()还支持对分片集群进行自动拆分。

4

Modify the balancer's window.修改平衡器的窗口。

Set the activeWindow using updateOne(), as in the following:使用updateOne()设置activeWindow,如下所示:

db.settings.updateOne(
{ _id: "balancer" },
{ $set: { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } },
{ upsert: true }
)

Replace <start-time> and <end-time> with time values using two digit hour and minute values (i.e. HH:MM) that specify the beginning and end boundaries of the balancing window.使用指定平衡窗口的开始和结束边界的两位小时和分钟值(即HH:MM)将<start-time><end-time>替换为时间值。

  • For HH values, use hour values ranging from 00 - 23.对于HH值,使用00-23之间的小时值。
  • For MM value, use minute values ranging from 00 - 59.对于MM值,请使用00-59之间的分钟值。

For on-premises or self-managed sharded clusters, MongoDB evaluates the start and stop times relative to the time zone of the primary member in the config server replica set.对于内部部署或自管理的分片集群,MongoDB评估相对于配置服务器副本集中主要成员的时区的开始和停止时间。

For Atlas clusters, MongoDB evaluates the start and stop times relative to the UTC timezone.对于Atlas集群,MongoDB评估相对于UTC时区的开始和停止时间。

Note

The balancer window must be sufficient to complete the migration of all data inserted during the day.平衡器窗口必须足以完成当天插入的所有数据的迁移。

As data insert rates can change based on activity and usage patterns, it is important to ensure that the balancing window you select will be sufficient to support the needs of your deployment.由于数据插入率可能会根据活动和使用模式而变化,因此确保您选择的平衡窗口足以满足您的部署需求非常重要。

Remove a Balancing Window Schedule删除平衡窗口计划

If you have set the balancing window and wish to remove the schedule so that the balancer is always running, use $unset to clear the activeWindow, as in the following:如果您已经设置了平衡窗口,并希望删除时间表,以便平衡器始终运行,请使用$unset清除activeWindow,如下所示:

use config
db.settings.updateOne( { _id : "balancer" }, { $unset : { activeWindow : true } } )

Disable the Balancer禁用平衡器

By default, the balancer may run at any time and only moves chunks as needed. 默认情况下,平衡器可以在任何时候运行,并且只根据需要移动块。To disable the balancer for a short period of time and prevent all migration, use the following procedure:要在短时间内禁用平衡器并阻止所有迁移,请使用以下过程:

  1. Connect to any mongos in the cluster using the mongosh shell.使用mongosh shell连接到集群中的任何mongos
  2. Issue the following operation to disable the balancer:发出以下操作以禁用平衡器:

    sh.stopBalancer()

    If a migration is in progress, the system will complete the in-progress migration before stopping.如果正在进行迁移,系统将在停止之前完成正在进行的迁移。

    Starting in MongoDB 6.1, automatic chunk splitting is not performed. This is because of balancing policy improvements. 从MongoDB 6.1开始,不执行自动区块分割。这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改

    In MongoDB versions earlier than 6.1, sh.stopBalancer() also disables auto-splitting for the sharded cluster.在6.1之前的MongoDB版本中,sh.stopBalancer()还禁用了分片集群的自动拆分。

  3. To verify that the balancer will not start, issue the following command, which returns false if the balancer is disabled:要验证平衡器是否不会启动,请发出以下命令,如果平衡器被禁用,则返回false

    sh.getBalancerState()

    Optionally, to verify no migrations are in progress after disabling, issue the following operation in the mongosh shell:(可选)要验证禁用后没有迁移正在进行,请在mongosh shell中发出以下操作:

    use config
    while( sh.isBalancerRunning() ) {
    print("waiting...");
    sleep(1000);
    }
Note

To disable the balancer from a driver, use the balancerStop command against the admin database, as in the following:要从驱动程序禁用平衡器,请对admin数据库使用balancerStop命令,如下所示:

db.adminCommand( { balancerStop: 1 } )

Enable the Balancer启用平衡器

Use this procedure if you have disabled the balancer and are ready to re-enable it:如果已禁用平衡器并准备重新启用,请使用以下步骤:

  1. Connect to any mongos in the cluster using the mongosh shell.使用mongosh shell连接到集群中的任何mongos
  2. Issue one of the following operations to enable the balancer:发出以下操作之一以启用平衡器:

    From the mongosh shell, issue:mongosh shell发布:

    sh.startBalancer()
    Note

    To enable the balancer from a driver, use the balancerStart command against the admin database, as in the following:要从驱动程序启用平衡器,请对admin数据库使用balancerStart命令,如下所示:

    db.adminCommand( { balancerStart: 1 } )

    Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.有关详细信息,请参阅平衡策略更改

    In MongoDB versions earlier than 6.1, sh.startBalancer() also enables auto-splitting for the sharded cluster.在6.1之前的MongoDB版本中,sh.startBalancer()还支持对分片集群进行自动拆分。

Disable Balancing During Backups在备份期间禁用平衡

Note

Disabling the balancer is only necessary when manually taking backups, either by calling mongodump or scheduling a task that calls mongodump at a specific time.只有在手动进行备份时才需要禁用平衡器,无论是通过调用mongodump还是安排在特定时间调用mongoump的任务。

You do not have to disable the balancer when using coordinated backup and restore processes:在使用协调的备份和恢复过程时,您不必禁用平衡器:

If MongoDB migrates a chunk during a backup, you can end with an inconsistent snapshot of your sharded cluster. 如果MongoDB在备份过程中迁移了一个区块,那么您可能会得到一个不一致的分片集群快照。Never run a backup while the balancer is active. 平衡器处于活动状态时,切勿运行备份。To ensure that the balancer is inactive during your backup operation:要确保平衡器在备份操作期间处于非活动状态,请执行以下操作:

  • Set the balancing window so that the balancer is inactive during the backup. Ensure that the backup can complete while you have the balancer disabled.设置平衡窗口,使平衡器在备份期间处于非活动状态。请确保在禁用平衡器的情况下可以完成备份。
  • manually disable the balancer for the duration of the backup procedure.在备份过程期间手动禁用平衡器

If you turn the balancer off while it is in the middle of a balancing round, the shut down is not instantaneous. 如果在平衡循环进行到一半时关闭平衡器,则关闭不是即时的。The balancer completes the chunk move in-progress and then ceases all further balancing rounds.平衡器完成正在进行的区块移动,然后停止所有进一步的平衡回合。

Before starting a backup operation, confirm that the balancer is not active. 在启动备份操作之前,请确认平衡器未处于活动状态。You can use the following command to determine if the balancer is active:您可以使用以下命令来确定平衡器是否处于活动状态:

!sh.getBalancerState() && !sh.isBalancerRunning()

When the backup procedure is complete you can reactivate the balancer process.备份过程完成后,您可以重新激活平衡器进程。

Disable Balancing on a Collection禁用集合上的平衡

You can disable balancing for a specific collection with the sh.disableBalancing() method. 您可以使用sh.disableBalancing()方法禁用特定集合的平衡。You may want to disable the balancer for a specific collection to support maintenance operations or atypical workloads, for example, during data ingestions or data exports.您可能希望禁用特定集合的平衡器,以支持维护操作或非典型工作负载,例如,在数据摄取或数据导出期间。

When you disable balancing on a collection, MongoDB will not interrupt in progress migrations.当您在集合上禁用平衡时,MongoDB不会中断正在进行的迁移。

To disable balancing on a collection, connect to a mongos with the mongosh shell and call the sh.disableBalancing() method.要禁用集合上的平衡,请使用mongosh shell连接到mongos并调用sh.disableBalancing()方法。

For example:例如:

sh.disableBalancing("students.grades")

The sh.disableBalancing() method accepts as its parameter the full namespace of the collection.sh.disableBalancing()方法接受集合的完整命名空间作为其参数。

Enable Balancing on a Collection对集合启用平衡

You can enable balancing for a specific collection with the sh.enableBalancing() method.您可以使用sh.enableBalancing()方法为特定集合启用平衡。

When you enable balancing for a collection, MongoDB will not immediately begin balancing data. 当您为集合启用平衡时,MongoDB不会立即开始平衡数据。However, if the data in your sharded collection is not balanced, MongoDB will be able to begin distributing the data more evenly.然而,如果您的分片集合中的数据不平衡,MongoDB将能够开始更均匀地分配数据。

To enable balancing on a collection, connect to a mongos with the mongosh shell and call the sh.enableBalancing() method.要在集合上启用平衡,请使用mongosh shell连接到mongos并调用sh.enableBalancing()方法。

For example:例如:

sh.enableBalancing("students.grades")

The sh.enableBalancing() method accepts as its parameter the full namespace of the collection.sh.enableBalancing()方法接受集合的完整命名空间作为其参数。

Confirm Balancing is Enabled or Disabled确认已启用或禁用平衡

To confirm whether balancing for a collection is enabled or disabled, query the collections collection in the config database for the collection namespace and check the noBalance field. 要确认集合的平衡是启用还是禁用,请在config数据库中查询集合命名空间collections集合,并检查noBalance字段。For example:例如:

db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;

This operation will return a null error, true, false, or no output:此操作将返回null错误、truefalse或无输出:

  • A null error indicates the collection namespace is incorrect.null错误表示集合命名空间不正确。
  • If the result is true, balancing is disabled.如果结果为true,则禁用平衡。
  • If the result is false, balancing is enabled currently but has been disabled in the past for the collection. 如果结果为false,则当前已启用平衡,但过去已禁用集合的平衡。Balancing of this collection will begin the next time the balancer runs.此集合的平衡将在平衡器下次运行时开始。
  • If the operation returns no output, balancing is enabled currently and has never been disabled in the past for this collection. 如果操作未返回任何输出,则当前已启用平衡,并且以前从未对此集合禁用过平衡。Balancing of this collection will begin the next time the balancer runs.此集合的平衡将在平衡器下次运行时开始。

You can also see if the balancer is enabled using sh.status(). 您还可以使用sh.status()查看平衡器是否已启用。The currently-enabled field indicates if the balancer is enabled.currently-enabled字段指示平衡器是否已启用。

Change Replication Behavior for Chunk Migration更改块迁移的复制行为

Secondary Throttle辅助节流阀

During chunk migration, the _secondaryThrottle value determines when the migration proceeds with next document in the chunk.在块迁移过程中,_secondaryThrottle值确定迁移何时进行块中的下一个文档。

In the config.settings collection:config.settings集合中:

  • If the _secondaryThrottle setting for the balancer is set to a write concern, each document move during chunk migration must receive the requested acknowledgement before proceeding with the next document.如果平衡器的_secondaryThrottle设置设置为写入问题,则块迁移期间的每个文档移动都必须在继续下一个文档之前收到请求的确认。
  • If the _secondaryThrottle setting for the balancer is set to true, each document move during chunk migration must receive acknowledgement from at least one secondary before the migration proceeds with the next document in the chunk. 如果平衡器的_secondaryThrottle设置设置为true,则块迁移期间的每个文档移动都必须至少从一个辅助文档接收到确认,然后才能继续迁移块中的下一个文档。This is equivalent to a write concern of { w: 2 }.这相当于{ w: 2 }的写入问题。
  • If the _secondaryThrottle setting is unset, the migration process does not wait for replication to a secondary and instead continues with the next document.如果未设置_secondaryThrottle设置,则迁移过程不会等待复制到辅助文档,而是继续执行下一个文档。

    This is the default behavior for WiredTiger.这是WiredTiger的默认行为。

To change the _secondaryThrottle setting, connect to a mongos instance and directly update the _secondaryThrottle value in the settings collection of the config database. 要更改_secondaryThrottle设置,请连接到mongos实例,然后直接更新config数据库settings集合中的_secondaryThrottle值。For example, from a mongosh shell connected to a mongos, issue the following command:例如,从连接到mongosmongosh shell中,发出以下命令:

use config
db.settings.updateOne(
{ "_id" : "balancer" },
{ $set : { "_secondaryThrottle" : { "w": "majority" } } },
{ upsert : true }
)

The effects of changing the _secondaryThrottle setting may not be immediate. 更改_secondaryThrottle设置可能不会立即产生影响。To ensure an immediate effect, stop and restart the balancer to enable the selected value of _secondaryThrottle.要确保立即生效,请停止并重新启动平衡器,以启用选定的_secondaryThrottle值。

For more information on the replication behavior during various steps of chunk migration, see Range Migration and Replication.有关区块迁移各个步骤期间的复制行为的更多信息,请参阅范围迁移和复制

  • Use the moveRange command's secondaryThrottle and writeConcern options to specify the behavior during the command.使用moveRange命令的secondaryThrottlewriteConcern选项可以指定命令执行期间的行为。
  • Use the moveChunk command's _secondaryThrottle and writeConcern options to specify the behavior during the command.使用moveChunk命令的_secondaryThrottlewriteConcern选项可以指定命令执行期间的行为。

For details, see moveRange and moveChunk.有关详细信息,请参阅moveRangemoveChunk

Wait for Delete等待删除

The _waitForDelete setting of the balancer and the moveChunk command affects how the balancer migrates multiple chunks from a shard. 平衡器的_waitForDelete设置和moveChunk命令会影响平衡器如何从分片迁移多个块。Similarly, the _waitForDelete setting of the balancer and the moveRange command also affect how the balancer migrates multiple chunks from a shard. 类似地,平衡器的_waitForDelete设置和moveRange命令也会影响平衡器如何从分片迁移多个块。By default, the balancer does not wait for the on-going migration's delete phase to complete before starting the next chunk migration. 默认情况下,平衡器不等待正在进行的迁移的删除阶段完成,然后再开始下一个块迁移。To have the delete phase block the start of the next chunk migration, you can set the _waitForDelete to true.要使删除阶段阻止下一个块迁移的开始,可以将_waitForDelete设置为true

For details on chunk migration, see Range Migration. 有关区块迁移的详细信息,请参阅范围迁移For details on the chunk migration queuing behavior, see Asynchronous Range Migration Cleanup.有关区块迁移排队行为的详细信息,请参阅异步范围迁移清理

The _waitForDelete is generally for internal testing purposes. _waitForDelete通常用于内部测试。To change the balancer's _waitForDelete value:要更改平衡器的_waitForDelete值,请执行以下操作:

  1. Connect to a mongos instance.连接到mongos实例。
  2. Update the _waitForDelete value in the settings collection of the config database. 更新config数据库settings集合中的_waitForDelete值。For example:例如:

    use config
    db.settings.updateOne(
    { "_id" : "balancer" },
    { $set : { "_waitForDelete" : true } },
    { upsert : true }
    )

Once set to true, to revert to the default behavior:一旦设置为true,要恢复到默认行为:

  1. Connect to a mongos instance.连接到mongos实例。
  2. Update or unset the _waitForDelete field in the settings collection of the config database:更新或取消设置config数据库settings集合中的_waitForDelete字段:

    use config
    db.settings.updateOne(
    { "_id" : "balancer", "_waitForDelete": true },
    { $unset : { "_waitForDelete" : "" } }
    )

Balance Ranges that Exceed Size Limit超出大小限制的平衡范围

By default, MongoDB cannot move a range if the number of documents in the range is greater than 2 times the result of dividing the configured range size by the average document size.默认情况下,如果范围中的文档数量大于配置的范围大小除以平均文档大小的结果的2倍,MongoDB将无法移动该范围。

Starting in MongoDB 4.4, by specifying the balancer setting attemptToBalanceJumboChunks to true, the balancer can migrate these large ranges as long as they have not been labeled as jumbo.从MongoDB 4.4开始,通过指定平衡器设置attemptToBalanceJumboChunkstrue,平衡器可以迁移这些大范围,只要它们没有被标记为jumbo

To set the balancer's attemptToBalanceJumboChunks setting, connect to a mongos instance and directly update the config.settings collection. 要设置平衡器的attemptToBalanceJumboChunks设置,请连接到mongos实例并直接更新config.settings集合。For example, from a mongosh shell connected to a mongos instance, issue the following command:例如,从连接到mongos实例的mongosh shell中,发出以下命令:

db.getSiblingDB("config").settings.updateOne(
{ _id: "balancer" },
{ $set: { attemptToBalanceJumboChunks : true } },
{ upsert: true }
)

When the balancer attempts to move the range, if the queue of writes that modify any documents being migrated surpasses 500MB of memory the migration will fail. 当平衡器尝试移动范围时,如果修改要迁移的任何文档的写入队列超过500MB内存,则迁移将失败。For details on the migration procedure, see Range Migration Procedure.有关迁移过程的详细信息,请参阅范围迁移过程

If the range you want to move is labeled jumbo, you can manually clear the jumbo flag to have the balancer attempt to migrate the range.如果要移动的范围标记为jumbo,则可以手动清除jumbo标志,让平衡器尝试迁移该范围。

You can also manually migrate ranges that exceed the size limit (with or without the jumbo label) using either:您还可以手动迁移超过大小限制的范围(使用或不使用jumbo标签),方法如下:

However, when you run moveRange or moveChunk with forceJumbo: true, write operations to the collection may block for a long period of time during the migration.但是,当您使用forceJumbo: true运行moveRangemoveChunk时,对集合的写入操作可能会在迁移期间阻塞很长一段时间。

Change the Maximum Storage Size for a Given Shard更改给定分片的最大存储大小

Changing a shard's maximum storage size is deprecated. 不赞成更改分片的最大存储大小。Starting in version 4.2, MongoDB uses the WiredTiger Storage Engine to manage how data is stored. WiredTiger compresses data to minimize storage use. 从4.2版本开始,MongoDB使用WiredTiger存储引擎来管理数据的存储方式。WiredTiger压缩数据以最大限度地减少存储使用。To learn more, see Compression.要了解更多信息,请参阅压缩

For MongoDB versions 6.1 and earlier, you can still set a maximum storage size for a given shard in the sharded cluster by using the maxSize field. 对于MongoDB 6.1及更早版本,您仍然可以使用maxSize字段为分片集群中的给定分片设置最大存储大小。You can find configuration data related to shards in the config.shards collection.您可以在config.shards集合中找到与分片相关的配置数据。

Starting in version 6.2, MongoDB removes the maxSize field from the addShard command. As a result:从6.2版本开始,MongoDB从addShard命令中删除了maxSize字段。因此:

  • Running addShard with the maxSize field returns an InvalidOptions error.使用maxSize字段运行addShard会返回InvalidOptions错误。
  • New documents in the shards collection no longer include the maxSize field.shards集合中的新文档不再包含maxSize字段。
  • Any pre-existing maxSize field entries are ignored.任何预先存在的maxSize字段条目都将被忽略。