16.MongoDB系列之分片管理
1. 查看当前状态
1.1 查看配置信息
mongos> use config
// 查看分片
mongos> db.shards.find()
{ "_id" : "study", "host" : "study/localhost:27018,localhost:27019,localhost:27020", "state" : 1 }
// 查看分片集合信息
mongos> db.collections.find().pretty()
{
"_id" : "config.system.sessions",
"lastmodEpoch" : ObjectId("62db964a7decd7b3c3f36bc1"),
"lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
"dropped" : false,
"key" : {
"_id" : 1
},
"unique" : false,
"uuid" : UUID("5351f3f9-63e3-44b8-af9a-a0261e681bef")
}
// 查询集合中块信息
mongos> db.chunks.find().pretty()
{
"_id" : "config.system.sessions-_id_MinKey",
"ns" : "config.system.sessions",
"min" : {
"_id" : { "$minKey" : 1 }
},
"max" : {
"_id" : { "$maxKey" : 1 }
},
"shard" : "study",
"lastmod" : Timestamp(1, 0),
"lastmodEpoch" : ObjectId("62db964a7decd7b3c3f36bc1"),
"history" : [
{
"validAfter" : Timestamp(1658558026, 4),
"shard" : "study"
}
]
}
// 跟踪集群拆分和迁移情况
mongos> db.changelog.find({'what': 'split'}).pretty()
2. 跟踪网络连接
mongos> db.adminCommand({'connPoolStats':1})
{
"numClientConnections" : 0,
"numAScopedConnections" : 0,
"totalInUse" : 0,
"totalAvailable" : 9,
"totalCreated" : 14,
"totalRefreshing" : 0,
"pools" : {
"NetworkInterfaceTL-ShardRegistry" : {
"poolInUse" : 0,
"poolAvailable" : 3,
"poolCreated" : 8,
"poolRefreshing" : 0,
"localhost:27038" : {
"inUse" : 0,
"available" : 1,
"created" : 2,
"refreshing" : 0
},
"localhost:27039" : {
"inUse" : 0,
"available" : 1,
"created" : 5,
"refreshing" : 0
},
"localhost:27040" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
}
},
"NetworkInterfaceTL-TaskExecutorPool-0" : {
"poolInUse" : 0,
"poolAvailable" : 6,
"poolCreated" : 6,
"poolRefreshing" : 0,
"localhost:27018" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27019" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27020" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27038" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27039" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27040" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
}
}
},
"hosts" : {
"localhost:27018" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27019" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27020" : {
"inUse" : 0,
"available" : 1,
"created" : 1,
"refreshing" : 0
},
"localhost:27038" : {
"inUse" : 0,
"available" : 2,
"created" : 3,
"refreshing" : 0
},
"localhost:27039" : {
"inUse" : 0,
"available" : 2,
"created" : 6,
"refreshing" : 0
},
"localhost:27040" : {
"inUse" : 0,
"available" : 2,
"created" : 2,
"refreshing" : 0
}
},
"replicaSets" : {
"configRS" : {
"hosts" : [
{
"addr" : "localhost:27038",
"ok" : true,
"ismaster" : false,
"hidden" : false,
"secondary" : true,
"pingTimeMillis" : 0
},
{
"addr" : "localhost:27039",
"ok" : true,
"ismaster" : true,
"hidden" : false,
"secondary" : false,
"pingTimeMillis" : 0
},
{
"addr" : "localhost:27040",
"ok" : true,
"ismaster" : false,
"hidden" : false,
"secondary" : true,
"pingTimeMillis" : 0
}
]
},
"study" : {
"hosts" : [
{
"addr" : "localhost:27018",
"ok" : true,
"ismaster" : false,
"hidden" : false,
"secondary" : true,
"pingTimeMillis" : 0,
"tags" : {
"dc" : "sh"
}
},
{
"addr" : "localhost:27019",
"ok" : true,
"ismaster" : true,
"hidden" : false,
"secondary" : false,
"pingTimeMillis" : 0,
"tags" : {
"dc" : "sh"
}
},
{
"addr" : "localhost:27020",
"ok" : true,
"ismaster" : false,
"hidden" : false,
"secondary" : true,
"pingTimeMillis" : 0,
"tags" : {
"dc" : "bj"
}
}
]
}
},
"ok" : 1,
"operationTime" : Timestamp(1658569778, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1658569778, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
可以在命令行配置 --maxConns选项来限制其可以创建的连接数
2. 服务器管理
2.1 添加服务器
可以在任何时候添加新的mongos进程。
可以使用addShard命令添加新的分片
2.2 修改分片中的服务器
要更改一个分片的成员,需要直接连接到该分片的主节点(而不是通过mongos),并重新配置副本集。集群配置会监测到变更并自动更新config.shards.不要手动修改config.shards
2.3 删除分片
> db.adminCommand({'removeShard': 'study'})
当有数据库将被移除的分片作为其主分片,则会提示you need to drop or movePrimary these databases
此时先将此数据库移动到其他分片, 在执行删除即可
> db.adminCommand({'movePrimary': 'video', to: 'shard01'})
> db.adminCommand({'removeShard': 'study'})
3. 数据均衡
3.1 均衡器
关闭均衡器是大部分管理操作的先决条件。
// 关闭均衡器
mongos> sh.setBalancerState(false)
// 检查是否仍有均衡过程正在进行
mongos> db.locks.find({'_id': 'balancer'})['state']
mongos> sh.setBalancerState(true)
// 设置均衡时间窗口
mongos> db.settings.update({_id: "balancer"}, {$set: {"activeWindow": {start: "13:00", stop: "16:00"}}}, {upsert:true})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "balancer" })
3.2 修改块的大小
// 修改块大小为32M
mongos> db.settings.save({'_id': 'chunksize', 'value': 32})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
3.3 移动块
> sh.moveChunk('video.movies', {imdId: 50000}, "shard0002")
3.4 超大块
当一个块大于config.settings中所设置的最大块大小时,均衡器就不允许移动这个块了。这些不可拆分、不可移动的块被称为超大块
// 检测超大块,其会被标记有jumbo属性
> sh.status()
...
{"x": 5} -->> {"x":6} on : shard0001 jumbo
...
// 拆分超大块,需要先找出片键的拆分点
> sh.splitAt('video.movies', {'imdId': NumberLong('7000000000000000000000')})
对于分发超大块,可以关闭均衡器,临时设置块大小大于该超大块,然后手工移动该块,后恢复块大小及启动均衡器。为了避免超大块,可以修改片键使其具有更小的粒度,如年月日片键,修改为年月日小时分钟秒。
4. 刷新配置
当mongos不能从配置服务器正确更新配置,则可以
> db.adminCommand({'flushRouterConfig': 1})
如果还没有解决,则需要重新启动所有的mongos或mongod进程以清除所有缓存数据