mongodb Mongo副本“NotPrimaryNoSecondaryOk”

gxwragnw  于 2023-11-17  发布在  Go
关注(0)|答案(4)|浏览(361)

我已经花了这个星期,使Mongo 4.4 PSA复制品由自由职业者创建的工作.我放弃了,从所有三个服务器删除完整的mongod和从头开始安装以下Mongo doc .唯一的变化是创建新的数据库和复制品初始化之前导入数据.
它第一次失败(连接超时),我重新查看了我的防火墙规则。然后它立即连接,mongo shell在所有节点上刷新:

rs_bud:SECONDARY> show collections
rs_bud:PRIMARY> use bud

字符串
但是当我在secondary上执行任何操作时,它们都会失败,并出现NotPrimaryNoSecondaryOk错误。

rs_bud:SECONDARY> use bud
rs_bud:SECONDARY> show collections
uncaught exception: Error: listCollections failed: {
        "topologyVersion" : {
                "processId" : ObjectId("612bbc4940995c508859973a"),
                "counter" : NumberLong(4)
        },
        "operationTime" : Timestamp(1630258863, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotPrimaryNoSecondaryOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1630258863, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :


我找不到对我有用的日志:

{"t":{"$date":"2021-08-29T19:41:41.550+02:00"},"s":"I",  "c":"NETWORK",  "id":23729,   "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"ServerPingMonitor is now monitoring host","attr":{"host":"10.0.0.4:27017","replicaSet":"rs_bud"}}
{"t":{"$date":"2021-08-29T19:41:41.550+02:00"},"s":"I",  "c":"NETWORK",  "id":4333213, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"RSM Topology Change","attr":{"replicaSet":"rs_bud","newTopologyDescription":"{ id: \"ad6bc985-50f4-4b01-9310-1457f8861f1f\", topologyType: \"ReplicaSetWithPrimary\", servers: { 10.0.0.2:27017: { address: \"10.0.0.2:27017\", topologyVersion: { processId: ObjectId('612bbc4940995c508859973a'), counter: 4 }, roundTripTime: 504, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSSecondary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.2:27017\", setName: \"rs_bud\", setVersion: 1, primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901549), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} }, 10.0.0.4:27017: { address: \"10.0.0.4:27017\", topologyVersion: { processId: ObjectId('612bba160d5e82841e5c8fc2'), counter: 6 }, roundTripTime: 1153, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSPrimary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.4:27017\", setName: \"rs_bud\", setVersion: 1, electionId: ObjectId('7fffffff0000000000000001'), primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901550), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} }, 10.0.0.5:27017: { address: \"10.0.0.5:27017\", type: \"Unknown\", minWireVersion: 0, maxWireVersion: 0, lastUpdateTime: new Date(-9223372036854775808), hosts: {}, arbiters: {}, passives: {} } }, logicalSessionTimeoutMinutes: 30, setName: \"rs_bud\", compatible: true, maxSetVersion: 1, maxElectionId: ObjectId('7fffffff0000000000000001') }","previousTopologyDescription":"{ id: \"ad6bc985-50f4-4b01-9310-1457f8861f1f\", topologyType: \"ReplicaSetNoPrimary\", servers: { 10.0.0.2:27017: { address: \"10.0.0.2:27017\", topologyVersion: { processId: ObjectId('612bbc4940995c508859973a'), counter: 4 }, roundTripTime: 504, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSSecondary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.2:27017\", setName: \"rs_bud\", setVersion: 1, primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901549), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} }, 10.0.0.4:27017: { address: \"10.0.0.4:27017\", type: \"Unknown\", minWireVersion: 0, maxWireVersion: 0, lastUpdateTime: new Date(-9223372036854775808), hosts: {}, arbiters: {}, passives: {} }, 10.0.0.5:27017: { address: \"10.0.0.5:27017\", type: \"Unknown\", minWireVersion: 0, maxWireVersion: 0, lastUpdateTime: new Date(-9223372036854775808), hosts: {}, arbiters: {}, passives: {} } }, logicalSessionTimeoutMinutes: 30, setName: \"rs_bud\", compatible: true }"}}
{"t":{"$date":"2021-08-29T19:41:41.550+02:00"},"s":"I",  "c":"NETWORK",  "id":23729,   "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"ServerPingMonitor is now monitoring host","attr":{"host":"10.0.0.5:27017","replicaSet":"rs_bud"}}
{"t":{"$date":"2021-08-29T19:41:41.551+02:00"},"s":"I",  "c":"NETWORK",  "id":4333213, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"RSM Topology Change","attr":{"replicaSet":"rs_bud","newTopologyDescription":"{ id: \"ad6bc985-50f4-4b01-9310-1457f8861f1f\", topologyType: \"ReplicaSetWithPrimary\", servers: { 10.0.0.2:27017: { address: \"10.0.0.2:27017\", topologyVersion: { processId: ObjectId('612bbc4940995c508859973a'), counter: 4 }, roundTripTime: 504, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSSecondary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.2:27017\", setName: \"rs_bud\", setVersion: 1, primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901549), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} }, 10.0.0.4:27017: { address: \"10.0.0.4:27017\", topologyVersion: { processId: ObjectId('612bba160d5e82841e5c8fc2'), counter: 6 }, roundTripTime: 1153, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSPrimary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.4:27017\", setName: \"rs_bud\", setVersion: 1, electionId: ObjectId('7fffffff0000000000000001'), primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901550), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} }, 10.0.0.5:27017: { address: \"10.0.0.5:27017\", topologyVersion: { processId: ObjectId('612bbc3d5c7d3123b83eedf8'), counter: 4 }, roundTripTime: 1468, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSSecondary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.5:27017\", setName: \"rs_bud\", setVersion: 1, primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901550), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} } }, logicalSessionTimeoutMinutes: 30, setName: \"rs_bud\", compatible: true, maxSetVersion: 1, maxElectionId: ObjectId('7fffffff0000000000000001') }","previousTopologyDescription":"{ id: \"ad6bc985-50f4-4b01-9310-1457f8861f1f\", topologyType: \"ReplicaSetWithPrimary\", servers: { 10.0.0.2:27017: { address: \"10.0.0.2:27017\", topologyVersion: { processId: ObjectId('612bbc4940995c508859973a'), counter: 4 }, roundTripTime: 504, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSSecondary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.2:27017\", setName: \"rs_bud\", setVersion: 1, primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901549), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} }, 10.0.0.4:27017: { address: \"10.0.0.4:27017\", topologyVersion: { processId: ObjectId('612bba160d5e82841e5c8fc2'), counter: 6 }, roundTripTime: 1153, lastWriteDate: new Date(1630258890000), opTime: { ts: Timestamp(1630258890, 1), t: 1 }, type: \"RSPrimary\", minWireVersion: 9, maxWireVersion: 9, me: \"10.0.0.4:27017\", setName: \"rs_bud\", setVersion: 1, electionId: ObjectId('7fffffff0000000000000001'), primary: \"10.0.0.4:27017\", lastUpdateTime: new Date(1630258901550), logicalSessionTimeoutMinutes: 30, hosts: { 0: \"10.0.0.2:27017\", 1: \"10.0.0.4:27017\", 2: \"10.0.0.5:27017\" }, arbiters: {}, passives: {} }, 10.0.0.5:27017: { address: \"10.0.0.5:27017\", type: \"Unknown\", minWireVersion: 0, maxWireVersion: 0, lastUpdateTime: new Date(-9223372036854775808), hosts: {}, arbiters: {}, passives: {} } }, logicalSessionTimeoutMinutes: 30, setName: \"rs_bud\", compatible: true, maxSetVersion: 1, maxElectionId: ObjectId('7fffffff0000000000000001') }"}}
{"t":{"$date":"2021-08-29T19:41:41.552+02:00"},"s":"I",  "c":"NETWORK",  "id":20113,   "ctx":"LogicalSessionCacheReap","msg":"Successfully connected to host","attr":{"connString":"10.0.0.4:27017","numOpenConns":1,"socketTimeoutSecs":0.0}}
{"t":{"$date":"2021-08-29T19:41:41.552+02:00"},"s":"I",  "c":"NETWORK",  "id":20113,   "ctx":"LogicalSessionCacheRefresh","msg":"Successfully connected to host","attr":{"connString":"10.0.0.4:27017","numOpenConns":2,"socketTimeoutSecs":0.0}}
{"t":{"$date":"2021-08-29T19:41:54.623+02:00"},"s":"I",  "c":"STORAGE",  "id":22430,   "ctx":"WTCheckpointThread","msg":"WiredTiger message","attr":{"message":"[1630258914:623411][1811:0x7f64db1aa700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 1607, snapshot max: 1607 snapshot count: 0, oldest timestamp: (1630258896, 1) , meta checkpoint timestamp: (1630258901, 1) base write gen: 325"}}


这个看起来也不错:

rs_bud:SECONDARY> rs.printReplicationInfo()
configured oplog size:   1483.0916013717651MB
log length start to end: 981secs (0.27hrs)
oplog first event time:  Sun Aug 29 2021 19:39:42 GMT+0200 (CEST)
oplog last event time:   Sun Aug 29 2021 19:56:03 GMT+0200 (CEST)
now:                     Sun Aug 29 2021 19:56:04 GMT+0200 (CEST)


rs_bud:SECONDARY> rs.status()
{
        "set" : "rs_bud",
        "date" : ISODate("2021-08-29T17:56:58.119Z"),
        "myState" : 2,
        "term" : NumberLong(1),
        "syncSourceHost" : "10.0.0.4:27017",
        "syncSourceId" : 0,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1630259813, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2021-08-29T17:56:53.431Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1630259813, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2021-08-29T17:56:53.431Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1630259813, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1630259813, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2021-08-29T17:56:53.431Z"),
                "lastDurableWallTime" : ISODate("2021-08-29T17:56:53.431Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1630259801, 2),
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(1),
                "lastVoteDate" : ISODate("2021-08-29T17:39:53.358Z"),
                "electionCandidateMemberId" : 0,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1630258782, 1),
                        "t" : NumberLong(-1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1630258782, 1),
                        "t" : NumberLong(-1)
                },
                "priorityAtElection" : 1,
                "newTermStartDate" : ISODate("2021-08-29T17:39:53.386Z"),
                "newTermAppliedDate" : ISODate("2021-08-29T17:39:54.485Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.0.0.4:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1035,
                        "optime" : {
                                "ts" : Timestamp(1630259813, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1630259813, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-08-29T17:56:53Z"),
                        "optimeDurableDate" : ISODate("2021-08-29T17:56:53Z"),
                        "lastHeartbeat" : ISODate("2021-08-29T17:56:57.415Z"),
                        "lastHeartbeatRecv" : ISODate("2021-08-29T17:56:57.901Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1630258793, 1),
                        "electionDate" : ISODate("2021-08-29T17:39:53Z"),
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 1,
                        "name" : "10.0.0.5:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1035,
                        "optime" : {
                                "ts" : Timestamp(1630259813, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1630259813, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-08-29T17:56:53Z"),
                        "optimeDurableDate" : ISODate("2021-08-29T17:56:53Z"),
                        "lastHeartbeat" : ISODate("2021-08-29T17:56:57.417Z"),
                        "lastHeartbeatRecv" : ISODate("2021-08-29T17:56:57.431Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "10.0.0.4:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 2,
                        "name" : "10.0.0.2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 3617,
                        "optime" : {
                                "ts" : Timestamp(1630259813, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-08-29T17:56:53Z"),
                        "syncSourceHost" : "10.0.0.4:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1630259813, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1630259813, 1)
}


什么是错误的,如何修复它?

bsxbgnwa

bsxbgnwa1#

rs.secondaryOk()

字符串
此命令启动了集群节点中的辅助节点,并能够在mongo上运行查询

axkjgtzd

axkjgtzd2#

正如上面提到的,你只能从secondary读取,这个行为可以通过read-preference选项来配置(这是否是一个好主意取决于你的目标)。但是,如果你设置directConnection=true,你仍然可以直接连接到secondary,但是你应该清楚地理解为什么你需要这样做。

brtdzjyr

brtdzjyr3#

您需要“连接到副本集”。如何做到这一点取决于驱动程序,例如这里的Ruby。当您这样做时,驱动程序将把操作路由到正确的服务器(例如,所有写入将被发送到当前的主服务器)。

bgtovc5b

bgtovc5b4#

在数据库连接中添加标志readPreference=secondary后,它对我很有效。

相关问题