乐趣区

关于java:MongoDB学习之复制集

复制集原理

复制集的作用

次要为了实现 mongodb 服务的高可用,复制集之间是一种主从关系

  • 当数据写入时将数据迅速复制到其它节点
  • 当主节点产生故障时主动选出一个新的节点代替

由下面的特点,咱们对 redis 理解的话,能够很快联想到 哨兵模式

在除了以上的 数据同步 故障节点替换 两大个性以外,复制集还存在以下作用

  • 数据散发:将数据散发到不同的区域,缩小读的提早

    例如公司在北京、上海、广州都有数据中心,但只有北京的是能够写入的核心,可将数据异地散发,进步上海和广州大区用户的读取效率。

  • 读写拆散
  • 异地容灾:数据中心故障时疾速切换到异地。

复制集的构造

复制集的构造与其它中间件的主从模式根本相似。

一个典型的复制集由 3 个及以上具备投票的节点组成:

  • 主节点:只有一个,负责数据的写入和读取,以及选举时投票。
  • 从节点:至多有两个,负责数据读取和选举投票。

数据的复制原理

  • 当数据 ( 主节点 ) 产生批改 ( 增删改 ) 时,它对数据的操作将被记录在一个 oplog 文件当中。
  • 从节点通过在主节点上代开一个 tailable 游标一直获取主节点中新增的 oplog 并且一直在本人的节点上重现这些操作,以此来实现和主节点上的数据统一。

主节点选举原理

  1. 具备投票权的节点之间两两相互发送心跳。
  2. 当 5 次心跳未收到时判断为节点失联。
  3. 如果主节点失联,从节点会选出新的主节点,但失联的从节点不会参加选举。
  4. 选举基于 RAFT 一致性算法实现,选举胜利的必要条件是大多数投票节点存活。
  5. 复制集中最多可有 50 个节点,但有投票权的最多有 7 个。
  6. 被选举为主节点的的节点 必须能与少数节点建设连贯、有比拟新的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

配置复制集

  1. 进入主节点

    mongo --port 28017

  2. 设置主节点

    rs.initiate()

    {
            "info2" : "no configuration specified. Using a default configuration for the set",
            "me" : "supman:28017",
            "ok" : 1
    }
  3. 查看节点状态

    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"
  4. 减少从节点

    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)
    }
  5. 再次查看状态

    {
            "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)
    }
  6. 在从节点上配置可读

    进入从节点

    mongo --port 28018 mongo --port 28019

    设置从节点可读

    rs.slaveOk()

  7. 最初在主节点减少一个用户,创立一个数据库和汇合,插入数据后也能在从节点查看到数据了
退出移动版