复制集原理
复制集的作用
主要为了实现mongodb
服务的高可用,复制集之间是一种主从关系
- 当数据写入时将数据迅速复制到其它节点
- 当主节点发生故障时自动选出一个新的节点代替
由上面的特点,我们对redis
了解的话,可以很快联想到哨兵模式
在除了以上的数据同步
和故障节点替换
两大特性以外,复制集还存在以下作用
数据分发:将数据分发到不同的区域,减少读的延迟
例如公司在北京、上海、广州都有数据中心,但只有北京的是可以写入的中心,可将数据异地分发,提高上海和广州大区用户的读取效率。
- 读写分离
- 异地容灾:数据中心故障时快速切换到异地。
复制集的结构
复制集的结构与其它中间件的主从模式基本类似。
一个典型的复制集由3个及以上具有投票的节点组成:
- 主节点:只有一个,负责数据的写入和读取,以及选举时投票。
- 从节点:至少有两个,负责数据读取和选举投票。
数据的复制原理
- 当数据(主节点)发生修改(增删改)时,它对数据的操作将被记录在一个
oplog
文件当中。 - 从节点通过在主节点上代开一个
tailable
游标不断获取主节点中新增的oplog
并且不断在自己的节点上重现这些操作,以此来实现和主节点上的数据一致。
主节点选举原理
- 具有投票权的节点之间两两互相发送心跳。
- 当5次心跳未收到时判断为节点失联。
- 如果主节点失联,从节点会选出新的主节点,但失联的从节点不会参与选举。
- 选举基于RAFT一致性算法实现,选举成功的必要条件是大多数投票节点存活。
- 复制集中最多可有50个节点,但有投票权的最多有7个。
- 被选举为主节点的的节点 必须能与多数节点建立连接、有比较新的
oplog
、比较高的优先级(如果配置中有)
搭建一个复制集
下面是在linux
下创建一个mongodb
复制集
创建三个目录
mkdir -p /data/mongodb/db{1,2,3}
配置文件
#节点1
systemLog:
destination: file
path: /data/mongodb/db1/mongod.log #日志路径
logAppend: true
storage:
dbPath: /data/mongodb/db1 #数据文件
net:
bindIp: 0.0.0.0 #在所有网卡监听,为了外网访问
port: 28017
replication: #复制集,没有的话就是一个单节点
replSetName: rs0 #复制集名字
processManagement:
fork: true #把进程作为独立的后台进程进程
#节点2
systemLog:
destination: file
path: /data/mongodb/db2/mongod.log #日志路径
logAppend: true
storage:
dbPath: /data/mongodb/db2 #数据文件
net:
bindIp: 0.0.0.0 #在所有网卡监听,为了外网访问
port: 28018
replication: #复制集,没有的话就是一个单节点
replSetName: rs0 #复制集名字
processManagement:
fork: true #把进程作为独立的后台进程进程
#节点3
systemLog:
destination: file
path: /data/mongodb/db3/mongod.log #日志路径
logAppend: true
storage:
dbPath: /data/mongodb/db3 #数据文件
net:
bindIp: 0.0.0.0 #在所有网卡监听,为了外网访问
port: 28019
replication: #复制集,没有的话就是一个单节点
replSetName: rs0 #复制集名字
processManagement:
fork: true #把进程作为独立的后台进程进程
启动进程
mongod -f db1/mongod.conf
mongod -f db2/mongod.conf
mongod -f db3/mongod.conf
配置复制集
进入主节点
mongo --port 28017
设置主节点
rs.initiate()
{ "info2" : "no configuration specified. Using a default configuration for the set", "me" : "supman:28017", "ok" : 1 }
查看节点状态
rs.status()
{ "set" : "rs0", "date" : ISODate("2021-11-30T15:33:30.616Z"), "myState" : 1, "term" : NumberLong(1), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 1, "writeMajorityCount" : 1, "votingMembersCount" : 1, "writableVotingMembersCount" : 1, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1638286404, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2021-11-30T15:33:24.656Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1638286404, 1), "t" : NumberLong(1) }, "readConcernMajorityWallTime" : ISODate("2021-11-30T15:33:24.656Z"), "appliedOpTime" : { "ts" : Timestamp(1638286404, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1638286404, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2021-11-30T15:33:24.656Z"), "lastDurableWallTime" : ISODate("2021-11-30T15:33:24.656Z") }, "lastStableRecoveryTimestamp" : Timestamp(1638286364, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2021-11-30T15:27:54.614Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1638286074, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 1, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "newTermStartDate" : ISODate("2021-11-30T15:27:54.637Z"), "wMajorityWriteAvailabilityDate" : ISODate("2021-11-30T15:27:54.660Z") }, "members" : [ { "_id" : 0, "name" : "supman:28017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1227, "optime" : { "ts" : Timestamp(1638286404, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-11-30T15:33:24Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1638286074, 2), "electionDate" : ISODate("2021-11-30T15:27:54Z"), "configVersion" : 1, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1638286404, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1638286404, 1) }
找到status信息里面的
members.name
字段"name" : "supman:28017"
增加从节点
rs.add("supman:28018")
{ "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1638286626, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1638286626, 1) }
rs.add("supman:28019")
{ "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1638286660, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1638286660, 1) }
再次查看状态
{ "set" : "rs0", "date" : ISODate("2021-11-30T15:44:29.903Z"), "myState" : 1, "term" : NumberLong(1), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 3, "writableVotingMembersCount" : 3, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2021-11-30T15:44:24.673Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "readConcernMajorityWallTime" : ISODate("2021-11-30T15:44:24.673Z"), "appliedOpTime" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2021-11-30T15:44:24.673Z"), "lastDurableWallTime" : ISODate("2021-11-30T15:44:24.673Z") }, "lastStableRecoveryTimestamp" : Timestamp(1638287034, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2021-11-30T15:27:54.614Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1638286074, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 1, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "newTermStartDate" : ISODate("2021-11-30T15:27:54.637Z"), "wMajorityWriteAvailabilityDate" : ISODate("2021-11-30T15:27:54.660Z") }, "members" : [ { "_id" : 0, "name" : "supman:28017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1886, "optime" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-11-30T15:44:24Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1638286074, 2), "electionDate" : ISODate("2021-11-30T15:27:54Z"), "configVersion" : 3, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "supman:28018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 442, "optime" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-11-30T15:44:24Z"), "optimeDurableDate" : ISODate("2021-11-30T15:44:24Z"), "lastHeartbeat" : ISODate("2021-11-30T15:44:28.545Z"), "lastHeartbeatRecv" : ISODate("2021-11-30T15:44:28.566Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "supman:28017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 3, "configTerm" : 1 }, { "_id" : 2, "name" : "supman:28019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 409, "optime" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1638287064, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-11-30T15:44:24Z"), "optimeDurableDate" : ISODate("2021-11-30T15:44:24Z"), "lastHeartbeat" : ISODate("2021-11-30T15:44:28.565Z"), "lastHeartbeatRecv" : ISODate("2021-11-30T15:44:29.316Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "supman:28018", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 3, "configTerm" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1638287064, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1638287064, 1) }
在从节点上配置可读
进入从节点
mongo --port 28018
mongo --port 28019
设置从节点可读
rs.slaveOk()
- 最后在主节点增加一个用户,创建一个数据库和集合,插入数据后也能在从节点查看到数据了
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。