To enable the balancer, proceed as follows:
- Open a mongo shell onto a mongos instance with sufficient cluster administration rights.
- Issue the sh.startBalancer() command to enable the balancer. You can supply an optional argument string representing the database and collection if you wish to enable balancing only on a specific collection. The command can be seen in the following code snippet:
sh.startBalancer("DB.COLLECTION");
- Confirm the balancer state by running the following command. A value of true indicates the balancer is enabled:
sh.getBalancerState();
Here is the output of this sequence from the sharded cluster model:
If you need to enable or disable the balancer from an application, it's best to use a direct database admin command.Â
Here is the command to disable the balancer:Â db.adminCommand( { balancerStop: 1 } ).Â
Here is the command to start the balancer:Â db.adminCommand...
Here is the command to disable the balancer:Â db.adminCommand( { balancerStop: 1 } ).Â
Here is the command to start the balancer:Â db.adminCommand...