1、在宿主機創造對應的文件夾
mkdir mongo1Data mongo2Data mongo3Data
2、在docker中啓動3個mongo容器
docker network create mongoCluster
docker run -d --rm -p 27018:27017 -v /home/mongo1Data:/data/db --name mongo1 --network mongoCluster mongo mongod --replSet rs0 --bind_ip_all
docker run -d --rm -p 27019:27017 -v /home/mongo2Data:/data/db --name mongo2 --network mongoCluster mongo mongod --replSet rs0 --bind_ip_all
docker run -d --rm -p 27020:27017 -v /home/mongo3Data:/data/db --name mongo3 --network mongoCluster mongo mongod --replSet rs0 --bind_ip_all
3、啓動複製集
docker exec -it mongo1 mongosh --eval "rs.initiate({
_id: \"rs0\",
members: [
{_id: 0, host: \"192.168.100.99:27018\"},
{_id: 1, host: \"192.168.100.99:27019\"},
{_id: 2, host: \"192.168.100.99:27020\"}
]
})"
ok:1 表示成功
執行docker exec -it mongo1 mongosh --eval "rs.status()" 查看狀態
https://github.com/browserslist/browserslist#browsers-data-updating
{
set: 'rs0',
date: ISODate("2022-03-07T07:35:14.990Z"),
myState: 1,
term: Long("1"),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long("2000"),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 3,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
lastCommittedWallTime: ISODate("2022-03-07T07:35:12.676Z"),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
appliedOpTime: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
durableOpTime: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
lastAppliedWallTime: ISODate("2022-03-07T07:35:12.676Z"),
lastDurableWallTime: ISODate("2022-03-07T07:35:12.676Z")
},
lastStableRecoveryTimestamp: Timestamp({ t: 1646638472, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate("2022-03-07T07:34:42.614Z"),
electionTerm: Long("1"),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1646638472, i: 1 }), t: Long("-1") },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1646638472, i: 1 }), t: Long("-1") },
numVotesNeeded: 2,
priorityAtElection: 1,
electionTimeoutMillis: Long("10000"),
numCatchUpOps: Long("0"),
newTermStartDate: ISODate("2022-03-07T07:34:42.661Z"),
wMajorityWriteAvailabilityDate: ISODate("2022-03-07T07:34:43.826Z")
},
members: [
{
_id: 0,
name: '192.168.100.99:27018',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 83,
optime: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
optimeDate: ISODate("2022-03-07T07:35:12.000Z"),
lastAppliedWallTime: ISODate("2022-03-07T07:35:12.676Z"),
lastDurableWallTime: ISODate("2022-03-07T07:35:12.676Z"),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: 'Could not find member to sync from',
electionTime: Timestamp({ t: 1646638482, i: 1 }),
electionDate: ISODate("2022-03-07T07:34:42.000Z"),
configVersion: 1,
configTerm: 1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '192.168.100.99:27019',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 42,
optime: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
optimeDate: ISODate("2022-03-07T07:35:12.000Z"),
optimeDurableDate: ISODate("2022-03-07T07:35:12.000Z"),
lastAppliedWallTime: ISODate("2022-03-07T07:35:12.676Z"),
lastDurableWallTime: ISODate("2022-03-07T07:35:12.676Z"),
lastHeartbeat: ISODate("2022-03-07T07:35:14.671Z"),
lastHeartbeatRecv: ISODate("2022-03-07T07:35:14.172Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.100.99:27018',
syncSourceId: 0,
infoMessage: '',
configVersion: 1,
configTerm: 1
},
{
_id: 2,
name: '192.168.100.99:27020',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 42,
optime: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1646638512, i: 1 }), t: Long("1") },
optimeDate: ISODate("2022-03-07T07:35:12.000Z"),
optimeDurableDate: ISODate("2022-03-07T07:35:12.000Z"),
lastAppliedWallTime: ISODate("2022-03-07T07:35:12.676Z"),
lastDurableWallTime: ISODate("2022-03-07T07:35:12.676Z"),
lastHeartbeat: ISODate("2022-03-07T07:35:14.672Z"),
lastHeartbeatRecv: ISODate("2022-03-07T07:35:14.193Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.100.99:27018',
syncSourceId: 0,
infoMessage: '',
configVersion: 1,
configTerm: 1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1646638512, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1646638512, i: 1 })
}
可以看出mongo1爲PRIMARY
進入mongo1 插入數據
進入mongo2 查看數據
默認從節點不支持讀的操作
執行secondaryOk() 讓節點可以查詢