视频版
https://www.bilibili.com/video/BV1Lv4y1Q7xe/
环境准备
按https://www.mongodb.com/docs/manual/administration/production-notes/#std-label-prod-notes-supported-platforms 所说6.0支持20.04和18.04
没说22.04支持,保险起见按文档准备系统版本
至少3个以上的奇数节点,5.0以上必须使用域名配置,节点间端口27017网络要求互通
0 1 2 3 4 5 6 |
IP1 mongodb0.example.net IP2 mongodb1.example.net IP3 mongodb2.example.net IP1 test.w.mongo.db IP2 test.r.mongo.db IP3 test.r.mongo.db |
1.安装
0 1 2 3 4 5 6 |
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - sudo touch /etc/apt/sources.list.d/mongodb-org-6.0.list echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt-get update sudo apt-get install -y mongodb-org |
常见目录
0 1 2 3 |
数据 /var/lib/mongodb 日志 /var/log/mongodb 配置 /etc/mongod.conf |
常用命令
0 1 2 3 4 5 |
sudo systemctl start mongod sudo systemctl stop mongod sudo systemctl restart mongod sudo systemctl status mongod mongosh |
2.集群配置
MongoDB 的复制集是由一组保持相同数据集的 mongod
进程组成的。复制集提供了冗余和 高可用的能力,同时它是所有生产环境部署的基础
在特定情况下可以提高读取性能,因为客户端可以把读取请求发送给不同的服务器。被复制多份数据可以用于灾备或审计
复制集包含几个 data bearing nodes和可选的一个arbiter node。在data bearing nodes上有且仅有一个成员是主节点,主节点 接收所有写请求操作,一个主节点会使用 { w: "majority" }
来标识
设置bindIp启用远程访问,生产环境注意提前设置密码和防火墙
https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIp
配置集群名
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
mongosh #初始化集群配置 rs.initiate( { _id : "rs0", members: [ { _id: 0, host: "mongodb0.example.net:27017" }, { _id: 1, host: "mongodb1.example.net:27017" }, { _id: 2, host: "mongodb2.example.net:27017" } ] }) test> rs.initiate( { ... _id : "rs0", ... members: [ ... { _id: 0, host: "test1.mongo.db:27017" }, ... { _id: 1, host: "test2.mongo.db:27017" }, ... { _id: 2, host: "test3.mongo.db:27017" } ... ] ... }) { ok: 1 } rs0 [direct: other] test> #查看集群配置 rs.conf() rs0 [direct: other] test> rs.conf() { _id: 'rs0', version: 1, term: 1, members: [ { _id: 0, host: 'test1.mongo.db:27017', arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1, tags: {}, secondaryDelaySecs: Long("0"), votes: 1 }, { _id: 1, host: 'test2.mongo.db:27017', arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1, tags: {}, secondaryDelaySecs: Long("0"), votes: 1 }, { _id: 2, host: 'test3.mongo.db:27017', arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1, tags: {}, secondaryDelaySecs: Long("0"), votes: 1 } ], protocolVersion: Long("1"), writeConcernMajorityJournalDefault: true, settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, catchUpTimeoutMillis: -1, catchUpTakeoverDelayMillis: 30000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 }, replicaSetId: ObjectId("639db0b39d9a0c1c17cfaf5a") } } rs0 [direct: primary] test> #查看主节点 rs.status() rs0 [direct: primary] test> rs.status() { set: 'rs0', date: ISODate("2022-12-17T12:06:57.005Z"), myState: 1, term: Long("1"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, lastCommittedWallTime: ISODate("2022-12-17T12:06:52.885Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, appliedOpTime: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, durableOpTime: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, lastAppliedWallTime: ISODate("2022-12-17T12:06:52.885Z"), lastDurableWallTime: ISODate("2022-12-17T12:06:52.885Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671278771, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'electionTimeout', lastElectionDate: ISODate("2022-12-17T12:06:22.806Z"), electionTerm: Long("1"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), numCatchUpOps: Long("0"), newTermStartDate: ISODate("2022-12-17T12:06:22.864Z"), wMajorityWriteAvailabilityDate: ISODate("2022-12-17T12:06:23.645Z") }, members: [ { _id: 0, name: 'test1.mongo.db:27017', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 142, optime: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, optimeDate: ISODate("2022-12-17T12:06:52.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:06:52.885Z"), lastDurableWallTime: ISODate("2022-12-17T12:06:52.885Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1671278782, i: 1 }), electionDate: ISODate("2022-12-17T12:06:22.000Z"), configVersion: 1, configTerm: 1, self: true, lastHeartbeatMessage: '' }, { _id: 1, name: 'test2.mongo.db:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 45, optime: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, optimeDurable: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, optimeDate: ISODate("2022-12-17T12:06:52.000Z"), optimeDurableDate: ISODate("2022-12-17T12:06:52.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:06:52.885Z"), lastDurableWallTime: ISODate("2022-12-17T12:06:52.885Z"), lastHeartbeat: ISODate("2022-12-17T12:06:56.857Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:06:55.864Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: 'test1.mongo.db:27017', syncSourceId: 0, infoMessage: '', configVersion: 1, configTerm: 1 }, { _id: 2, name: 'test3.mongo.db:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 45, optime: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, optimeDurable: { ts: Timestamp({ t: 1671278812, i: 1 }), t: Long("1") }, optimeDate: ISODate("2022-12-17T12:06:52.000Z"), optimeDurableDate: ISODate("2022-12-17T12:06:52.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:06:52.885Z"), lastDurableWallTime: ISODate("2022-12-17T12:06:52.885Z"), lastHeartbeat: ISODate("2022-12-17T12:06:56.857Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:06:55.860Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: 'test1.mongo.db:27017', syncSourceId: 0, infoMessage: '', configVersion: 1, configTerm: 1 } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671278812, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671278812, i: 1 }) } |
3.灾备测试
挂掉1之后到2 上查看集群状态如下,可以发现2自动变成了主
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
rs0 [direct: primary] test> rs.status() { set: 'rs0', date: ISODate("2022-12-17T12:18:35.005Z"), myState: 1, term: Long("2"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671279508, i: 1 }), t: Long("2") }, lastCommittedWallTime: ISODate("2022-12-17T12:18:28.862Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671279508, i: 1 }), t: Long("2") }, appliedOpTime: { ts: Timestamp({ t: 1671279508, i: 1 }), t: Long("2") }, durableOpTime: { ts: Timestamp({ t: 1671279508, i: 1 }), t: Long("2") }, lastAppliedWallTime: ISODate("2022-12-17T12:18:28.862Z"), lastDurableWallTime: ISODate("2022-12-17T12:18:28.862Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671279498, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'stepUpRequestSkipDryRun', lastElectionDate: ISODate("2022-12-17T12:17:28.838Z"), electionTerm: Long("2"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1671279442, i: 1 }), t: Long("1") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1671279442, i: 1 }), t: Long("1") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), priorPrimaryMemberId: 0, numCatchUpOps: Long("0"), newTermStartDate: ISODate("2022-12-17T12:17:28.858Z"), wMajorityWriteAvailabilityDate: ISODate("2022-12-17T12:17:29.859Z") }, electionParticipantMetrics: { votedForCandidate: true, electionTerm: Long("1"), lastVoteDate: ISODate("2022-12-17T12:06:22.621Z"), electionCandidateMemberId: 0, voteReason: '', lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, priorityAtElection: 1 }, members: [ { _id: 0, name: 'test1.mongo.db:27017', health: 0, state: 8, stateStr: '(not reachable/healthy)', uptime: 0, optime: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDurable: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDate: ISODate("1970-01-01T00:00:00.000Z"), optimeDurableDate: ISODate("1970-01-01T00:00:00.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:17:38.860Z"), lastDurableWallTime: ISODate("2022-12-17T12:17:38.860Z"), lastHeartbeat: ISODate("2022-12-17T12:18:33.050Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:17:41.884Z"), pingMs: Long("0"), lastHeartbeatMessage: 'Error connecting to test1.mongo.db:27017 (192.168.2.224:27017) :: caused by :: Connection refused', syncSourceHost: '', syncSourceId: -1, infoMessage: '', configVersion: 1, configTerm: 2 }, { _id: 1, name: 'test2.mongo.db:27017', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 839, optime: { ts: Timestamp({ t: 1671279508, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-17T12:18:28.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:18:28.862Z"), lastDurableWallTime: ISODate("2022-12-17T12:18:28.862Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1671279448, i: 1 }), electionDate: ISODate("2022-12-17T12:17:28.000Z"), configVersion: 1, configTerm: 2, self: true, lastHeartbeatMessage: '' }, { _id: 2, name: 'test3.mongo.db:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 742, optime: { ts: Timestamp({ t: 1671279508, i: 1 }), t: Long("2") }, optimeDurable: { ts: Timestamp({ t: 1671279508, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-17T12:18:28.000Z"), optimeDurableDate: ISODate("2022-12-17T12:18:28.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:18:28.862Z"), lastDurableWallTime: ISODate("2022-12-17T12:18:28.862Z"), lastHeartbeat: ISODate("2022-12-17T12:18:34.909Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:18:33.989Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: 'test2.mongo.db:27017', syncSourceId: 1, infoMessage: '', configVersion: 1, configTerm: 2 } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671279508, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671279508, i: 1 }) } |
重启1之后,再次查看,可以发现1重新加入了集群,但身份是从节点
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
rs0 [direct: primary] test> rs.status() { set: 'rs0', date: ISODate("2022-12-17T12:19:42.878Z"), myState: 1, term: Long("2"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, lastCommittedWallTime: ISODate("2022-12-17T12:19:39.170Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, appliedOpTime: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, durableOpTime: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, lastAppliedWallTime: ISODate("2022-12-17T12:19:39.170Z"), lastDurableWallTime: ISODate("2022-12-17T12:19:39.170Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671279558, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'stepUpRequestSkipDryRun', lastElectionDate: ISODate("2022-12-17T12:17:28.838Z"), electionTerm: Long("2"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1671279442, i: 1 }), t: Long("1") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1671279442, i: 1 }), t: Long("1") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), priorPrimaryMemberId: 0, numCatchUpOps: Long("0"), newTermStartDate: ISODate("2022-12-17T12:17:28.858Z"), wMajorityWriteAvailabilityDate: ISODate("2022-12-17T12:17:29.859Z") }, electionParticipantMetrics: { votedForCandidate: true, electionTerm: Long("1"), lastVoteDate: ISODate("2022-12-17T12:06:22.621Z"), electionCandidateMemberId: 0, voteReason: '', lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, priorityAtElection: 1 }, members: [ { _id: 0, name: 'test1.mongo.db:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 5, optime: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, optimeDurable: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-17T12:19:39.000Z"), optimeDurableDate: ISODate("2022-12-17T12:19:39.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:19:39.170Z"), lastDurableWallTime: ISODate("2022-12-17T12:19:39.170Z"), lastHeartbeat: ISODate("2022-12-17T12:19:41.318Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:19:41.768Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: 'test3.mongo.db:27017', syncSourceId: 2, infoMessage: '', configVersion: 1, configTerm: 2 }, { _id: 1, name: 'test2.mongo.db:27017', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 907, optime: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-17T12:19:39.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:19:39.170Z"), lastDurableWallTime: ISODate("2022-12-17T12:19:39.170Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1671279448, i: 1 }), electionDate: ISODate("2022-12-17T12:17:28.000Z"), configVersion: 1, configTerm: 2, self: true, lastHeartbeatMessage: '' }, { _id: 2, name: 'test3.mongo.db:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 810, optime: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, optimeDurable: { ts: Timestamp({ t: 1671279579, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-17T12:19:39.000Z"), optimeDurableDate: ISODate("2022-12-17T12:19:39.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:19:39.170Z"), lastDurableWallTime: ISODate("2022-12-17T12:19:39.170Z"), lastHeartbeat: ISODate("2022-12-17T12:19:40.962Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:19:42.044Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: 'test2.mongo.db:27017', syncSourceId: 1, infoMessage: '', configVersion: 1, configTerm: 2 } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671279579, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671279579, i: 1 }) } |
从2写入点新数据,到1查询测试,可以发现是正常的
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
rs0 [direct: secondary] test> use sample_mflix switched to db sample_mflix rs0 [direct: secondary] sample_mflix> db.getMongo().setReadPref('secondary') rs0 [direct: secondary] sample_mflix> db.movies.find( { title: "The Favourite1" } ) [ { _id: ObjectId("639db429d29c478bd6ec0507"), title: 'The Favourite1', genres: [ 'Drama', 'History' ], runtime: 121, rated: 'R', year: 2018, directors: [ 'Yorgos Lanthimos' ], cast: [ 'Olivia Colman', 'Emma Stone', 'Rachel Weisz' ], type: 'movie' } ] rs0 [direct: secondary] sample_mflix> |
挂掉现在的从节点3,写入新数据后再重启3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
rs0 [direct: primary] sample_mflix> rs.status() { set: 'rs0', date: ISODate("2022-12-17T12:22:41.079Z"), myState: 1, term: Long("2"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1671279758, i: 1 }), t: Long("2") }, lastCommittedWallTime: ISODate("2022-12-17T12:22:38.878Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1671279758, i: 1 }), t: Long("2") }, appliedOpTime: { ts: Timestamp({ t: 1671279758, i: 1 }), t: Long("2") }, durableOpTime: { ts: Timestamp({ t: 1671279758, i: 1 }), t: Long("2") }, lastAppliedWallTime: ISODate("2022-12-17T12:22:38.878Z"), lastDurableWallTime: ISODate("2022-12-17T12:22:38.878Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1671279738, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'stepUpRequestSkipDryRun', lastElectionDate: ISODate("2022-12-17T12:17:28.838Z"), electionTerm: Long("2"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1671279442, i: 1 }), t: Long("1") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1671279442, i: 1 }), t: Long("1") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), priorPrimaryMemberId: 0, numCatchUpOps: Long("0"), newTermStartDate: ISODate("2022-12-17T12:17:28.858Z"), wMajorityWriteAvailabilityDate: ISODate("2022-12-17T12:17:29.859Z") }, electionParticipantMetrics: { votedForCandidate: true, electionTerm: Long("1"), lastVoteDate: ISODate("2022-12-17T12:06:22.621Z"), electionCandidateMemberId: 0, voteReason: '', lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1671278771, i: 1 }), t: Long("-1") }, priorityAtElection: 1 }, members: [ { _id: 0, name: 'test1.mongo.db:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 183, optime: { ts: Timestamp({ t: 1671279748, i: 1 }), t: Long("2") }, optimeDurable: { ts: Timestamp({ t: 1671279748, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-17T12:22:28.000Z"), optimeDurableDate: ISODate("2022-12-17T12:22:28.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:22:38.878Z"), lastDurableWallTime: ISODate("2022-12-17T12:22:38.878Z"), lastHeartbeat: ISODate("2022-12-17T12:22:39.460Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:22:39.967Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: '', syncSourceId: -1, infoMessage: '', configVersion: 1, configTerm: 2 }, { _id: 1, name: 'test2.mongo.db:27017', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 1086, optime: { ts: Timestamp({ t: 1671279758, i: 1 }), t: Long("2") }, optimeDate: ISODate("2022-12-17T12:22:38.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:22:38.878Z"), lastDurableWallTime: ISODate("2022-12-17T12:22:38.878Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1671279448, i: 1 }), electionDate: ISODate("2022-12-17T12:17:28.000Z"), configVersion: 1, configTerm: 2, self: true, lastHeartbeatMessage: '' }, { _id: 2, name: 'test3.mongo.db:27017', health: 0, state: 8, stateStr: '(not reachable/healthy)', uptime: 0, optime: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDurable: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") }, optimeDate: ISODate("1970-01-01T00:00:00.000Z"), optimeDurableDate: ISODate("1970-01-01T00:00:00.000Z"), lastAppliedWallTime: ISODate("2022-12-17T12:22:28.878Z"), lastDurableWallTime: ISODate("2022-12-17T12:22:28.878Z"), lastHeartbeat: ISODate("2022-12-17T12:22:39.106Z"), lastHeartbeatRecv: ISODate("2022-12-17T12:22:34.175Z"), pingMs: Long("0"), lastHeartbeatMessage: 'Error connecting to test3.mongo.db:27017 (192.168.2.178:27017) :: caused by :: Connection refused', syncSourceHost: '', syncSourceId: -1, infoMessage: '', configVersion: 1, configTerm: 2 } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1671279758, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1671279758, i: 1 }) } |
可以发现新写入的数据依然可以正常查询
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
rs0 [direct: secondary] sample_mflix> db.getMongo().setReadPref('secondary') rs0 [direct: secondary] sample_mflix> db.movies.find( { title: "The Favourite2" } ) [ { _id: ObjectId("639db4b7d29c478bd6ec0508"), title: 'The Favourite2', genres: [ 'Drama', 'History' ], runtime: 121, rated: 'R', year: 2018, directors: [ 'Yorgos Lanthimos' ], cast: [ 'Olivia Colman', 'Emma Stone', 'Rachel Weisz' ], type: 'movie' } ] |
附录
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
mongo https://mirrors.tuna.tsinghua.edu.cn #分配DNS 192.168.2.224 test1.mongo.db 192.168.2.147 test2.mongo.db 192.168.2.178 test3.mongo.db #远程ssh+免密+禁用密码登录 ssh mongo1@test1.mongo.db ssh mongo2@test2.mongo.db ssh mongo3@test3.mongo.db #安装mongo wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - sudo touch /etc/apt/sources.list.d/mongodb-org-6.0.list echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt-get update sudo apt-get install -y mongodb-org #单机测试 mongosh 端口 27017 db use test #集群配置 修改每个机器的bindIp sudo vim /etc/mongod.conf sudo service mongod restart #尝试远程连接 mongosh test1.mongo.db mongosh test2.mongo.db mongosh test3.mongo.db #配置集群名 replSetName: "rs0" sudo vim /etc/mongod.conf sudo service mongod restart #初始化集群 rs.initiate( { _id : "rs0", members: [ { _id: 0, host: "test1.mongo.db:27017" }, { _id: 1, host: "test2.mongo.db:27017" }, { _id: 2, host: "test3.mongo.db:27017" } ] }) rs.conf() rs.status() #集群测试 use sample_mflix db.movies.insertOne( { title: "The Favourite", genres: [ "Drama", "History" ], runtime: 121, rated: "R", year: 2018, directors: [ "Yorgos Lanthimos" ], cast: [ "Olivia Colman", "Emma Stone", "Rachel Weisz" ], type: "movie" } ) db.getMongo().setReadPref('secondary') db.movies.find( { title: "The Favourite" } ) |
参考
https://www.mongodb.com/docs/manual/
https://www.mongodb.com/docs/manual/replication/
https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/
0 Comments