MongoDB 副本集环境搭建
主机名 | IP地址 | 端口 | 角色 |
---|---|---|---|
node-01 | 192.168.229.21 | 27017 | 主节点(PRIMARY) |
node-02 | 192.168.229.22 | 27017 | 从节点1(SECONDARY) |
node-03 | 192.168.229.23 | 27017 | 从节点2(SECONDARY) |
node-03 | 192.168.229.23 | 28017 | 仲裁者(ARBITER) |
搭建步骤
步骤一:修改副本集配置
在 node-01 上拷贝 mongdb.conf 配置并修改,如下:
复制代码
[root@node-01 config]# cp mongodb.conf replica_set.conf
[root@node-01 config]# vim replica_set.conf
#关闭权限验证
auth=false
#副本集名称
replSet=test
#操作日志大小(MB)
oplogSize=100
步骤二:拷贝副本集配置
将 replica_set.conf 配置文件分别拷贝到 node-02、node-03
复制代码
[root@node-01 config]# scp replica_set.conf node-02:$PWD
[root@node-01 config]# scp replica_set.conf node-03:$PWD
步骤三:拷贝仲裁者配置
拷贝 node-03 配置改名为 arbiter.cnf 并修改配置,如下:
复制代码
[root@node-03 config]# cp replica_set.conf arbiter.conf
[root@node-03 config]# vim arbiter.conf
dbpath=/root/apps/mongodb-4.2.7/data/arbiter_db
#日志文件存放目录
logpath=/root/apps/mongodb-4.2.7/logs/arbiter_mongodb.log
#pid文件
pidfilepath=/root/apps/mongodb-4.2.7/run/arbiter_mongodb.pid
#端口
port=28017
[root@node-03 config]# cd ..
[root@node-03 mongodb-4.2.7]# cd data/
[root@node-03 data]# mkdir arbiter_db
步骤三:启动 MongoDB 服务
复制代码
[root@node-01 mongodb-4.2.7]# bin/mongod -f config/replica_set.conf
[root@node-02 mongodb-4.2.7]# bin/mongod -f config/replica_set.conf
[root@node-03 mongodb-4.2.7]# bin/mongod -f config/replica_set.conf
[root@node-03 mongodb-4.2.7]# bin/mongod -f config/arbiter.conf
[root@node-01 mongodb-4.2.7]# ps -ef | grep mongo
root 1552 1 1 23:01 ? 00:00:52 bin/mongod -f config/replica_set.conf
root 1652 1489 0 23:47 pts/0 00:00:00 grep --color=auto mongo
[root@node-02 mongodb-4.2.7]# ps -ef | grep mongo
root 1552 1 1 23:01 ? 00:00:52 bin/mongod -f config/replica_set.conf
root 1652 1489 0 23:47 pts/0 00:00:00 grep --color=auto mongo
[root@node-03 mongodb-4.2.7]# ps -ef | grep mongo
root 1758 1 1 23:31 ? 00:00:13 bin/mongod -f config/replica_set.conf
root 1858 1 4 23:45 ? 00:00:00 bin/mongod -f config/arbiter.conf
root 1895 1490 0 23:45 pts/0 00:00:00 grep --color=auto mongo
步骤四:初始化副本集
复制代码
[root@node-01 mongodb-4.2.7]# bin/mongo
> use admin
switched to db admin
# 初始化副本集
> rs.initiate( {
_id: "test",
members: [
{ _id: 0, host: "192.168.229.21:27017" },
{ _id: 1, host: "192.168.229.22:27017" },
{ _id: 2, host: "192.168.229.23:27017" },
{ _id: 3, host: "192.168.229.23:28017",arbiterOnly:true }
] })
#副本集初始化成功
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1616341576, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1616341576, 1)
}
test:SECONDARY>
_id:"test" 是副本集名称(必须与副本集配置文件中保持一致)
查看各节点状态
复制代码
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2021-03-21T15:50:08.438Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 3,
"writeMajorityCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1616341807, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-03-21T15:50:07.277Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1616341807, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-03-21T15:50:07.277Z"),
"appliedOpTime" : {
"ts" : Timestamp(1616341807, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1616341807, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-03-21T15:50:07.277Z"),
"lastDurableWallTime" : ISODate("2021-03-21T15:50:07.277Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1616341767, 1),
"lastStableCheckpointTimestamp" : Timestamp(1616341767, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-03-21T15:46:27.223Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1616341576, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 3,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-03-21T15:46:27.244Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-03-21T15:46:28.716Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.229.21:27017", #主节点
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3001,
"optime" : {
"ts" : Timestamp(1616341807, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-21T15:50:07Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1616341587, 1),
"electionDate" : ISODate("2021-03-21T15:46:27Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.229.22:27017",#从节点
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 232,
"optime" : {
"ts" : Timestamp(1616341807, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1616341807, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-03-21T15:50:07Z"),
"optimeDurableDate" : ISODate("2021-03-21T15:50:07Z"),
"lastHeartbeat" : ISODate("2021-03-21T15:50:07.490Z"),
"lastHeartbeatRecv" : ISODate("2021-03-21T15:50:06.779Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.229.21:27017",
"syncSourceHost" :