#进入主节点
mongosh 主机名(ip):port
rs.initiate() 进入节点配置
回车一直到现在primary 模式
rs0 [direct: primary] test>
rs.status() 查看主节点的信息
有一个members属性 显示的是主节点有哪些节点
{
set: 'rs0',
date: ISODate("2023-05-31T06:19:01.043Z"),
myState: 2,
term: Long("1"),
syncSourceHost: 'zyj:28017',
syncSourceId: 0,
heartbeatIntervalMillis: Long("2000"),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 3,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
lastCommittedWallTime: ISODate("2023-05-31T06:18:59.097Z"),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
appliedOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
durableOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
lastAppliedWallTime: ISODate("2023-05-31T06:18:59.097Z"),
lastDurableWallTime: ISODate("2023-05-31T06:18:59.097Z")
},
lastStableRecoveryTimestamp: Timestamp({ t: 1685513919, i: 1 }),
members: [
{
_id: 0,
name: 'zyj:28017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 9333,
optime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-05-31T06:18:59.000Z"),
optimeDurableDate: ISODate("2023-05-31T06:18:59.000Z"),
lastAppliedWallTime: ISODate("2023-05-31T06:18:59.097Z"),
lastDurableWallTime: ISODate("2023-05-31T06:18:59.097Z"),
lastHeartbeat: ISODate("2023-05-31T06:19:00.716Z"),
lastHeartbeatRecv: ISODate("2023-05-31T06:19:00.718Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1685503988, i: 2 }),
electionDate: ISODate("2023-05-31T03:33:08.000Z"),
configVersion: 5,
configTerm: 1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1685513939, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1685513939, i: 1 })
}
rs.add("主机名:port")
![[Pasted image 20230531114343.png]]
##现在主节点的成员节点变成了2个
members: [
{
_id: 0,
name: 'zyj:28017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 1329,
optime: { ts: Timestamp({ t: 1685504628, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-05-31T03:43:48.000Z"),
lastAppliedWallTime: ISODate("2023-05-31T03:43:48.781Z"),
lastDurableWallTime: ISODate("2023-05-31T03:43:48.781Z"),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1685503988, i: 2 }),
electionDate: ISODate("2023-05-31T03:33:08.000Z"),
configVersion: 3,
configTerm: 1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: 'zyj:28018',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 24,
optime: { ts: Timestamp({ t: 1685504628, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1685504628, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-05-31T03:43:48.000Z"),
optimeDurableDate: ISODate("2023-05-31T03:43:48.000Z"),
lastAppliedWallTime: ISODate("2023-05-31T03:43:48.781Z"),
lastDurableWallTime: ISODate("2023-05-31T03:43:48.781Z"),
lastHeartbeat: ISODate("2023-05-31T03:43:51.166Z"),
lastHeartbeatRecv: ISODate("2023-05-31T03:43:50.167Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: 'zyj:28017',
syncSourceId: 0,
infoMessage: '',
configVersion: 3,
configTerm: 1
}
],
验证
# 第一次进入从节点会报错
rs0 [direct: secondary] test> db.test.find()
MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string
# 需要设置从节点允许读操作
db.getMongo().setReadPref("secondary")
# 现在查询就不会报错了
rs0 [direct: secondary] test> db.fruit.findOne()
{ _id: ObjectId("6476c59c7776dcfd03e5b6d0"), name: 'apple' }