mongodb集群部署(副本集)详细
1:部署前环境准备
操作系统:CentOS Linux release 7.8.2003 (Core)
安装包下载地址:https://www.mongodb.com/download-center/community
服务器资源信息:
192.168.111.139--------------master#####主节点 192.168.111.140--------------slaver#####备节点 192.168.111.141--------------arbiter#####仲裁节点
2:解压安装并启动
2.1:192.168.111.139
mkdir -p /opt/mongodb/data/master ###创建存放数据的目录 mkdir -p /opt/mongodb/log ###创建存放日志的目录 mkdir -p /opt/mongodb/conf ###创建存放配置文件的目录
配置文件:master.conf
dbpath=/opt/mongodb/data/master
logpath=/opt/mongodb/log/master.log
pidfilepath=/opt/mongodb/master.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=192.168.111.139
port=27017
oplogSize=10000
fork=true
#noprealloc=true
./monood -f ../conf/master.conf
2.2 192.168.111.140
mkdir -p /opt/mongodb/data/slaver ###创建存放数据的目录 mkdir -p /opt/mongodb/log ###创建存放日志的目录 mkdir -p /opt/mongodb/conf ###创建存放配置文件的目录
配置文件:slaver.conf
dbpath=/opt/mongodb/data/slaver
logpath=/opt/mongodb/log/slaver.log
pidfilepath=/opt/mongodb/slaver.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=192.168.111.140
port=27017
oplogSize=10000
fork=true
#noprealloc=true
./monood -f ../conf/slaver.conf
2.3 192.168.111.141
mkdir -p /opt/mongodb/data/arbiter ###创建存放数据的目录 mkdir -p /opt/mongodb/log ###创建存放日志的目录 mkdir -p /opt/mongodb/conf ###创建存放配置文件的目录
配置文件:arbiter.conf
dbpath=/opt/mongodb/data/arbiter
logpath=/opt/mongodb/log/arbiter.log
pidfilepath=/opt/mongodb/arbiter.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=192.168.111.141
port=27017
oplogSize=10000
fork=true
#noprealloc=true
./monood -f ../conf/arbiter.conf
3:配置主 备 仲裁节点
./mongo 192.168.111.139:27017 use admin cfg={ _id:"testrs", members:[ {_id:0,host:'192.168.111.139:27017',priority:2}, {_id:1,host:'192.168.111.140:27017',priority:1},{_id:2,host:'192.168.111.141:27017',arbiterOnly:true}] }; rs.initiate(cfg) ###使之生效
查看rs的相关信息
rs.status()
##########以上基本就是副本集集群部署的详细步骤,简单测试步骤如下=============
测试步骤:
1:再主节点添加数据
2:再从节点查看读取数据
报错如上:是因为从节点没有开启读操作
执行命令
rs.slaveOk()
然后再进行读取操作
3:测试主节点宕机故障
宕机故障前查看rs.status()
-
{ "set" : "testrs", "date" : ISODate("2022-01-06T09:25:12.526Z"), "myState" : 2, "term" : NumberLong(1), "syncSourceHost" : "192.168.111.139:27017", "syncSourceId" : 0, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1641461104, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2022-01-06T09:25:04.568Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1641461104, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1641461104, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1641461104, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2022-01-06T09:25:04.568Z"), "lastDurableWallTime" : ISODate("2022-01-06T09:25:04.568Z") }, "lastStableRecoveryTimestamp" : Timestamp(1641461064, 1), "electionParticipantMetrics" : { "votedForCandidate" : true, "electionTerm" : NumberLong(1), "lastVoteDate" : ISODate("2022-01-06T08:41:24.268Z"), "electionCandidateMemberId" : 0, "voteReason" : "", "lastAppliedOpTimeAtElection" : { "ts" : Timestamp(1641458473, 1), "t" : NumberLong(-1) }, "maxAppliedOpTimeInSet" : { "ts" : Timestamp(1641458473, 1), "t" : NumberLong(-1) }, "priorityAtElection" : 1, "newTermStartDate" : ISODate("2022-01-06T08:41:24.284Z"), "newTermAppliedDate" : ISODate("2022-01-06T08:41:24.369Z") }, "members" : [ { "_id" : 0, "name" : "192.168.111.139:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2638, "optime" : { "ts" : Timestamp(1641461104, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1641461104, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2022-01-06T09:25:04Z"), "optimeDurableDate" : ISODate("2022-01-06T09:25:04Z"), "lastAppliedWallTime" : ISODate("2022-01-06T09:25:04.568Z"), "lastDurableWallTime" : ISODate("2022-01-06T09:25:04.568Z"), "lastHeartbeat" : ISODate("2022-01-06T09:25:11.711Z"), "lastHeartbeatRecv" : ISODate("2022-01-06T09:25:10.982Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1641458484, 1), "electionDate" : ISODate("2022-01-06T08:41:24Z"), "configVersion" : 1, "configTerm" : 1 }, { "_id" : 1, "name" : "192.168.111.140:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 2911, "optime" : { "ts" : Timestamp(1641461104, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2022-01-06T09:25:04Z"), "lastAppliedWallTime" : ISODate("2022-01-06T09:25:04.568Z"), "lastDurableWallTime" : ISODate("2022-01-06T09:25:04.568Z"), "syncSourceHost" : "192.168.111.139:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "192.168.111.141:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 2638, "lastHeartbeat" : ISODate("2022-01-06T09:25:11.711Z"), "lastHeartbeatRecv" : ISODate("2022-01-06T09:25:10.765Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 1, "configTerm" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1641461104, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1641461104, 1) }
2:将主节点的进程杀掉
ps -ef|grep mongo kill -9 2705
登录从节点查看节点状态
{ "set" : "testrs", "date" : ISODate("2022-01-06T09:31:29.890Z"), "myState" : 1, "term" : NumberLong(2), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1641461424, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2022-01-06T09:30:24.594Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1641461424, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1641461481, 1), "t" : NumberLong(2) }, "durableOpTime" : { "ts" : Timestamp(1641461481, 1), "t" : NumberLong(2) }, "lastAppliedWallTime" : ISODate("2022-01-06T09:31:21.169Z"), "lastDurableWallTime" : ISODate("2022-01-06T09:31:21.169Z") }, "lastStableRecoveryTimestamp" : Timestamp(1641461424, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2022-01-06T09:30:41.137Z"), "electionTerm" : NumberLong(2), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(1641461424, 1), "t" : NumberLong(1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1641461424, 1), "t" : NumberLong(1) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2022-01-06T09:30:41.161Z") }, "electionParticipantMetrics" : { "votedForCandidate" : true, "electionTerm" : NumberLong(1), "lastVoteDate" : ISODate("2022-01-06T08:41:24.268Z"), "electionCandidateMemberId" : 0, "voteReason" : "", "lastAppliedOpTimeAtElection" : { "ts" : Timestamp(1641458473, 1), "t" : NumberLong(-1) }, "maxAppliedOpTimeInSet" : { "ts" : Timestamp(1641458473, 1), "t" : NumberLong(-1) }, "priorityAtElection" : 1 }, "members" : [ { "_id" : 0, "name" : "192.168.111.139: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"), "lastAppliedWallTime" : ISODate("2022-01-06T09:30:24.594Z"), "lastDurableWallTime" : ISODate("2022-01-06T09:30:24.594Z"), "lastHeartbeat" : ISODate("2022-01-06T09:31:29.296Z"), "lastHeartbeatRecv" : ISODate("2022-01-06T09:30:31.293Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Error connecting to 192.168.111.139:27017 :: caused by :: Connection refused", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 1, "configTerm" : 1 }, { "_id" : 1, "name" : "192.168.111.140:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 3288, "optime" : { "ts" : Timestamp(1641461481, 1), "t" : NumberLong(2) }, "optimeDate" : ISODate("2022-01-06T09:31:21Z"), "lastAppliedWallTime" : ISODate("2022-01-06T09:31:21.169Z"), "lastDurableWallTime" : ISODate("2022-01-06T09:31:21.169Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1641461441, 1), "electionDate" : ISODate("2022-01-06T09:30:41Z"), "configVersion" : 1, "configTerm" : 2, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "192.168.111.141:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 3015, "lastHeartbeat" : ISODate("2022-01-06T09:31:29.200Z"), "lastHeartbeatRecv" : ISODate("2022-01-06T09:31:29.214Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 1, "configTerm" : 2 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1641461481, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1641461481, 1) }
从上面信息可以看到从节点的节点状态已经变成了“PRIMARY”主节点,
重启主节点信息,然后查看节点状态发现节点的主从状态再次更新变化。。。
上面的部署环境:一主一丛一仲裁 =====主节点挂机后通过仲裁机制自动切换到备机上
一主两从===========主节点挂机后自动切换到另外两台中的一台
###########################################即供参考,请勿乱喷。。