首页 > 数据库 > NoSQL > MongoDB 4.2副本集配置(一主一副一仲裁)
项目环境:
CentOS 7.5
MongoDB 4.2.12
一、配置参数文件
172.16.254.131
dbpath=/mongodb/data logpath=/mongodb/logs/mongodb.log port=27017 fork=true #auth=true noauth=true #verbose=true #vvvv=true journal = true maxConns=500 logappend=true directoryperdb=true #pidfilepath=/var/run/mongo.pid #cpu=true #nohttpinterface=false #notablescan=false #profile=0 #slowms=200 #quiet=true #syncdelay=60 bind_ip=0.0.0.0 replSet=handong1
172.16.254.132
dbpath=/mongodb/data logpath=/mongodb/logs/mongodb.log port=27017 fork=true #auth=true noauth=true #verbose=true #vvvv=true journal = true maxConns=500 logappend=true directoryperdb=true #pidfilepath=/var/run/mongo.pid #cpu=true #nohttpinterface=false #notablescan=false #profile=0 #slowms=200 #quiet=true #syncdelay=60 bind_ip=0.0.0.0 replSet=handong1
172.16.254.133
dbpath=/mongodb/data logpath=/mongodb/logs/mongodb.log port=27017 fork=true #auth=true noauth=true #verbose=true #vvvv=true journal = true maxConns=500 logappend=true directoryperdb=true #pidfilepath=/var/run/mongo.pid #cpu=true #nohttpinterface=false #notablescan=false #profile=0 #slowms=200 #quiet=true #syncdelay=60 bind_ip=0.0.0.0 replSet=handong1
二、启动mongodb服务
三个节点执行:
mongod -f /mongodb/conf/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 23212 child process started successfully, parent exiting
三、配置副本集
在计划的主节点执行:
rs.initiate({_id:"handong1",members:[{_id:0,host:"mongo1:27017"},{_id:1,host:"mongo2:27017"},{_id:2,host:"mongo3:27017",arbiterOnly:true}]}
查看副本集状态:
handong1:PRIMARY> rs.status()
{
"set" : "handong1",
"date" : ISODate("2021-03-18T09:28:47.224Z"),
"myState" : 1,
"term" : NumberLong(3),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1616059724, 1),
"t" : NumberLong(3)
},
"lastCommittedWallTime" : ISODate("2021-03-18T09:28:44.052Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1616059724, 1),
"t" : NumberLong(3)
},
"readConcernMajorityWallTime" : ISODate("2021-03-18T09:28:44.052Z"),
"appliedOpTime" : {
"ts" : Timestamp(1616059724, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1616059724, 1),
"t" : NumberLong(3)
},
"lastAppliedWallTime" : ISODate("2021-03-18T09:28:44.052Z"),
"lastDurableWallTime" : ISODate("2021-03-18T09:28:44.052Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1616059657, 1),
"lastStableCheckpointTimestamp" : Timestamp(1616059657, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-03-18T09:05:47.089Z"),
"electionTerm" : NumberLong(3),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1616055916, 1),
"t" : NumberLong(2)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2021-03-18T09:05:47.098Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-03-18T09:05:47.129Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.16.254.131:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1383,
"optime" : {
"ts" : Timestamp(1616059724, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2021-03-18T09:28:44Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1616058347, 1),
"electionDate" : ISODate("2021-03-18T09:05:47Z"),
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "mongo2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 28,
"optime" : {
"ts" : Timestamp(1616059724, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1616059724, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2021-03-18T09:28:44Z"),
"optimeDurableDate" : ISODate("2021-03-18T09:28:44Z"),
"lastHeartbeat" : ISODate("2021-03-18T09:28:46.063Z"),
"lastHeartbeatRecv" : ISODate("2021-03-18T09:28:46.065Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.131:27017",
"syncSourceHost" : "172.16.254.131:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 2,
"name" : "mongo3:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1,
"lastHeartbeat" : ISODate("2021-03-18T09:28:46.063Z"),
"lastHeartbeatRecv" : ISODate("2021-03-18T09:28:46.072Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 6
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1616059724, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1616059724, 1)
}
四、验证副本集
172.16.254.131
handong1:PRIMARY> use db3
switched to db db3
handong1:PRIMARY> db.db3.insert({"name":"mongodb4.2"})
WriteResult({ "nInserted" : 1 })
172.16.254.132
handong1:SECONDARY> rs.secondaryOk();
handong1:SECONDARY> db.db3.find()
{ "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" }
{ "_id" : ObjectId("6053225bdebdeda2639f026e"), "name" : "mongodb4.2" }
数据已经同步过来
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30135314/viewspace-2763658/ ,如需转载,请注明出处,否则将追究法律责任。