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.不检查平衡器是否正在主动迁移数据。
To see if the balancer is enabled in your sharded cluster, run 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, and 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:要查看平衡器进程是否在集群中处于活动状态,请执行以下操作:
Configure Default Range Size配置默认范围大小
The default range size for a sharded cluster is 128 megabytes. 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.分片集群的默认范围大小为128 MB。在大多数情况下,默认大小适合分割和迁移块。有关范围大小如何影响部署的信息,请参阅详细信息,请参阅范围大小。
Changing the default range size affects ranges that are processes during migrations and auto-splits but does not retroactively affect all ranges.更改默认范围大小会影响迁移和自动拆分过程中的范围,但不会追溯影响所有范围。
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. 在某些情况下,特别是当数据集增长缓慢并且迁移会影响性能时,确保平衡器仅在特定时间处于活动状态是有用的。By default, the balancer process is always enabled and migrating chunks. The following procedure specifies the 默认情况下,平衡器进程始终处于启用状态并迁移块。以下过程指定了activeWindow, which is the timeframe during which the balancer is able to migrate chunks:activeWindow,这是平衡器能够迁移块的时间段:
Connect to mongos using mongosh.使用mongosh连接到mongos。
mongos using mongosh.You can connect to any 您可以连接到集群中的任何mongos in the cluster.mongos。
Switch to the Config Database.切换到Config数据库。
Issue the following command to switch to the config database.发出以下命令以切换到配置数据库。
use configEnsure that the balancer is not stopped.确保平衡器没有停止。
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:stopped,请使用shstartBalancer(),如下所示:
sh.startBalancer()
The balancer will not start if you are outside of the 如果您在activeWindow timeframe.activeWindow时间范围之外,平衡器将不会启动。
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.<start-time>和<end-time>替换为使用两位数小时和分钟值(即HH:MM)的时间值,这些值指定了平衡窗口的开始和结束边界。
For对于HHvalues, use hour values ranging from00-23.HH值,使用00-23范围内的小时值。For对于MMvalue, use minute values ranging from00-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.由于数据插入率可能会根据活动和使用模式而变化,因此确保您选择的平衡窗口足以支持部署的需求非常重要。
Check Balancing Window检查平衡窗口
To see the current balancing window, run the following command:要查看当前的平衡窗口,请运行以下命令:
use config
db.settings.find( { _id: "balancer" } )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禁用平衡器
Important
Leaving the balancer disabled for extended periods of time can lead to unbalanced shards, which degrade cluster performance. Only disable the balancer if necessary, and ensure that you re-enable the balancer when maintenance is complete.长时间禁用平衡器可能会导致分片不平衡,从而降低集群性能。仅在必要时禁用平衡器,并确保在维护完成后重新启用平衡器。
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:默认情况下,平衡器可以随时运行,并且只在需要时移动块。要在短时间内禁用平衡器并阻止所有迁移,请使用以下过程:
Connect to any使用mongosin the cluster using themongoshshell.mongoshshell连接到集群中的任何mongos。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.如果正在进行迁移,系统将在停止之前完成正在进行的迁移。To verify that the balancer won't start, run the following command, which returns要验证平衡器不会启动,请运行以下命令,如果平衡器被禁用,则返回falseif the balancer is disabled:false:sh.getBalancerState()Optionally, to verify no migrations are in progress after disabling, run the following operation in the(可选)要验证禁用后没有正在进行的迁移,请在mongoshshell:mongoshshell中运行以下操作: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:如果您已禁用平衡器并准备重新启用它,请使用此过程:
- Connect to any
mongosin the cluster using themongoshshell. Issue one of the following operations to enable the balancer:执行以下操作之一以启用平衡器:From the来自猫鼬壳,问题:mongoshshell, issue:sh.startBalancer()Note
To enable the balancer from a driver, use the balancerStart command against the要从驱动程序启用平衡器,请对admindatabase, as in the following:admin数据库使用balancerStart命令,如下所示:db.adminCommand( { balancerStart: 1 } )
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还是安排在特定时间调用mongodump的任务。
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在备份过程中迁移了一个admin块,您可能会以分片集群的不一致快照结束。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. 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.sh.disableBalancing()方法禁用特定集合的平衡。您可能希望禁用特定集合的平衡器,以支持维护操作或非典型工作负载,例如在数据摄入或数据导出期间。
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. However, if the data in your sharded collection is not balanced, MongoDB will be able to begin distributing the data more evenly.当您为集合启用平衡时,MongoDB不会立即开始平衡数据。但是,如果分片集合中的数据不平衡,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. For example:config数据库中查询collections集合命名空间中的集合集合集合,并检查noBalance字段。例如:
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;
This operation will return a null error, 此操作将返回true, false, or no output:null错误、true、false或无输出:
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. Balancing of this collection will begin the next time the balancer runs.false,则当前已启用平衡,但过去已禁用该集合的平衡。下次平衡器运行时,将开始对此集合进行平衡。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如果平衡器的_secondaryThrottlesetting for the balancer is set to a write concern, each document moved during chunk migration must receive the requested acknowledgment before proceeding with the next document._seconditionThrottle设置设置为写入关注,则块迁移期间移动的每个文档在继续处理下一个文档之前都必须收到请求的确认。If the如果未设置_secondaryThrottlesetting is unset, the migration process does not wait for replication to a secondary and instead continues with the next document._seconditionThrottle设置,迁移过程不会等待复制到辅助文档,而是继续处理下一个文档。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. For example, from a mongosh shell connected to a mongos, run the following command:_secondaryThrottle设置,请连接到mongos实例,并直接更新config数据库settings集合中的_secondaryThrottle值。例如,从连接到mongos的mongosh 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. To ensure an immediate effect, stop and restart the balancer to enable the selected value of _secondaryThrottle._secondaryThrottle设置的效果可能不会立即显现。为确保立即生效,请停止并重新启动平衡器,以启用选定的_secondaryThrottle值。
For more information on the replication behavior during various steps of chunk migration, see Range Migration and Replication.有关块迁移各个步骤中的复制行为的更多信息,请参阅范围迁移和复制。
Use the使用moveRangecommand'ssecondaryThrottleandwriteConcernoptions to specify the behavior during the command.moveRange命令的secondaryThrottle和writeConcern选项指定命令执行期间的行为。Use the使用moveChunkcommand's_secondaryThrottleandwriteConcernoptions to specify the behavior during the command.moveChunk命令的_seconditionThrottle和writeConcern选项指定命令执行期间的行为。
For details, see moveRange and moveChunk.
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.有关块迁移的详细信息,请参阅范围迁移。有关块迁移排队行为的详细信息,请参阅异步范围迁移清理。
Important
When the 当设置_waitForDelete field is set, MongoDB does not wait on the orphanCleanupDelaySecs delay before performing the range deletion. _waitForDelete字段时,MongoDB在执行范围删除之前不会等待orphanCleanupDelaySecs延迟。If you use the 如果使用_waitForDelete parameter and have any read operations occurring on secondaries, the read might terminate due to the migration's delete phase. _waitForDelete参数并在次服务器上进行任何读取操作,则读取可能会因迁移的删除阶段而终止。To learn more, see 要了解更多信息,请参阅终止terminateSecondaryReadsOnOrphanCleanup.terminateSecondaryReadsOnOrphanCleanup。
To change the balancer's 要更改平衡器的_waitForDelete value:_waitForDelete值,请执行以下操作:
Connect to a连接到mongosinstance.mongos实例。Update the更新_waitForDeletevalue in thesettingscollection of the config database. For example:config数据库的settings集合中的_waitForDelete值。例如:use config
db.settings.updateOne(
{ "_id" : "balancer" },
{ $set : { "_waitForDelete" : true } },
{ upsert : true }
)
Once set to 一旦设置为true, to revert to the default behavior:true,要恢复默认行为:
Connect to a连接到mongosinstance.mongos实例。Update or unset the更新或取消设置_waitForDeletefield in thesettingscollection 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无法移动范围。
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.attemptToBalanceJumboChunks指定为true,平衡器可以迁移这些大范围,只要它们没有被标记为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, run the following command:mongos实例的mongosh shell运行以下命令:
db.getSiblingDB("config").settings.updateOne(
{ _id: "balancer" },
{ $set: { attemptToBalanceJumboChunks : true } },
{ upsert: true }
)
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标签):
the带有moveRangecommand with the forceJumbo: true optionforceJumbo: true选项的moveRange命令the带有moveChunkcommand with the forceJumbo: true optionforceJumbo: true选项的moveChunk命令
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运行moveRange或moveChunk时,对集合的写入操作可能会在迁移过程中长时间阻塞。