分片集群的查询及管理

分片集群的查询及管理

判断是否Shard集群

admin> db.runCommand({ isdbgrid : 1})

mongos> db.runCommand( { isdbgrid:1 })
{
	"isdbgrid" : 1,
	"hostname" : "mysql-node01",
	"ok" : 1,
	"operationTime" : Timestamp(1603542446, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1603542446, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos>

列出所有分片信息

admin> db.runCommand({ listshards : 1})

mongos> db.runCommand( {listshards :1 } )
{
	"shards" : [
		{
			"_id" : "shard1",
			"host" : "sh1/10.0.50.61:38021,10.0.50.61:38022",
			"state" : 1
		},
		{
			"_id" : "shard2",
			"host" : "sh2/10.0.50.61:38024,10.0.50.61:38025",
			"state" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1603542496, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1603542496, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
mongos> 

列出开启分片的数据库

admin> use config
config> db.databases.find( { "partitioned": true } )
或者:
config> db.databases.find() //列出所有数据库分片情况

查看分片的片键

config> db.collections.find().pretty()
{
    "_id" : "test.vast",
    "lastmodEpoch" : ObjectId("58a599f19c898bbfb818b63c"),
    "lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
    "dropped" : false,
    "key" : {
        "id" : 1
    },
    "unique" : false
}

查看分片的详细信息

admin> sh.status()

# 以下是需要关注的信息
(1)所有分片节点 #Arbiter 节点不会在这里显示
shards:
{  "_id" : "shard1",  "host" : "sh1/10.0.50.61:38021,10.0.50.61:38022",  "state" : 1 } 
{  "_id" : "shard2",  "host" : "sh2/10.0.50.61:38024,10.0.50.61:38025",  "state" : 1 }

(2)balancer状态
balancer:
Currently enabled:  yes
Currently running:  no
Failed balancer rounds in last 5 attempts:  0
Migration Results for the last 24 hours: 
2 : Success

(3)开启分片功能的库和表信息
{  "_id" : "oldboy",  "primary" : "shard1",  "partitioned" : true }
oldboy.vast	#库名.表名
shard key: { "id" : "hashed" }	#分片方式
balancing: true
chunks:
shard1	2
shard2	2
 
{  "_id" : "test",  "primary" : "shard2",  "partitioned" : true }
test.vast
shard key: { "id" : 1 }
balancing: true
chunks:
shard2	1



删除分片节点(谨慎)

(1)确认blancer是否在工作
sh.getBalancerState()
(2)删除shard2节点(谨慎)
mongos> db.runCommand( { removeShard: "shard2" } )
注意:删除操作一定会立即触发blancer。

添加分片节点

搭建复制集 shard3

按照之前步骤搭建

添加分片
db.runCommand( { addshard : "sh3/10.0.50.61:38027,10.0.50.61:38028,10.0.50.61:38029",name:"shard3"} )

Balancer操作

介绍

mongos的一个重要功能,自动巡查所有shard节点上的chunk的情况,自动做chunk迁移。
什么时候工作?
1、自动运行,会检测系统不繁忙的时候做迁移
2、在做节点删除的时候,立即开始迁移工作
3、balancer只能在预设定的时间窗口内运行

可以人为定制balancer工作时间,生产环境推荐在晚上执行,避开业务繁忙期。
要避开备份的时间,因为chunk的迁移,会导致备份不一致

有需要时可以关闭和开启blancer(备份的时候)
在mongos节点,use config

mongos> sh.stopBalancer()
mongos> sh.startBalancer()

自定义 自动平衡进行的时间段

https://docs.mongodb.com/manual/tutorial/manage-sharded-cluster-balancer/#schedule-the-balancing-window
// connect to mongos

# 生产中必调参数

use config
sh.setBalancerState( true )

# 设置balancer 工作时间为凌晨 3.00 到 5.00进行chunk迁移
db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "3:00", stop : "5:00" } } }, true )

# blancer工作时间要避开备份的时间窗口
# blancer触发chunk迁移,会导致备份数据不一致
    
sh.getBalancerWindow()
sh.status()

# 删除指定的时间段:
在mongos节点,
use config
db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true })


关于集合的balancer(了解下)
关闭某个集合的balance
sh.disableBalancing("students.grades")
打开某个集合的balancer
sh.enableBalancing("students.grades")
确定某个集合的balance是开启或者关闭
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;
posted @ 2022-05-20 00:00  oldSimon  阅读(60)  评论(0编辑  收藏  举报