Mongodb-副本集部署
一、简单理解MongoDB副本集
1.概念:
MongoDB副本集相当于MySQL的主从复制,一主多从,单与MySQL不同的是,MongoDB的副本集实例的数量至少有三个。
2.结构
(1)一个主节点,多个从节点
(2)本次部署采用一主两从的结构
参考【http://www.mongoing.com/docs/core/replica-set-architecture-three-members.html】

3)优点:可以实现故障转移
二、部署副本集
参考文档【https://github.com/johnnian/Blog/issues/8】
注:本次因资源限制,所以在一台服务器上运行三个实例
1.部署MongoDB实例
请参考【http://www.cnblogs.com/dingkailinux/p/9007057.html】
(1)实例端口
实例1:edu-dev02:27027
实例2:edu-dev03:27027
实例2:edu-dev04:27027
实例2:edu-dev05:27027
(2)创建应用目录
mkdir -p /opt/data/mongodb
mkdir -p /opt/data/mongodb/log
mkdir -p /opt/data/mongodb/key
(3)修改mongo配置文件
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
# path: /var/log/mongodb/mongod.log
path: /opt/data/mongodb/log/mongod.log
# Where and how to store data.
storage:
dbPath: /opt/data/mongodb
# dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# pidFilePath: /opt/data/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27027
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
security:
keyFile: /opt/data/mongodb/key/mongodb.key
#operationProfiling:
#replication:
replication:
oplogSizeMB: 256
replSetName: eduDev
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
(4)启动实例
systemctl start mongod.service
2.配置副本集
(1)配置副本集
mongo --host localhost --port 27027
MongoDB shell version v4.2.18
connecting to: mongodb://localhost:27027/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ff0a2b1b-0796-4291-8a44-c3f9dbc45761") }
MongoDB server version: 4.2.18
> config = {_id: 'eduDev', members: [{_id: 0, host: 'edu-dev02:27027'},{_id: 1, host: 'edu-dev03:27027'},{_id: 2, host:du-dev04:27027'},{_id: 3, host:'edu-dev05:27027'}]}
{
"_id" : "eduDev",
"members" : [
{
"_id" : 0,
"host" : "edu-dev02:27027"
},
{
"_id" : 1,
"host" : "edu-dev03:27027"
},
{
"_id" : 2,
"host" : "edu-dev04:27027"
},
{
"_id" : 3,
"host" : "edu-dev05:27027"
}
]
}
> rs.initiate(config) #副本集初始化
eduDev:SECONDARY> rs.status()
{
"set" : "eduDev",
"date" : ISODate("2022-01-27T08:00:07.093Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 3,
"writeMajorityCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2022-01-27T08:00:01.285Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2022-01-27T08:00:01.285Z"),
"appliedOpTime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2022-01-27T08:00:01.285Z"),
"lastDurableWallTime" : ISODate("2022-01-27T08:00:01.285Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1643270351, 4),
"lastStableCheckpointTimestamp" : Timestamp(1643270351, 4),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2022-01-27T07:59:11.183Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1643270340, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 3,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2022-01-27T07:59:11.276Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2022-01-27T07:59:11.865Z")
},
"members" : [
{
"_id" : 0,
"name" : "edu-dev02:27027",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3151,
"optime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-01-27T08:00:01Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1643270351, 1),
"electionDate" : ISODate("2022-01-27T07:59:11Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "edu-dev03:27027",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 66,
"optime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-01-27T08:00:01Z"),
"optimeDurableDate" : ISODate("2022-01-27T08:00:01Z"),
"lastHeartbeat" : ISODate("2022-01-27T08:00:05.268Z"),
"lastHeartbeatRecv" : ISODate("2022-01-27T08:00:05.866Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "edu-dev02:27027",
"syncSourceHost" : "edu-dev02:27027",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "edu-dev04:27027",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 66,
"optime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-01-27T08:00:01Z"),
"optimeDurableDate" : ISODate("2022-01-27T08:00:01Z"),
"lastHeartbeat" : ISODate("2022-01-27T08:00:05.268Z"),
"lastHeartbeatRecv" : ISODate("2022-01-27T08:00:05.871Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "edu-dev02:27027",
"syncSourceHost" : "edu-dev02:27027",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "edu-dev05:27027",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 66,
"optime" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1643270401, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-01-27T08:00:01Z"),
"optimeDurableDate" : ISODate("2022-01-27T08:00:01Z"),
"lastHeartbeat" : ISODate("2022-01-27T08:00:05.268Z"),
"lastHeartbeatRecv" : ISODate("2022-01-27T08:00:05.870Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "edu-dev02:27027",
"syncSourceHost" : "edu-dev02:27027",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1643270401, 1),
"signature" : {
"hash" : BinData(0,"Jtlp3o/A8X3n2ZCCejli/SlFuYw="),
"keyId" : NumberLong("7057792416031440900")
}
},
"operationTime" : Timestamp(1643270401, 1)
}

浙公网安备 33010602011771号