天天看點

mongobd分片 之 balancer常用指令

文章目錄

        • 檢視在 shards 間遷移 資料塊 的 balancer 是 打開或關閉
            • 1、開啟關閉balanceer
            • 2、關閉或開啟某張表的balanceer

均衡器¶

The balancer attempts to evenly distribute a sharded collection’s chunks across all shards in the cluster.

對于每一個标記為要遷移的 chunk ,均衡器檢查所有配置區域内的每一個可能的目标分片。如果資料塊範圍屬于某一個區域,那麼均衡器就會将該資料塊遷移到該區域上的一個分片。不屬于任何區域的資料塊可以存在于叢集中的 任何 分片,并且正常遷移。

在均衡過程中,如果均衡器檢測到任何違背已配置區域上給定的分片,均衡器将會把這些資料塊遷移到不存在沖突的分片上。

在使用一個片鍵範圍配置區域,并且将它與一個或多個分片關聯起來之後,叢集可能會花費一些時間來遷移影響的資料。這依賴于資料塊的劃分以及目前叢集中資料的分布。當均衡完成之後,在某一給定區域的文檔讀取和寫入将隻會路由到該區域内的一個分片或幾個分片。

一旦配置完成後,均衡器将在 balancing rounds 中重新檢查區域。

檢視在 shards 間遷移 資料塊 的 balancer 是 打開或關閉

sh.isBalancerRunning()#檢視在 shards 間遷移 資料塊 的 balancer 是 打開或關閉.
           

1、開啟關閉balanceer

關閉balancer

mongos> use config
	switched to db config
	mongos> sh.getBalancerState()
	true
	mongos> sh.stopBalancer()
	Waiting for active hosts...
	Waiting for the balancer lock...
	Waiting again for active hosts after balancer is off...
	WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "balancer" })
	mongos> sh.getBalancerState()
	false
	#開啟
	mongos> sh.setBalancerState(true)
	WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
	mongos> sh.getBalancerState()
	true
	mongos>
           

2、關閉或開啟某張表的balanceer

#關閉
	mongos> sh.disableBalancing('testdb.table1')
	WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
	#檢視
	mongos> db.getSiblingDB("config").collections.findOne({_id:'testdb.table1'}).noBalance;
	true
	#開啟
	mongos> sh.enableBalancing('testdb.table1')
	WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
	mongos> db.getSiblingDB("config").collections.findOne({_id:'testdb.table1'}).noBalance;
	false
	mongos> sh.disableBalancing('testdb.table1')
	WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
	mongos> db.getSiblingDB("config").collections.findOne({_id:'testdb.table1'}).noBalance;
	true
           
sh.getBalancerHost()	
傳回一個布爾值,報告目前是否有均衡器在進行資料塊的遷移.傳回負責均衡過程的一個mongos 名字.

sh.status()	
内部指令,等待指定的 sharded cluster 分布鎖.