MongoDB 副本集环境搭建

节点规划

img

主机名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" : "192.168.229.21:27017",
                       "syncSourceId" : 0,
                       "infoMessage" : "",
                       "configVersion" : 1
              },
              {
                       "_id" : 2,
                       "name" : "192.168.229.23: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.486Z"),
                       "lastHeartbeatRecv" : ISODate("2021-03-21T15:50:06.999Z"),
                       "pingMs" : NumberLong(0),
                       "lastHeartbeatMessage" : "",
                       "syncingTo" : "192.168.229.21:27017",
                       "syncSourceHost" : "192.168.229.21:27017",
                       "syncSourceId" : 0,
                       "infoMessage" : "",
                       "configVersion" : 1
              },
              {
                       "_id" : 3,
                       "name" : "192.168.229.23:28017", #仲裁者
                       "health" : 1,
                       "state" : 7,
                       "stateStr" : "ARBITER",
                       "uptime" : 232,
                       "lastHeartbeat" : ISODate("2021-03-21T15:50:07.481Z"),
                       "lastHeartbeatRecv" : ISODate("2021-03-21T15:50:06.684Z"),
                       "pingMs" : NumberLong(0),
                       "lastHeartbeatMessage" : "",
                       "syncingTo" : "",
                       "syncSourceHost" : "",
                       "syncSourceId" : -1,
                       "infoMessage" : "",
                       "configVersion" : 1
              }
      ],
       "ok" : 1,
       "$clusterTime" : {
               "clusterTime" : Timestamp(1616341807, 1),
               "signature" : {
                       "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                       "keyId" : NumberLong(0)
              }
      },
       "operationTime" : Timestamp(1616341807, 1)
}
test:PRIMARY>

以上结果显示,node-01(192.168.229.21)为主节点,node-02(192.168.229.22) 为从节点,node-03(192.168.229.23) 为从节点。node-03(192.168.229.23)上还有一个仲裁者。

测试主节点

复制代码

test:PRIMARY> use test
switched to db test
test:PRIMARY> db.mytest.insert({name:"zhangsan"})
WriteResult({ "nInserted" : 1 })
test:PRIMARY> db.mytest.find()
{ "_id" : ObjectId("60576d254f85e0289ad88339"), "name" : "zhangsan" }

主节点上可以正常插入文档

测试从节点

复制代码

[root@node-02 mongodb-4.2.7]# bin/mongo
test:SECONDARY> db.mytest.find()
Error: error: {
       "operationTime" : Timestamp(1616342427, 1),
       "ok" : 0,
       "errmsg" : "not master and slaveOk=false",
       "code" : 13435,
       "codeName" : "NotMasterNoSlaveOk",
       "$clusterTime" : {
               "clusterTime" : Timestamp(1616342427, 1),
               "signature" : {
                       "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                       "keyId" : NumberLong(0)
              }
      }
}
test:SECONDARY>

从节点查询文档时,出现“not master and slaveok=false”错误,这是正常的,原因是从节点默认不允许读写文档。

配置主从读写分离
  • node-02 从节点设置

    复制代码

    test:SECONDARY> db.getMongo().setSlaveOk()
  • node-01 主节点设置

    复制代码

    test:PRIMARY> db.getMongo().setSlaveOk()

再在从节点上查看和插入文档如下:

复制代码

test:SECONDARY> db.mytest.find()
{ "_id" : ObjectId("60576d254f85e0289ad88339"), "name" : "zhangsan" }

test:SECONDARY> db.mytest.insert({name:null})
WriteCommandError({
       "operationTime" : Timestamp(1616343009, 1),
       "ok" : 0,
       "errmsg" : "not master",
       "code" : 10107,
       "codeName" : "NotMaster",
       "$clusterTime" : {
               "clusterTime" : Timestamp(1616343009, 1),
               "signature" : {
                       "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                       "keyId" : NumberLong(0)
              }
      }
})

现在可以查看文档,但是不能插入文档。

测试故障转移

node-01 上关闭 MongoDB 服务,如下:

复制代码

[root@node-01 mongodb-4.2.7]# bin/mongod --shutdown --dbpath data/db

node-03 上查看集群节点状态,如下:

复制代码

test:SECONDARY> rs.status()
{
       "set" : "test",
       "date" : ISODate("2021-03-21T16:12:11.585Z"),
       "myState" : 2,
       "term" : NumberLong(3),
       "syncingTo" : "",
       "syncSourceHost" : "",
       "syncSourceId" : -1,
       "heartbeatIntervalMillis" : NumberLong(2000),
       "majorityVoteCount" : 3,
       "writeMajorityCount" : 3,
       "optimes" : {
               "lastCommittedOpTime" : {
                       "ts" : Timestamp(1616343119, 1),
                       "t" : NumberLong(1)
              },
               "lastCommittedWallTime" : ISODate("2021-03-21T16:11:59.385Z"),
               "readConcernMajorityOpTime" : {
                       "ts" : Timestamp(1616343119, 1),
                       "t" : NumberLong(1)
              },
               "readConcernMajorityWallTime" : ISODate("2021-03-21T16:11:59.385Z"),
               "appliedOpTime" : {
                       "ts" : Timestamp(1616343119, 1),
                       "t" : NumberLong(1)
              },
               "durableOpTime" : {
                       "ts" : Timestamp(1616343119, 1),
                       "t" : NumberLong(1)
              },
               "lastAppliedWallTime" : ISODate("2021-03-21T16:11:59.385Z"),
               "lastDurableWallTime" : ISODate("2021-03-21T16:11:59.385Z")
      },
       "lastStableRecoveryTimestamp" : Timestamp(1616343087, 1),
       "lastStableCheckpointTimestamp" : Timestamp(1616343087, 1),
       "electionParticipantMetrics" : {
               "votedForCandidate" : true,
               "electionTerm" : NumberLong(3),
               "lastVoteDate" : ISODate("2021-03-21T16:12:10.417Z"),
               "electionCandidateMemberId" : 1,
               "voteReason" : "",
               "lastAppliedOpTimeAtElection" : {
                       "ts" : Timestamp(1616343119, 1),
                       "t" : NumberLong(1)
              },
               "maxAppliedOpTimeInSet" : {
                       "ts" : Timestamp(1616343119, 1),
                       "t" : NumberLong(1)
              },
               "priorityAtElection" : 1
      },
       "members" : [
              {
                       "_id" : 0,
                       "name" : "192.168.229.21:27017",
                       "health" : 0,
                       "state" : 8,
                       "stateStr" : "(not reachable/healthy)", #下线
                       "uptime" : 0,
                       "optime" : {
                               "ts" : Timestamp(0, 0),
                               "t" : NumberLong(-1)
                      },
                       "optimeDurable" : {
                               "ts" : Timestamp(0, 0),
                               "t" : NumberLong(-1)
                      },
                       "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                       "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                       "lastHeartbeat" : ISODate("2021-03-21T16:12:11.418Z"),
                       "lastHeartbeatRecv" : ISODate("2021-03-21T16:11:59.056Z"),
                       "pingMs" : NumberLong(0),
                       "lastHeartbeatMessage" : "Error connecting to 192.168.229.21:27017 :: caused by :: Connection refused",
                       "syncingTo" : "",
                       "syncSourceHost" : "",
                       "syncSourceId" : -1,
                       "infoMessage" : "",
                       "configVersion" : -1
              },
              {
                       "_id" : 1,
                       "name" : "192.168.229.22:27017", #新主节点
                       "health" : 1,
                       "state" : 1,
                       "stateStr" : "PRIMARY",
                       "uptime" : 1555,
                       "optime" : {
                               "ts" : Timestamp(1616343130, 2),
                               "t" : NumberLong(3)
                      },
                       "optimeDurable" : {
                               "ts" : Timestamp(1616343130, 2),
                               "t" : NumberLong(3)
                      },
                       "optimeDate" : ISODate("2021-03-21T16:12:10Z"),
                       "optimeDurableDate" : ISODate("2021-03-21T16:12:10Z"),
                       "lastHeartbeat" : ISODate("2021-03-21T16:12:11.353Z"),
                       "lastHeartbeatRecv" : ISODate("2021-03-21T16:12:10.419Z"),
                       "pingMs" : NumberLong(1),
                       "lastHeartbeatMessage" : "",
                       "syncingTo" : "",
                       "syncSourceHost" : "",
                       "syncSourceId" : -1,
                       "infoMessage" : "",
                       "electionTime" : Timestamp(1616343130, 1),
                       "electionDate" : ISODate("2021-03-21T16:12:10Z"),
                       "configVersion" : 1
              },
              {
                       "_id" : 2,
                       "name" : "192.168.229.23:27017",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 2413,
                       "optime" : {
                               "ts" : Timestamp(1616343119, 1),
                               "t" : NumberLong(1)
                      },
                       "optimeDate" : ISODate("2021-03-21T16:11:59Z"),
                       "syncingTo" : "",
                       "syncSourceHost" : "",
                       "syncSourceId" : -1,
                       "infoMessage" : "",
                       "configVersion" : 1,
                       "self" : true,
                       "lastHeartbeatMessage" : ""
              },
              {
                       "_id" : 3,
                       "name" : "192.168.229.23:28017",
                       "health" : 1,
                       "state" : 7,
                       "stateStr" : "ARBITER",
                       "uptime" : 1555,
                       "lastHeartbeat" : ISODate("2021-03-21T16:12:11.325Z"),
                       "lastHeartbeatRecv" : ISODate("2021-03-21T16:12:09.640Z"),
                       "pingMs" : NumberLong(0),
                       "lastHeartbeatMessage" : "",
                       "syncingTo" : "",
                       "syncSourceHost" : "",
                       "syncSourceId" : -1,
                       "infoMessage" : "",
                       "configVersion" : 1
              }
      ],
       "ok" : 1,
       "$clusterTime" : {
               "clusterTime" : Timestamp(1616343130, 2),
               "signature" : {
                       "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                       "keyId" : NumberLong(0)
              }
      },
       "operationTime" : Timestamp(1616343119, 1)
}
test:SECONDARY>

可以看到,node-02(192.168.229.22:27017)成了新主节点,故障转移成功:)

posted @ 2021-11-22 14:50  追こするれい的人  阅读(105)  评论(0编辑  收藏  举报