gpt4 book ai didi

mongodb - pymongo 无法连接到主节点

转载 作者:可可西里 更新时间:2023-11-01 10:21:46 28 4
gpt4 key购买 nike

我正在尝试通过连接到我的副本集的主节点来find_one

MongoClient(hostname, replicaSet="rs0", read_preference=ReadPreference.PRIMARY)

但它会导致错误:

ServerSelectionTimeoutError: No replica set members match selector "Primary()"

我能够使用 SECONDARY_PREFERRED 成功阅读。我还尝试使用 MongoReplicaSetClient 进行连接,但没有成功。我猜这是由于配置不当造成的,但我应该寻找什么?

rs.状态:

rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 111313,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 1,
"host" : "ANDROMEDA:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongo02.db.com:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0.5,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "mongo03.db.com:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0.5,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
rs0:SECONDARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2016-08-04T08:58:02.293Z"),
"myState" : 2,
"term" : NumberLong(90),
"syncingTo" : "mongo03.db.com:27017",
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 1,
"name" : "ANDROMEDA:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2503,
"optime" : {
"ts" : Timestamp(1470299746, 1),
"t" : NumberLong(90)
},
"optimeDate" : ISODate("2016-08-04T08:35:46Z"),
"lastHeartbeat" : ISODate("2016-08-04T08:58:01.109Z"),
"lastHeartbeatRecv" : ISODate("2016-08-04T08:58:01.803Z"),
"pingMs" : NumberLong(28),
"electionTime" : Timestamp(1469600522, 1),
"electionDate" : ISODate("2016-07-27T06:22:02Z"),
"configVersion" : 111313
},
{
"_id" : 2,
"name" : "mongo02.db.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 7604104,
"optime" : {
"ts" : Timestamp(1470299746, 1),
"t" : NumberLong(90)
},
"optimeDate" : ISODate("2016-08-04T08:35:46Z"),
"syncingTo" : "mongo03.db.com:27017",
"configVersion" : 111313,
"self" : true
},
{
"_id" : 3,
"name" : "mongo03.db.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2503,
"optime" : {
"ts" : Timestamp(1470299746, 1),
"t" : NumberLong(90)
},
"optimeDate" : ISODate("2016-08-04T08:35:46Z"),
"lastHeartbeat" : ISODate("2016-08-04T08:58:01.948Z"),
"lastHeartbeatRecv" : ISODate("2016-08-04T08:58:01.802Z"),
"pingMs" : NumberLong(28),
"syncingTo" : "ANDROMEDA:27017",
"configVersion" : 111313
}
],
"ok" : 1
}

最佳答案

如果出现类似于 ServerSelectionTimeoutError: No replica set members match selector "Primary()" 的错误消息并且副本集状态函数输出,rs.status() , 显示主要成员使用非 FQDN(ANDROMEDA:27017 在这种情况下),那么应用程序很可能无法解析网络上的主要主机。

您可以从运行应用程序的主机的命令行使用以下命令轻松检查这一点:

$ dig ANDROMEDA
$ ping ANDROMEDA
$ mongo --host ANDROMEDA:27017

如果您没有在运行应用程序的主机上安装 Mongo Shell,您可以改用 Telnet。

$ telnet ANDROMEDA 27017

这些输出将允许您检查您的应用程序主机和您的 mongod 主机之间的连接,以确定这是否是导致问题的原因。

关于mongodb - pymongo 无法连接到主节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38762897/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com