MongoDB-ShardingCluster

分片集群架构

默认分片规则:

初始1个chunk
缺省chunk大小:64MB
MongoDB:自动拆分并迁移chunk

默认是在存储数据时先在第一个节点开辟一个chunk开始存数据,当存满了以后会分裂出一个新的chunk继续存储。

在MongoDB空闲时,均衡器(balancer)会自动把第一个节点的数据进行对所有节点平均分配,也就是迁移chunk。

环境规划

# 十个实例
(1)configserver:38018-38020
3台构成的复制集(1主两从,不支持arbiter)38018-38020(复制集名字configsvr)
(2)shard节点:
sh1:38021-23(1主2从,其中一个节点为arbiter,复制集名字sh1)
sh2:38024-26(1主2从,其中一个节点为arbiter,复制集名字sh2)
(3)mongos:
38017

分片集群搭建

shard节点
# 目录规划
mkdir -p /mongodb/{38021..38026}/{conf,log,data}

# 第一组复制集节点:21-23(1主1从1aribiter)
systemLog:
  destination: file
  path: /mongodb/38021/log/mongodb.log
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /mongodb/38021/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: 127.0.0.1,172.19.232.224
  port: 38021
replication:
  oplogSizeMB: 2048
  replSetName: sh1
sharding:
  clusterRole: shardsvr
processManagement:
  fork: true
  
\cp /mongodb/38021/conf/mongodb.conf /mongodb/38022/conf
\cp /mongodb/38021/conf/mongodb.conf /mongodb/38023/conf

sed -ri 's/38021/38022/g' /mongodb/38022/conf/mongodb.conf
sed -ri 's/38021/38023/g' /mongodb/38022/conf/mongodb.conf

# 第二组复制集节点:24-26(1主1从1aribiter)
systemLog:
  destination: file
  path: /mongodb/38024/log/mongodb.log
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /mongodb/38024/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: 127.0.0.1,172.19.232.224
  port: 38024
replication:
  oplogSizeMB: 2048
  replSetName: sh2
sharding:
  clusterRole: shardsvr
processManagement:
  fork: true
  
\cp /mongodb/38024/conf/mongodb.conf /mongodb/38025/conf
\cp /mongodb/38024/conf/mongodb.conf /mongodb/38026/conf

sed -ri 's/38024/38025/g' /mongodb/38025/conf/mongodb.conf
sed -ri 's/38024/38026/g' /mongodb/38026/conf/mongodb.conf

# 启动所有节点,搭建复制集
su - mongodb
mongod -f /mongodb/38021/conf/mongodb.conf 
mongod -f /mongodb/38022/conf/mongodb.conf 
mongod -f /mongodb/38023/conf/mongodb.conf 
mongod -f /mongodb/38024/conf/mongodb.conf 
mongod -f /mongodb/38025/conf/mongodb.conf 
mongod -f /mongodb/38026/conf/mongodb.conf

mongo --port 38021
use admin
config = {_id: 'sh1', members: [
	{_id: 0, host: '172.19.232.224:38021'},
	{_id: 1, host: '172.19.232.224:38022'},
	{_id: 2, host: '172.19.232.224:38023', "arbiterOnly": true}]
}
rs.initiate(config)

mongo --port 38024
use admin
config = {_id: 'sh2', members: [
	{_id: 0, host: '172.19.232.224:38024'},
	{_id: 1, host: '172.19.232.224:38025'},
	{_id: 2, host: '172.19.232.224:38026', "arbiterOnly": true}]
}
rs.initiate(config)
config节点
# 目录规划
mkdir -p /mongodb/{38018..38020}/{conf,log,data}

# 配置文件
systemLog:
  destination: file
  path: /mongodb/38018/log/mongodb.log
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /mongodb/38018/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: 127.0.0.1,172.19.232.224
  port: 38018
replication:
  oplogSizeMB: 2048
  replSetName: configReplSet
sharding:
  clusterRole: configsvr
processManagement:
  fork: true
  
\cp /mongodb/38018/conf/mongodb.conf /mongodb/38019/conf
\cp /mongodb/38018/conf/mongodb.conf /mongodb/38020/conf

sed -ri 's/38018/38019/g' /mongodb/38019/conf/mongodb.conf
sed -ri 's/38018/38020/g' /mongodb/38020/conf/mongodb.conf

# 启动节点,配置复制集
su - mongodb
mongod -f /mongodb/38018/conf/mongodb.conf 
mongod -f /mongodb/38019/conf/mongodb.conf 
mongod -f /mongodb/38020/conf/mongodb.conf

mongo --port 38018
use admin
config = {_id: 'configReplSet', members: [
	{_id: 0, host: '172.19.232.224:38018'},
	{_id: 1, host: '172.19.232.224:38019'},
	{_id: 2, host: '172.19.232.224:38020'} ]
}
rs.initiate(config)

注:configserve可以是一个节点,官方建议复制集。configserver不能有aribiter。
新版本中,要求必须是复制集
注:mongodb 3.4之后,虽然要求config server为relica set,但是不支持aribiter
mongos节点
# 目录规划
mkdir -p /mongodb/38017/{conf,log}

# 配置文件
systemLog:
  destination: file
  path: /mongodb/38017/log/mongos.log
  logAppend: true
net:
  bindIp: 127.0.0.1,172.19.232.224
  port: 38017
sharding:
  configDB: configReplSet/172.19.232.224:38018,172.19.232.224:38019,172.19.232.224:38020
processManagement:
  fork: true
  
# 启动mongos
su - mongodb
mongos -f /mongodb/38017/conf/mongos.conf

# 分片集群添加节点
连接到其中一个mongos(172.19.232.224),做以下配置
1.连接到mongs的admin数据库
su - mongodb
mongo 172.19.232.224:38017/admin
2.添加分片
db.runCommand({ addshard : "sh1/172.19.232.224:38021,172.19.232.224:38022,172.19.232.224:38023",name:"shard1"})
db.runCommand({ addshard : "sh2/172.19.232.224:38024,172.19.232.224:38025,172.19.232.224:38026",name:"shard2"})
3.列出分片
db.runCommand( { listshards : 1 } )
4.整体状态查看
sh.status()

使用分片集群

RANGE自动分片
# 1.激活数据库分片功能
mongo --port 38017 admin
mongos> ( { enablesharding : "test1" } )
mongos> db.runCommand( { enablesharding : "test1" } )

# 2.指定分片键对集合分片
# 创建索引
mongos> use test1
mongos> db.tab1.ensureIndex( { id: 1 } )

# 开启分片
mongos> use admin
mongos> db.runCommand( { shardcollection : "test1.tab1",key : {id: 1} } )

# 3.集合分片验证
mongos> use test1
mongos> for(i=1;i<10000;i++){ db.tab1.insert({"id":1,"name":"cai","age":21,"date":new Date()});}
mongos> db.tab1.stats()
zone方式进行range手工定制分片
mongo --port 38017 admin
mongos> use zonedb
mongos> db.vast.ensureIndex( {order_id: 1})

# 对于zonedb开启分片功能
mongos> use admin
mongos> db.runCommand( { enablesharding : "zonedb" } )
mongos> sh.shardCollection("zonedb.vast", {order_id: 1});

mongos> sh.addShardTag("shard1", "shard00")
mongos> sh.addShardTag("shard2", "shard01")

mongos> sh.addTagRange(
"zonedb.vast",
{ "order_id" : MinKey },
{ "order_id" : 500 },"shard00" )

mongos> sh.addTagRange(
"zonedb.vast",
{ "order_id" : 501 },
{ "order_id" : MaxKey },"shard01" )

use zonedb
for(i=1;i<1000;i++){ db.vast.insert({"id":1,"name":"ceshi","age":110,"date":new Date()});}
db.vast.getShardDistribution()
Hash分片
对hashdb库下的vast大表进行hash
创建哈希索引
(1)对于hashdb开启分片功能
mongo --port 38017 admin
use admin
admin> db.runCommand( { enablesharding : "hashdb" } )

(2)对于hashdb库下的vast表建立hash索引
use hashdb
hashdb> db.vast.ensureIndex( { id: "hashed" } )

(3)开启分片
use admin
admin> sh.shardCollection( "hashdb.vast", { id: "hashed" } )

(4)录入10w行数据测试
use hashdb
for(i=1;i<100000;i++){ db.vast.insert({"id":1,"name":"guangzhou","age":100,"date":new Date()}); }

(5)hash分片结果测试
mongo --port 38021
use hashdb
db.vast.count()

mongo --port 38024
use hashdb
db.vast.count()

分片集群查询及管理

# 1.判断是否Shard集群
admin> db.runCommand({ isdbgrid : 1})

# 2.列出所有分片信息
admin> db.runCommand({ listshards : 1})

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

# 4.查看分片的片键
config> db.collections.find().pretty()

# 5.查看分片的详细信息
admin> sh.status()

# 6.删除分片节点
(1) 确认blance是否再工作
sh.getBalancerState()
(2) 删除shard2节点
mongos> db.runCommand( { removeShard: "shard2" } )
注意:删除操作一定会立即出发blancer

Balancer

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

开启和关闭balancer
mongos> sh.stopBalancer()
mongos> sh.startBalancer()
自定义 自动平衡进行的时间段
use config
sh.setBalancerState( true )
db.settings.update({ _id : "balancer"}, { $set : { activeWindow : { start : "3:00", stop, "5:00" } } }, true)

sh.getBalancerWindow()
sh.status()
posted @ 2022-02-15 11:21  Cai_HL  阅读(28)  评论(0)    收藏  举报
>