使用Mongoose连接Docker中MongoDB副本集遇ReplicaSetNoPrimary错误
Docker搭建MongoDB副本集后Mongoose连接失败排查
使用软件版本
- mongo v6.0.2
- mongoose v6.9.1
- docker 20.10.12-0ubuntu4
Docker搭建MongoDB副本集步骤
- 创建Docker网络:
docker network create mongo-cluster
- 启动三个Mongo实例:
docker run \ -p 30001:27017 \ --name mongo1 \ --net mongo-cluster \ mongo mongod --replSet rs0 docker run \ -p 30002:27017 \ --name mongo2 \ --net mongo-cluster \ mongo mongod --replSet rs0 docker run \ -p 30003:27017 \ --name mongo3 \ --net mongo-cluster \ mongo mongod --replSet rs0
- 初始化副本集配置:
config = { "_id" : "rs0", "members" : [ {"_id" : 0, "host" : "mongo1:27017"}, {"_id" : 1, "host" : "mongo2:27017"}, {"_id" : 2, "host" : "mongo3:27017"} ] } rs.initiate(config) // result : ok: 1
- 设置mongo1为主节点:
db.getMongo().setReadPref()
Mongoose连接配置及错误
使用以下代码连接副本集:
await mongoose.connect(`mongodb://mongo1:30001,mongo2:30002,mongo3:30003/chatappdb`, { replicaSet: 'rs0', readPreference: 'primary', w: 'majority' }
出现错误:
Uncaught [MongoServerSelectionError: getaddrinfo EAI_AGAIN mongo1] { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) { 'mongo1:27017' => [ServerDescription], 'mongo2:27017' => [ServerDescription], 'mongo3:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: 'rs0', maxElectionId: new ObjectId("7fffffff0000000000000001"), maxSetVersion: 1, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined, [Symbol(errorLabels)]: Set(0) {} }
副本集状态检查
通过rs.status()检查集群状态,结果显示主节点已正常定义:
{ set: 'rs0', date: ISODate("2023-02-23T23:27:05.901Z"), myState: 1, term: Long("1"), syncSourceHost: '', syncSourceId: -1, heartbeatIntervalMillis: Long("2000"), majorityVoteCount: 2, writeMajorityCount: 2, votingMembersCount: 3, writableVotingMembersCount: 3, optimes: { lastCommittedOpTime: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, lastCommittedWallTime: ISODate("2023-02-23T23:26:57.517Z"), readConcernMajorityOpTime: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, appliedOpTime: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, durableOpTime: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, lastAppliedWallTime: ISODate("2023-02-23T23:26:57.517Z"), lastDurableWallTime: ISODate("2023-02-23T23:26:57.517Z") }, lastStableRecoveryTimestamp: Timestamp({ t: 1677194797, i: 1 }), electionCandidateMetrics: { lastElectionReason: 'electionTimeout', lastElectionDate: ISODate("2023-02-23T21:40:47.293Z"), electionTerm: Long("1"), lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1677188435, i: 1 }), t: Long("-1") }, lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1677188435, i: 1 }), t: Long("-1") }, numVotesNeeded: 2, priorityAtElection: 1, electionTimeoutMillis: Long("10000"), numCatchUpOps: Long("0"), newTermStartDate: ISODate("2023-02-23T21:40:47.337Z"), wMajorityWriteAvailabilityDate: ISODate("2023-02-23T21:40:48.598Z") }, members: [ { _id: 0, name: 'mongo1:27017', health: 1, state: 1, stateStr: 'PRIMARY', uptime: 6518, optime: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, optimeDate: ISODate("2023-02-23T23:26:57.000Z"), lastAppliedWallTime: ISODate("2023-02-23T23:26:57.517Z"), lastDurableWallTime: ISODate("2023-02-23T23:26:57.517Z"), syncSourceHost: '', syncSourceId: -1, infoMessage: '', electionTime: Timestamp({ t: 1677188447, i: 1 }), electionDate: ISODate("2023-02-23T21:40:47.000Z"), configVersion: 1, configTerm: 1, self: true, lastHeartbeatMessage: '' }, { _id: 1, name: 'mongo2:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 6389, optime: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, optimeDurable: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, optimeDate: ISODate("2023-02-23T23:26:57.000Z"), optimeDurableDate: ISODate("2023-02-23T23:26:57.000Z"), lastAppliedWallTime: ISODate("2023-02-23T23:26:57.517Z"), lastDurableWallTime: ISODate("2023-02-23T23:26:57.517Z"), lastHeartbeat: ISODate("2023-02-23T23:27:05.316Z"), lastHeartbeatRecv: ISODate("2023-02-23T23:27:04.810Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: 'mongo1:27017', syncSourceId: 0, infoMessage: '', configVersion: 1, configTerm: 1 }, { _id: 2, name: 'mongo3:27017', health: 1, state: 2, stateStr: 'SECONDARY', uptime: 6389, optime: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, optimeDurable: { ts: Timestamp({ t: 1677194817, i: 1 }), t: Long("1") }, optimeDate: ISODate("2023-02-23T23:26:57.000Z"), optimeDurableDate: ISODate("2023-02-23T23:26:57.000Z"), lastAppliedWallTime: ISODate("2023-02-23T23:26:57.517Z"), lastDurableWallTime: ISODate("2023-02-23T23:26:57.517Z"), lastHeartbeat: ISODate("2023-02-23T23:27:05.309Z"), lastHeartbeatRecv: ISODate("2023-02-23T23:27:04.811Z"), pingMs: Long("0"), lastHeartbeatMessage: '', syncSourceHost: 'mongo1:27017', syncSourceId: 0, infoMessage: '', configVersion: 1, configTerm: 1 } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1677194817, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1677194817, i: 1 }) }
额外尝试及日志
尝试用MongoClient连接:
let newClient = new MongoClient("mongodb://mongo1:30001,mongo2:30002,mongo3:30003/chatappdb?replicaSet=rs0", { useNewUrlParser: true, useUnifiedTopology: true })
仍出现相同错误。MongoDB运行日志显示连接突然中断:
{"t":{"$date":"2023-02-23T23:40:30.888+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"#","uuid":"#","connectionId":92,"connectionCount":29}} {"t":{"$date":"2023-02-23T23:40:30.890+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn92","msg":"client metadata","attr":{"remote":"#","client":"conn92","doc":{"driver":{"name":"nodejs|Mongoose","version":"4.13.0"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"6.1.11-76060111-generic"},"platform":"Node.js v18.12.0, LE (unified)","version":"4.13.0|6.9.1"}}} {"t":{"$date":"2023-02-23T23:40:30.893+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn92","msg":"Connection ended","attr":{"remote":"#","uuid":"#","connectionId":92,"connectionCount":28}}
内容的提问来源于stack exchange,提问作者Devinou971
相关产品推荐
相关产品推荐

