1.概念
MongoDB Cluster集群由3个角色组成:
Router Server(路由服务器):接收client请求,从config server找到对应的在data server对应的分片、分块数据。使用mongos脚本命令启动
Config Server(配置服务器):找到data server的数据映射。使用mongod脚本命令启动
Data Server(数据服务器、多个副本集[具有选举功能的主从集群]):副本集和副本集之间存储的内容不同,每个副本集的数据节点存储的数据是一样的,每个数据节点是由多个被切片之后的chunk(块)组成。使用mongod脚本命令启动
2.部署
a.数据服务器配置:
配置一个副本集:
node01的配置文件mongodb.cfg
# 数据文件位置
dbpath=/root/mongodb/datasvr/rs1/node01/data
# 日志文件位置
logpath=/root/mongodb/datasvr/rs1/node01/logs/mongodb.log
# 以追加的方式写入日志
logappend=true
# 以守护进程方式运行
fork=true
bind_ip=192.168.179.10
port=27003
#副本集名称
replSet=rs001
#副本集支持
shardsvr=true
node02的配置文件mongodb.cfg
# 数据文件位置
dbpath=/root/mongodb/datasvr/rs1/node02/data
# 日志文件位置
logpath=/root/mongodb/datasvr/rs1/node02/logs/mongodb.log
# 以追加的方式写入日志
logappend=true
# 以守护进程方式运行
fork=true
bind_ip=192.168.179.10
port=27004
#副本集名称
replSet=rs001
#副本集支持
shardsvr=true
node03的配置文件mongodb.cfg
# 数据文件位置
dbpath=/root/mongodb/datasvr/rs1/node03/data
# 日志文件位置
logpath=/root/mongodb/datasvr/rs1/node03/logs/mongodb.log
# 以追加的方式写入日志
logappend=true
# 以守护进程方式运行
fork=true
bind_ip=192.168.179.10
port=27005
#副本集名称
replSet=rs001
#副本集支持
shardsvr=true
3个节点构成一个副本集,启动副本集
mongod -f /root/mongodb/datasvr/rs1/node01/mongodb.cfg
mongod -f /root/mongodb/datasvr/rs1/node02/mongodb.cfg
mongod -f /root/mongodb/datasvr/rs1/node03/mongodb.cfg
连接副本集,只需连接其中一台即可
mongo 192.168.179.10:27003
配置副本集
use admin
cfg={_id:"rs001",members:[{_id:0,host:"192.168.179.10:27003"},{_id:1,host:"192.168.179.10:27004"},{_id:2,host:"192.168.179.10:27005"}]}
rs.initiate(cfg);
配置完成后,查看副本集状态:
rs.status();
{
"set" : "rs001",
"date" : ISODate("2019-07-28T06:16:12.879Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.179.10:27003",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 384,
"optime" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-28T06:16:04Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1564294513, 1),
"electionDate" : ISODate("2019-07-28T06:15:13Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.179.10:27004",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 70,
"optime" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-28T06:16:04Z"),
"optimeDurableDate" : ISODate("2019-07-28T06:16:04Z"),
"lastHeartbeat" : ISODate("2019-07-28T06:16:11.343Z"),
"lastHeartbeatRecv" : ISODate("2019-07-28T06:16:11.039Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.179.10:27003",
"syncSourceHost" : "192.168.179.10:27003",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.179.10:27005",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 70,
"optime" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1564294564, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-28T06:16:04Z"),
"optimeDurableDate" : ISODate("2019-07-28T06:16:04Z"),
"lastHeartbeat" : ISODate("2019-07-28T06:16:11.270Z"),
"lastHeartbeatRecv" : ISODate("2019-07-28T06:16:10.996Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.179.10:27003",
"syncSourceHost" : "192.168.179.10:27003",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
可见,端口为27003的 主机被分配为PRIMARY,其他两台为SECONDARY
b.配置服务器配置:
配置路由服务器集群:
node01的配置文件mongodb.cfg
dbpath=/root/mongodb/configsvr/node01/data
logpath=/root/mongodb/configsvr/node01/logs/mongodb.log
logappend=true
fork=true
bind_ip=192.168.179.10
port=28001
configsvr=true
replSet=configsvr
node02的配置文件mongodb.cfg
dbpath=/root/mongodb/configsvr/node02/data
logpath=/root/mongodb/configsvr/node02/logs/mongodb.log
logappend=true
fork=true
bind_ip=192.168.179.10
port=28002
configsvr=true
replSet=configsvr
node03的配置文件mongodb.cfg
dbpath=/root/mongodb/configsvr/node03/data
logpath=/root/mongodb/configsvr/node03/logs/mongodb.log
logappend=true
fork=true
bind_ip=192.168.179.10
port=28003
configsvr=true
replSet=configsvr
启动路由集群
mongod -f /root/mongodb/configsvr/node01/mongodb.cfg
mongod -f /root/mongodb/configsvr/node02/mongodb.cfg
mongod -f /root/mongodb/configsvr/node03/mongodb.cfg
连接路由集群:
mongo 192.168.179.10:28001
配置路由服务器:
use admin
cfg={_id:"configsvr",members:[{_id:0,host:"192.168.179.10:28001"},{_id:1,host:"192.168.179.10:28002"},{_id:2,host:"192.168.179.10:28003"}]}
rs.initate(cfg);
配置完成后,查看状态:
rs.status();
{
"set" : "configsvr",
"date" : ISODate("2019-07-28T06:20:29.148Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.179.10:28001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 190,
"optime" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-28T06:20:21Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1564294786, 1),
"electionDate" : ISODate("2019-07-28T06:19:46Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.179.10:28002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 53,
"optime" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-28T06:20:21Z"),
"optimeDurableDate" : ISODate("2019-07-28T06:20:21Z"),
"lastHeartbeat" : ISODate("2019-07-28T06:20:28.242Z"),
"lastHeartbeatRecv" : ISODate("2019-07-28T06:20:29.123Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.179.10:28001",
"syncSourceHost" : "192.168.179.10:28001",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.179.10:28003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 53,
"optime" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1564294821, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-07-28T06:20:21Z"),
"optimeDurableDate" : ISODate("2019-07-28T06:20:21Z"),
"lastHeartbeat" : ISODate("2019-07-28T06:20:28.237Z"),
"lastHeartbeatRecv" : ISODate("2019-07-28T06:20:28.934Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.179.10:28001",
"syncSourceHost" : "192.168.179.10:28001",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1564294821, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1564294775, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"$clusterTime" : {
"clusterTime" : Timestamp(1564294821, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
c.路由服务器配置:
node01节点配置文件mongodb.cfg
# 关联配置服务器
configdb=configsvr/192.168.179.10:28001,192.168.179.10:28002,192.168.179.10:28003
# 日志文件位置
logpath=/root/mongodb/routersvr/node01/logs/mongodb.log
# 以追加的方式写入日志
logappend=true
# 以守护进程方式运行
fork=true
bind_ip=192.168.179.10
port=30000
启动路由服务器:
mongos -f /root/mongodb/routersvr/node01/mongodb.cfg
配置路由服务器:
连接服务器
mongo 192.168.179.10:30000
配置服务器
关联数据服务器
sh.addShard("rs001/192.168.179.10:27003");
use yalong
sh.enableSharding("yalong");
sh.shardCollection("yalong.mycollection",{name:"hashed"});
for(var i=1;i<=100;i++) db.mycollection.insert(name:"yalong"+i,age:i});
查询插入的数据
db.mycollection.find();
{ "_id" : ObjectId("5d3d5cc663819fca88797076"), "name" : "yalong1", "age" : 1 }
{ "_id" : ObjectId("5d3d5cc663819fca88797077"), "name" : "yalong2", "age" : 2 }
{ "_id" : ObjectId("5d3d5cc663819fca88797078"), "name" : "yalong3", "age" : 3 }
{ "_id" : ObjectId("5d3d5cc663819fca88797079"), "name" : "yalong4", "age" : 4 }
{ "_id" : ObjectId("5d3d5cc663819fca8879707a"), "name" : "yalong5", "age" : 5 }
{ "_id" : ObjectId("5d3d5cc663819fca8879707b"), "name" : "yalong6", "age" : 6 }
{ "_id" : ObjectId("5d3d5cc663819fca8879707c"), "name" : "yalong7", "age" : 7 }
{ "_id" : ObjectId("5d3d5cc663819fca8879707d"), "name" : "yalong8", "age" : 8 }
{ "_id" : ObjectId("5d3d5cc663819fca8879707e"), "name" : "yalong9", "age" : 9 }
{ "_id" : ObjectId("5d3d5cc663819fca8879707f"), "name" : "yalong10", "age" : 10 }
{ "_id" : ObjectId("5d3d5cc663819fca88797080"), "name" : "yalong11", "age" : 11 }
{ "_id" : ObjectId("5d3d5cc663819fca88797081"), "name" : "yalong12", "age" : 12 }
{ "_id" : ObjectId("5d3d5cc663819fca88797082"), "name" : "yalong13", "age" : 13 }
{ "_id" : ObjectId("5d3d5cc663819fca88797083"), "name" : "yalong14", "age" : 14 }
{ "_id" : ObjectId("5d3d5cc663819fca88797084"), "name" : "yalong15", "age" : 15 }
{ "_id" : ObjectId("5d3d5cc663819fca88797085"), "name" : "yalong16", "age" : 16 }
{ "_id" : ObjectId("5d3d5cc663819fca88797086"), "name" : "yalong17", "age" : 17 }
{ "_id" : ObjectId("5d3d5cc663819fca88797087"), "name" : "yalong18", "age" : 18 }
{ "_id" : ObjectId("5d3d5cc663819fca88797088"), "name" : "yalong19", "age" : 19 }
{ "_id" : ObjectId("5d3d5cc663819fca88797089"), "name" : "yalong20", "age" : 20 }
Type "it" for more
.
.
.
{ "_id" : ObjectId("5d3d5cc663819fca887970bd"), "name" : "yalong72", "age" : 72 }
{ "_id" : ObjectId("5d3d5cc663819fca887970be"), "name" : "yalong73", "age" : 73 }
{ "_id" : ObjectId("5d3d5cc663819fca887970bf"), "name" : "yalong74", "age" : 74 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c0"), "name" : "yalong75", "age" : 75 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c1"), "name" : "yalong76", "age" : 76 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c2"), "name" : "yalong77", "age" : 77 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c3"), "name" : "yalong78", "age" : 78 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c4"), "name" : "yalong79", "age" : 79 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c5"), "name" : "yalong80", "age" : 80 }
Type "it" for more
mongos> it
{ "_id" : ObjectId("5d3d5cc663819fca887970c6"), "name" : "yalong81", "age" : 81 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c7"), "name" : "yalong82", "age" : 82 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c8"), "name" : "yalong83", "age" : 83 }
{ "_id" : ObjectId("5d3d5cc663819fca887970c9"), "name" : "yalong84", "age" : 84 }
{ "_id" : ObjectId("5d3d5cc663819fca887970ca"), "name" : "yalong85", "age" : 85 }
{ "_id" : ObjectId("5d3d5cc663819fca887970cb"), "name" : "yalong86", "age" : 86 }
{ "_id" : ObjectId("5d3d5cc663819fca887970cc"), "name" : "yalong87", "age" : 87 }
{ "_id" : ObjectId("5d3d5cc663819fca887970cd"), "name" : "yalong88", "age" : 88 }
{ "_id" : ObjectId("5d3d5cc663819fca887970ce"), "name" : "yalong89", "age" : 89 }
{ "_id" : ObjectId("5d3d5cc663819fca887970cf"), "name" : "yalong90", "age" : 90 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d0"), "name" : "yalong91", "age" : 91 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d1"), "name" : "yalong92", "age" : 92 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d2"), "name" : "yalong93", "age" : 93 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d3"), "name" : "yalong94", "age" : 94 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d4"), "name" : "yalong95", "age" : 95 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d5"), "name" : "yalong96", "age" : 96 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d6"), "name" : "yalong97", "age" : 97 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d7"), "name" : "yalong98", "age" : 98 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d8"), "name" : "yalong99", "age" : 99 }
{ "_id" : ObjectId("5d3d5cc663819fca887970d9"), "name" : "yalong100", "age" : 100 }
本文详细介绍了MongoDB集群的三个核心角色:RouterServer、ConfigServer和DataServer的配置与部署过程,包括副本集的创建、配置服务器集群的设置以及路由服务器的关联与启用分片,展示了如何构建一个高可用性和高性能的MongoDB集群。
364

被折叠的 条评论
为什么被折叠?



