147. MongoDB Sharding Cluster 分片集群【MSC】

接上一篇博文整理—》

7. MongoDB Sharding Cluster 分片集群

7.1 规划

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

7.2 Shard节点配置过程

7.2.1 目录创建:

mkdir -p /mongodb/38021/conf  /mongodb/38021/log  /mongodb/38021/data
mkdir -p /mongodb/38022/conf  /mongodb/38022/log  /mongodb/38022/data
mkdir -p /mongodb/38023/conf  /mongodb/38023/log  /mongodb/38023/data
mkdir -p /mongodb/38024/conf  /mongodb/38024/log  /mongodb/38024/data
mkdir -p /mongodb/38025/conf  /mongodb/38025/log  /mongodb/38025/data
mkdir -p /mongodb/38026/conf  /mongodb/38026/log  /mongodb/38026/data

7.2.2 修改配置文件:

第一组复制集搭建:21-23(1主 1从 1Arb)

cat >  /mongodb/38021/conf/mongodb.conf  <<EOF
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: 10.0.0.51,127.0.0.1
  port: 38021
replication:
  oplogSizeMB: 2048
  replSetName: sh1	#复制集名称
sharding:
  clusterRole: shardsvr		#集群固定角色名
processManagement: 
  fork: true
EOF

\cp  /mongodb/38021/conf/mongodb.conf  /mongodb/38022/conf/
\cp  /mongodb/38021/conf/mongodb.conf  /mongodb/38023/conf/

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

第二组节点:24-26(1主1从1Arb)

cat > /mongodb/38024/conf/mongodb.conf <<EOF
systemLog:
  destination: file
  path: /mongodb/38024/log/mongodb.log   
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /mongodb/38024/data
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
net:
  bindIp: 10.0.0.51,127.0.0.1
  port: 38024
replication:
  oplogSizeMB: 2048
  replSetName: sh2
sharding:
  clusterRole: shardsvr
processManagement: 
  fork: true
EOF

\cp  /mongodb/38024/conf/mongodb.conf  /mongodb/38025/conf/
\cp  /mongodb/38024/conf/mongodb.conf  /mongodb/38026/conf/
    
sed 's#38024#38025#g' /mongodb/38025/conf/mongodb.conf -i
sed 's#38024#38026#g' /mongodb/38026/conf/mongodb.conf -i

7.2.3 启动所有节点,并搭建复制集

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  
ps -ef |grep mongod

[mongod@db01 ~]$ mongo --port 38021
use  admin
config = {_id: 'sh1', members: [
                          {_id: 0, host: '10.0.0.51:38021'},
                          {_id: 1, host: '10.0.0.51:38022'},
                          {_id: 2, host: '10.0.0.51:38023',"arbiterOnly":true}]
           }
==显示结果如下:================================================
{
	"_id" : "sh1",
	"members" : [
		{
			"_id" : 0,
			"host" : "10.0.0.51:38021"
		},
		{
			"_id" : 1,
			"host" : "10.0.0.51:38022"
		},
		{
			"_id" : 2,
			"host" : "10.0.0.51:38023",
			"arbiterOnly" : true
		}
	]
}

> rs.initiate(config)
{ "ok" : 1 }

  
mongo --port 38024 
use admin
config = {_id: 'sh2', members: [
                          {_id: 0, host: '10.0.0.51:38024'},
                          {_id: 1, host: '10.0.0.51:38025'},
                          {_id: 2, host: '10.0.0.51:38026',"arbiterOnly":true}]
           }
==显示结果如下:================================================ 
{
	"_id" : "sh2",
	"members" : [
		{
			"_id" : 0,
			"host" : "10.0.0.51:38024"
		},
		{
			"_id" : 1,
			"host" : "10.0.0.51:38025"
		},
		{
			"_id" : 2,
			"host" : "10.0.0.51:38026",
			"arbiterOnly" : true
		}
	]
}
 
> rs.initiate(config)
{ "ok" : 1 }

7.3 config节点配置

7.3.1 目录创建

mkdir -p /mongodb/38018/conf  /mongodb/38018/log  /mongodb/38018/data

mkdir -p /mongodb/38019/conf  /mongodb/38019/log  /mongodb/38019/data

mkdir -p /mongodb/38020/conf  /mongodb/38020/log  /mongodb/38020/data

7.3.2修改配置文件:

cat > /mongodb/38018/conf/mongodb.conf <<EOF
systemLog:
  destination: file
  path: /mongodb/38018/log/mongodb.conf
  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: 10.0.0.51,127.0.0.1
  port: 38018
replication:
  oplogSizeMB: 2048
  replSetName: configReplSet
sharding:
  clusterRole: configsvr	#集群配置节点的角色
processManagement: 
  fork: true
EOF

\cp /mongodb/38018/conf/mongodb.conf /mongodb/38019/conf/
\cp /mongodb/38018/conf/mongodb.conf /mongodb/38020/conf/
    
sed 's#38018#38019#g' /mongodb/38019/conf/mongodb.conf -i
sed 's#38018#38020#g' /mongodb/38020/conf/mongodb.conf -i

7.3.3启动节点,并配置复制集

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: '10.0.0.51:38018'},
                          {_id: 1, host: '10.0.0.51:38019'},
                          {_id: 2, host: '10.0.0.51:38020'}]
           }
==显示结果如下:1组两从复制集================================================  {
	"_id" : "configReplSet",
	"members" : [
		{
			"_id" : 0,
			"host" : "10.0.0.51:38018"
		},
		{
			"_id" : 1,
			"host" : "10.0.0.51:38019"
		},
		{
			"_id" : 2,
			"host" : "10.0.0.51:38020"
		}
	]
}
           
> rs.initiate(config)
==显示结果如下:================================================ 
{
	"ok" : 1,
	"operationTime" : Timestamp(1578630303, 1),
	"$gleStats" : {
		"lastOpTime" : Timestamp(1578630303, 1),
		"electionId" : ObjectId("000000000000000000000000")
	},
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578630303, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
  
  
注:configserver 可以是一个节点,官方建议复制集。configserver不能有arbiter。
新版本中,要求必须是复制集。
注:mongodb 3.4之后,虽然要求config server为replica set,但是不支持arbiter

7.4 mongos节点配置:

7.4.1创建目录:

mkdir -p /mongodb/38017/conf  /mongodb/38017/log 

7.4.2配置文件:

cat > /mongodb/38017/conf/mongos.conf <<EOF
systemLog:
  destination: file
  path: /mongodb/38017/log/mongos.log
  logAppend: true
net:
  bindIp: 10.0.0.51,127.0.0.1
  port: 38017
sharding:
  configDB: configReplSet/10.0.0.51:38018,10.0.0.51:38019,10.0.0.51:38020	#复制集
processManagement: 
  fork: true
EOF

7.4.3启动mongos

[mongod@db01 ~]$ mongos -f /mongodb/38017/conf/mongos.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 4272
child process started successfully, parent exiting

7.5 分片集群添加节点

 连接到其中一个mongos(10.0.0.51),做以下配置
(1)连接到mongs的admin数据库
# su - mongod
[mongod@db01 ~]$ mongo 10.0.0.51:38017/admin

(2)添加分片
mongos> db.runCommand( { addshard : "sh1/10.0.0.51:38021,10.0.0.51:38022,10.0.0.51:38023",name:"shard1"} )
==显示结果如下:================================================ 
{
	"shardAdded" : "shard1",
	"ok" : 1,
	"operationTime" : Timestamp(1578630429, 7),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578630429, 7),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

mongos> db.runCommand( { addshard : "sh2/10.0.0.51:38024,10.0.0.51:38025,10.0.0.51:38026",name:"shard2"} )
==显示结果如下:================================================ 
{
	"shardAdded" : "shard2",
	"ok" : 1,
	"operationTime" : Timestamp(1578630450, 6),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578630450, 6),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}3)列出分片
mongos> db.runCommand( { listshards : 1 } )
==显示结果如下:================================================ 
{
	"shards" : [
		{
			"_id" : "shard1",
			"host" : "sh1/10.0.0.51:38021,10.0.0.51:38022",
			"state" : 1
		},
		{
			"_id" : "shard2",
			"host" : "sh2/10.0.0.51:38024,10.0.0.51:38025",
			"state" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1578630472, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578630472, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}4)整体状态查看
mongos> sh.status();
==显示结果如下:================================================ 
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("5e17fcac2da5fd62a2420873")
  }
  shards:
        {  "_id" : "shard1",  "host" : "sh1/10.0.0.51:38021,10.0.0.51:38022",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "sh2/10.0.0.51:38024,10.0.0.51:38025",  "state" : 1 }
  active mongoses:
        "3.6.12" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

7.6 使用分片集群

7.6.1 RANGE分片配置及测试

1、激活数据库分片功能

1.登录mongo
mongo --port 38017 admin
use admin
admin>  ( { enablesharding : "数据库名称" } )
eg:
mongos> db.runCommand( { enablesharding : "test" } )
{
	"ok" : 1,
	"operationTime" : Timestamp(1578640496, 8),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578640496, 8),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

2、指定分片键对集合分片

1. 创建索引
use test
mongos> db.vast.ensureIndex( { id: 1 } )
{
	"raw" : {
		"sh2/10.0.0.51:38024,10.0.0.51:38025" : {
			"createdCollectionAutomatically" : true,
			"numIndexesBefore" : 1,
			"numIndexesAfter" : 2,
			"ok" : 1
		}
	},
	"ok" : 1,
	"operationTime" : Timestamp(1578640541, 3),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578640541, 3),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

2. 开启分片
mongos> use admin
switched to db admin
mongos> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
{
	"collectionsharded" : "test.vast",
	"collectionUUID" : UUID("fdd6b123-248f-4e89-a4fa-cdf476333053"),
	"ok" : 1,
	"operationTime" : Timestamp(1578640569, 10),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578640569, 10),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

3、集合分片验证

mongos> use test
switched to db test
mongos> for(i=1;i<1000000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }

test> db.vast.stats()

4、分片结果测试

shard1:
mongo --port 38021
db.vast.count();

shard2:
mongo --port 38024
db.vast.count();

7.6.2 Hash分片 实战:

对cheng库下的vast大表进行hash
创建哈希索引
(1)对于cheng开启分片功能
[mongod@db01 ~]$ mongo --port 38017 admin
use admin
开启分片策略
mongos> db.runCommand( { enablesharding : "cheng" } )
{
	"ok" : 1,
	"operationTime" : Timestamp(1578640812, 474),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578640812, 474),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}


(2)对于cheng库下的vast表建立hash索引
use cheng
mongos> db.vast.ensureIndex( { id: "hashed" } )
{
	"raw" : {
		"sh1/10.0.0.51:38021,10.0.0.51:38022" : {
			"createdCollectionAutomatically" : true,
			"numIndexesBefore" : 1,
			"numIndexesAfter" : 2,
			"ok" : 1
		}
	},
	"ok" : 1,
	"operationTime" : Timestamp(1578640843, 454),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578640843, 486),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}


(3)开启分片 
use admin
mongos> sh.shardCollection( "cheng.vast", { id: "hashed" } )
{
	"collectionsharded" : "cheng.vast",
	"collectionUUID" : UUID("7f8e023e-fe67-40b0-8c2b-0bc08bfc7a6a"),
	"ok" : 1,
	"operationTime" : Timestamp(1578640865, 354),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578640865, 354),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}


(4)录入10w行数据测试
use cheng
mongos> for(i=1;i<100000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
WriteResult({ "nInserted" : 1 })

mongos> db.vast.count();
999995hash分片结果测试
[mongod@db01 ~]$ mongo --port 38021
sh1:PRIMARY> use cheng
sh1:PRIMARY> db.vast.count();

[mongod@db01 ~]$ mongo --port 38024
sh1:PRIMARY> use cheng
sh1:PRIMARY> db.vast.count();

7.7 分片集群的查询及管理

7.7.1 判断是否Shard集群

[mongod@db01 ~]$ mongo --port 38017 admin
admin> db.runCommand({ isdbgrid : 1})

7.7.2 列出所有分片信息

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

7.7.3 列出开启分片的数据库

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

7.7.4 查看分片的片键

mongos> use config
switched to db config
mongos> db.collections.find().pretty()
{
	"_id" : "config.system.sessions",
	"lastmodEpoch" : ObjectId("5e17fd7b2da5fd62a2420d1a"),
	"lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
	"dropped" : false,
	"key" : {
		"_id" : 1
	},
	"unique" : false,
	"uuid" : UUID("9f1ed165-b1e0-45f5-a0d4-b117624e1bf3")
}
{
	"_id" : "test.vast",
	"lastmodEpoch" : ObjectId("5e1824b92da5fd62a242eecb"),
	"lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
	"dropped" : false,
	"key" : {
		"id" : 1
	},
	"unique" : false,
	"uuid" : UUID("fdd6b123-248f-4e89-a4fa-cdf476333053")
}
{
	"_id" : "cheng.vast",
	"lastmodEpoch" : ObjectId("5e1825e12da5fd62a242f6f1"),
	"lastmod" : ISODate("1970-02-19T17:02:47.297Z"),
	"dropped" : false,
	"key" : {
		"id" : "hashed"
	},
	"unique" : false,
	"uuid" : UUID("7f8e023e-fe67-40b0-8c2b-0bc08bfc7a6a")
}

7.7.5 查看分片的详细信息

admin> sh.status()

7.7.6 删除分片节点(谨慎)

(1)确认blance是否在工作
sh.getBalancerState()

(2)删除shard2节点(谨慎)
mongos> db.runCommand( { removeShard: "shard2" } )
注意:删除操作一定会立即触发blancer。

7.8 balancer操作

7.8.1 介绍

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

有需要时可以关闭和开启blancer(备份的时候)
mongos> sh.stopBalancer()
mongos> sh.startBalancer()

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

官方文档地址:https://docs.mongodb.com/manual/tutorial/manage-sharded-cluster-balancer/#schedule-the-balancing-window
// connect to mongos

[mongod@db01 ~]$ mongo --port 38017 admin
mongos> use config
switched to db config
mongos> sh.setBalancerState( true )
{
	"ok" : 1,
	"operationTime" : Timestamp(1578641744, 546),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1578641744, 546),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

mongos> db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "3:00", stop : "5:00" } } }, true )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
查看时间间隔    
mongos> sh.getBalancerWindow()
{ "start" : "3:00", "stop" : "5:00" }

sh.status() 查看时间间隔
Balancer active window is set between 3:00 and 5:00 server local time

关于集合的balancer(了解下)
关闭某个集合的balance
sh.disableBalancing("students.grades")
打开某个集合的balancer
sh.enableBalancing("students.grades")
确定某个集合的balance是开启或者关闭
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;
发布了148 篇原创文章 · 获赞 65 · 访问量 7587

猜你喜欢

转载自blog.csdn.net/chengyinwu/article/details/103929409