How Do I Manage the Balancer?
Scenario
- Enable the balancer during off-peak hours.
- Disable the balancer during peak hours.
- Schedule a balancing window so that the balancer can work during off-peak hours.
Constraints
- The balancer applies only to cluster instances.
- DDS enables the balancer by default.
Enabling the Balancer
- .
- After connecting to a dds mongos node, run the following command to switch to the config database:
use config
- Run the following command to enable the balancer:
sh.setBalancerState(true)
Disabling the Balancer
- .
- After connecting to a dds mongos node, run the following command to switch to the config database:
use config
- Run the following command to check the status of the balancer:
while( sh.isBalancerRunning() ) { print("waiting..."); sleep(500); }
- If the command output is empty, the balancer is not executing any task. In this case, you can proceed to disable the balancer.
- If the command output is waiting, the balancer is migrating chunks. In this case, you cannot disable the balancer. Otherwise, data may become inconsistent.
- If the command output in 3 is empty, run the following command to disable the balancer:
sh.stopBalancer()
Scheduling a Balancing Window
To prevent chunk migration from affecting your businesses, you can perform the following operations to schedule a balancing window.

Before scheduling a balancing window, ensure that the balancer is enabled by referring to Enabling the Balancer.
- .
- After connecting to a dds mongos node, run the following command to switch to the config database:
use config
- Run the following commands to schedule a balancing window:
db.adminCommand({"setBalancerActiveWindow": 1, start : "<start-time>", stop : "<stop-time>"})
- <start-time>: indicates the beginning of the balancing window. Specify the time in the HH:MM format (UTC time). For HH values, use hour values ranging from 00 to 23. For MM value, use minute values ranging from 00 to 59.
- <stop-time>: indicates the end of the balancing window. Specify the time in the HH:MM format (UTC time). For HH values, use hour values ranging from 00 to 23. For MM value, use minute values ranging from 00 to 59.
- If the error message "CommandNotFound" is displayed when you run the setBalancerActiveWindow command as user rwuser to schedule a balancing window, the minor version is too early. In this case, upgrade the minor version of the instance and try again. Alternatively, .
You can run the sh.status() command to check a balancing window schedule. The following command output in Figure 1 shows that the balancing window is 22:00 to 24:00.
Removing a Balancing Window Schedule
If you have set the balancing window and wish to remove the schedule so that the balancer is always running, run the following command:
db.adminCommand({ 'unsetBalancerActiveWindow' : 1 })
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.