MongoDB4.0 WINDOWS环境下 副本集分片部署

Posted zwz418

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB4.0 WINDOWS环境下 副本集分片部署相关的知识,希望对你有一定的参考价值。

部署开始:

创建路径 D:Program FilesMongoDBMySet下

config Data log 文件夹

config文件夹下准备配置文件:

分片1的副本集1

storage:
  dbPath: D:/Program Files/MongoDB/MySet/Data/shard11
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path: D:/Program Files/MongoDB/MySet/log/shard11.log
net:
  port: 13211
  bindIp: 192.168.33.52

replication:
  oplogSizeMB: 10240
  replSetName: shard1

sharding:
  clusterRole: shardsvr

分片1的副本集2

storage:
  dbPath: D:Program FilesMongoDBMySetDatashard12
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogshard12.log
net:
  port: 13212
  bindIp: 192.168.33.52

replication:
  oplogSizeMB: 10240
  replSetName: shard1

sharding:
  clusterRole: shardsvr

分片1的副本集3

storage:
  dbPath: D:Program FilesMongoDBMySetDatashard13
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogshard13.log
net:
  port: 13213
  bindIp: 192.168.33.52

replication:
  oplogSizeMB: 10240
  replSetName: shard1

sharding:
  clusterRole: shardsvr

分片2的副本集1

storage:
  dbPath: D:Program FilesMongoDBMySetDatashard21
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogshard21.log
net:
  port: 13221
  bindIp: 192.168.33.52

replication:
  oplogSizeMB: 10240
  replSetName: shard2

sharding:
  clusterRole: shardsvr

分片2的副本集2

storage:
  dbPath: D:Program FilesMongoDBMySetDatashard22
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogshard22.log
net:
  port: 13222
  bindIp: 192.168.33.52

replication:
  oplogSizeMB: 10240
  replSetName: shard2

sharding:
  clusterRole: shardsvr

分片2的副本集3

storage:
  dbPath: D:Program FilesMongoDBMySetDatashard23
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogshard23.log
net:
  port: 13223
  bindIp: 192.168.33.52

replication:
  oplogSizeMB: 10240
  replSetName: shard2

sharding:
  clusterRole: shardsvr

配置节点1

storage:
  dbPath: D:Program FilesMongoDBMySetDataconfig1
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogconfig1.log
net:
  port: 13201
  bindIp: 192.168.33.52
  
replication:
  oplogSizeMB: 10240
  replSetName: rscfg

sharding:
  clusterRole: configsvr

配置节点2

storage:
  dbPath: D:Program FilesMongoDBMySetDataconfig2
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogconfig2.log
net:
  port: 13202
  bindIp: 192.168.33.52
  
replication:
  oplogSizeMB: 10240
  replSetName: rscfg

sharding:
  clusterRole: configsvr

配置节点3

storage:
  dbPath: D:Program FilesMongoDBMySetDataconfig3
  journal:
    enabled: true
systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlogconfig3.log
net:
  port: 13203
  bindIp: 192.168.33.52
  
replication:
  oplogSizeMB: 10240
  replSetName: rscfg

sharding:
  clusterRole: configsvr

路由节点

systemLog:
  destination: file
  logAppend: true
  path:  D:Program FilesMongoDBMySetlog
oute.log
net:
  port: 13200
  bindIp: 192.168.33.52

sharding:
  configDB: rscfg/192.168.33.52:13201,192.168.33.52:13202,192.168.33.52:13203

配置文件准备完成,打开一个cmd窗口

切换到mongo的bin路径

cd /d D://

cd D:Program FilesMongoDBServer4.0in

找到配置文件地址进行服务的安装,服务名和服务展示名都需要加上,不然会提示服务名已存在,分片11,12,13,21,22,23,配置1,2,3 这9个节点都是由mongod进程寄宿,路由节点由mongos进程寄宿

mongod -config "D:Program FilesMongoDBMySetconfigshard11.cfg" --serviceName "MongoDB_Shard11" --install --serviceDisplayName "MongoDB_Shard11"
mongod -config "D:Program FilesMongoDBMySetconfigshard12.cfg" --serviceName "MongoDB_Shard12" --install --serviceDisplayName "MongoDB_Shard12"
mongod -config "D:Program FilesMongoDBMySetconfigshard13.cfg" --serviceName "MongoDB_Shard13" --install --serviceDisplayName "MongoDB_Shard13"

mongod -config "D:Program FilesMongoDBMySetconfigshard21.cfg" --serviceName "MongoDB_Shard21" --install --serviceDisplayName "MongoDB_Shard21"
mongod -config "D:Program FilesMongoDBMySetconfigshard22.cfg" --serviceName "MongoDB_Shard22" --install --serviceDisplayName "MongoDB_Shard22"
mongod -config "D:Program FilesMongoDBMySetconfigshard23.cfg" --serviceName "MongoDB_Shard23" --install --serviceDisplayName "MongoDB_Shard23"

mongod -config "D:Program FilesMongoDBMySetconfigconfig1.cfg" --serviceName "MongoDB_Config1" --install --serviceDisplayName "MongoDB_Config1"
mongod -config "D:Program FilesMongoDBMySetconfigconfig2.cfg" --serviceName "MongoDB_Config2" --install --serviceDisplayName "MongoDB_Config2"
mongod -config "D:Program FilesMongoDBMySetconfigconfig3.cfg" --serviceName "MongoDB_Config3" --install --serviceDisplayName "MongoDB_Config3"

mongos -config "D:Program FilesMongoDBMySetconfig oute.cfg" --serviceName "MongoDB_Route" --install --serviceDisplayName "MongoDB_Route"

所有节点都安装完成后,找到log文件夹下的log文件,如果出现提示,当前可以使用命令行“XXXXX”启动服务,那就成功安装服务了

开启服务,shard1分片的副本集群

net start MongoDB_Shard11
net start MongoDB_Shard12
net start MongoDB_Shard13

开起之后需要进行初始化以及副本集设置

cd /d D://
cd D:Program FilesMongoDBServer4.0in

连接到副本集的任意一个节点
mongo 192.168.33.52:13211

初始化副本集
rs.initiate( {_id : "shard1",members: [{ _id: 0, host: "192.168.33.52:13211" },{ _id: 1, host: "192.168.33.52:13212" },{ _id: 2, host: "192.168.33.52:13213" }]})

查看副本集初始化状态

rs.conf()

查看副本集是否是主节点
db.isMaster()

退出当前查询窗口
exit

shard2副本集相关设置,同上

net start MongoDB_Shard21
net start MongoDB_Shard22
net start MongoDB_Shard23

cd /d D://
cd D:Program FilesMongoDBServer4.0in
mongo 192.168.33.52:13221
rs.initiate( {_id : "shard2",members: [{ _id: 0, host: "192.168.33.52:13221" },{ _id: 1, host: "192.168.33.52:13222" },{ _id: 2, host: "192.168.33.52:13223" }]})

rs.conf()
db.isMaster()
exit

配置节点副本集相关设置(新版本的配置节点不能使用mirro模式,需要也形成副本集才能由路由节点调用),同上

net start MongoDB_Config1
net start MongoDB_Config2
net start MongoDB_Config3

cd /d D://
cd D:Program FilesMongoDBServer4.0in
mongo 192.168.33.52:13201
rs.initiate( {_id : "rscfg",members: [{ _id: 0, host: "192.168.33.52:13201" },{ _id: 1, host: "192.168.33.52:13202" },{ _id: 2, host: "192.168.33.52:13203" }]})

rs.conf()
db.isMaster()
exit

开启路由节点的服务

net start MongoDB_Route

cd /d D://
cd D:Program FilesMongoDBServer4.0in

连接路由节点
mongo 192.168.33.52:13200

配置分片shard1,shard2

use admin
db.runCommand({addshard:"shard1/192.168.33.52:13211,192.168.33.52:13212,192.168.33.52:13213"})
db.runCommand({addshard:"shard2/192.168.33.52:13221,192.168.33.52:13222,192.168.33.52:13223"})

查看一下分片列表,确认分片是否配置成功

 

db.runCommand({ listshards:1 })

对Monitor库启用分片
db.runCommand({enablesharding:"Monitor"})

对多张表设置片键,片键设置模式为 第一个字段为大粒度的局部字段比如类型、月份,第二个字段为常查询字段,粒度细,能够增加随机度

这里设置的前提为未创建表,设置片键后会自动创建索引,已有表,字段又无索引的会提示错误

db.runCommand( { shardcollection : "Monitor.LoadTable",key : {server_type: 1,time:1}} )
db.runCommand( { shardcollection : "Monitor.LogInfo",key : {server_type: 1,time:1}} )
db.runCommand( { shardcollection : "Monitor.LogError",key : {server_type: 1,time:1}} )
db.runCommand( { shardcollection : "Monitor.RequestLog",key : {server_type: 1,time:1}} )

到此为止配置完成,可连接路由节点使用,未测试failover是否生效

删除服务的语句,预留以防需要重新部署

sc delete MongoDB_Shard11
sc delete MongoDB_Shard12
sc delete MongoDB_Shard13

sc delete MongoDB_Shard21
sc delete MongoDB_Shard22
sc delete MongoDB_Shard23

sc delete MongoDB_Config1
sc delete MongoDB_Config2
sc delete MongoDB_Config3

sc delete MongoDB_Route


停止服务的语句,预留以防需要重新部署

net stop MongoDB_Shard11
net stop MongoDB_Shard12
net stop MongoDB_Shard13

net stop MongoDB_Shard21
net stop MongoDB_Shard22
net stop MongoDB_Shard23

net stop MongoDB_Config1
net stop MongoDB_Config2
net stop MongoDB_Config3

net stop MongoDB_Route

以上是关于MongoDB4.0 WINDOWS环境下 副本集分片部署的主要内容,如果未能解决你的问题,请参考以下文章

CentOS7.4搭建基于用户认证的MongoDB4.0三节点副本集集群

mongodb4.0.2 复制集主从部署

mongodb4.0分片集群安装

Mongodb副本集和分片

Windows上的MongoDB副本集面临一些基本问题

创建MongoDB副本集教程