pymongo 无法连接到主节点

pymongo unable to connect to primary

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

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

但是会导致错误:

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

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

rs.status:

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
}

我使用这个连接字符串:

 MongoClient('mongodb://mongo01.db.com:27017,mongo02.db.com:27017,mongo03.db.com:27017/mydb',replicaSet="rs0", read_preference=ReadPreference.PRIMARY)

在错误消息类似于 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 主机之间的连接,以确定这是否是导致问题的原因。