项目环境:
CentOS 7.5
MongoDB 4.2.12
一、部署 MongoDB
1、三个节点安装以下包
yum install -y cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs lm_sensors-libs net-snmp-agent-libs \
net-snmp openssl openssl-devel rpm-libs tcp_wrappers-libs
2、创建MongoDB用户和组
groupadd -g 10001 mongodb
useradd -u 10001 -g mongodb mongodb
3、修改/etc/security/limits.conf
mongodb hard nofile 25600
mongodb soft nofile 25600
mongodb hard nproc 25600
mongodb soft nproc 25600
4、创建目录
mkdir -p /mongodb/apps
mkdir -p /mongodb/data
mkdir -p /mongodb/logs
mkdir -p /mongodb/conf
5、修改mongodb.conf
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
6、启动MongoDB
三个节点分别执行
mongod -f /mongodb/conf/mongodb.conf
mongod -f /mongodb/conf/mongodb.conf
mongod -f /mongodb/conf/mongodb.conf
二、配置复制集
1、创建复制集
rs.initiate({
_id:"handong1",
members:[{
_id:0,
host:"172.16.254.131:27017"
},{
_id:1,
host:"172.16.254.132:27017"
},{
_id:2,
host:"172.16.254.133:27017"
}]
})
2、开启副本节点查询
MongoDB复制集副本节点默认不支持查询,需要手动开启。
副本节点执行:
rs.secondaryOk();
3、查看复制集信息
handong1:PRIMARY> rs.status()
{
"set" : "handong1",
"date" : ISODate("2021-03-17T11:46:06.442Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-03-17T11:46:05.489Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-03-17T11:46:05.489Z"),
"appliedOpTime" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-03-17T11:46:05.489Z"),
"lastDurableWallTime" : ISODate("2021-03-17T11:46:05.489Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1615981555, 1),
"lastStableCheckpointTimestamp" : Timestamp(1615981555, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-03-17T09:21:47.969Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1615972897, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-03-17T09:21:48.059Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-03-17T09:21:49.600Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.16.254.131:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 10604,
"optime" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-17T11:46:05Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1615972908, 1),
"electionDate" : ISODate("2021-03-17T09:21:48Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.16.254.132:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8668,
"optime" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-17T11:46:05Z"),
"optimeDurableDate" : ISODate("2021-03-17T11:46:05Z"),
"lastHeartbeat" : ISODate("2021-03-17T11:46:06.180Z"),
"lastHeartbeatRecv" : ISODate("2021-03-17T11:46:04.538Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.131:27017",
"syncSourceHost" : "172.16.254.131:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.16.254.133:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8668,
"optime" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1615981565, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-17T11:46:05Z"),
"optimeDurableDate" : ISODate("2021-03-17T11:46:05Z"),
"lastHeartbeat" : ISODate("2021-03-17T11:46:06.182Z"),
"lastHeartbeatRecv" : ISODate("2021-03-17T11:46:04.541Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.254.131:27017",
"syncSourceHost" : "172.16.254.131:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1615981565, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1615981565, 1)
}
三、验证MongoDB复制集
创建db
handong1:PRIMARY> use db3
switched to db db3
插入测试数据:
handong1:PRIMARY> db.db3.insert({"name":"mongodb"})
WriteResult({ "nInserted" : 1 })
查询数据:
172.16.254.131
handong1:SECONDARY> use db3
switched to db db3
handong1:PRIMARY> db.db3.find()
{ "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" }
172.16.254.132
handong1:SECONDARY> use db3
switched to db db3
handong1:SECONDARY> db.db3.find()
{ "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" }
172.16.254.133
handong1:SECONDARY> use db3
switched to db db3
handong1:SECONDARY> db.db3.find()
{ "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" }
可以看到副本节点数据已经同步。