Pymongo读取首选项 - ServerSelectionTimeoutError:没有主要可用于写入

时间:2016-11-23 13:16:44

标签: python mongodb python-3.x pymongo pymongo-3.x

当连接到MongoDB副本集时,我能够从辅助节点读取,但之后,当使用相同的连接写入时,我收到以下错误:

pymongo.errors.ServerSelectionTimeoutError: No primary available for writes

Python 3.4.3 Pymongo 3.2.2

我的联系:

var_host = 'XXX.XXX.XXX.XXX' # Public IP
var_port = 27017
var_username = 'username'
var_password = 'password'
mongo_uri = 'mongodb://%s:%s@%s:%s' % (username, password, host, port)
client_db = MongoClient(mongo_uri,
                 replicaSet='rs0',
                 readPreference='secondaryPreferred')

重要提示:删除'replicaSet'和'readPrefence'字段时,连接有效。

在服务器中:

rs0:PRIMARY> rs.status()
{"set" : "rs0",
 "date" : ISODate("2016-11-23T13:09:55.699Z"),
 "myState" : 1,
 "term" : NumberLong(4),
 "heartbeatIntervalMillis" : NumberLong(2000),
 "members" : [
    {
        "_id" : 0,
        "name" : "mongodb0:27017",
        "health" : 1,
        "state" : 1,
        "stateStr" : "PRIMARY",
        "uptime" : 64491,
        "optime" : {
            "ts" : Timestamp(1479906595, 21),
            "t" : NumberLong(4)
        },
        "optimeDate" : ISODate("2016-11-23T13:09:55Z"),
        "electionTime" : Timestamp(1479844637, 1),
        "electionDate" : ISODate("2016-11-22T19:57:17Z"),
        "configVersion" : 424888,
        "self" : true
    },
    {
        "_id" : 1,
        "name" : "mongodb2:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 61631,
        "optime" : {
            "ts" : Timestamp(1479906593, 31),
            "t" : NumberLong(4)
        },
        "optimeDate" : ISODate("2016-11-23T13:09:53Z"),
        "lastHeartbeat" : ISODate("2016-11-23T13:09:53.776Z"),
        "lastHeartbeatRecv" : ISODate("2016-11-23T13:09:53.758Z"),
        "pingMs" : NumberLong(0),
        "syncingTo" : "XXXXXXXX:27017",
        "configVersion" : 424888
    },
    {
        "_id" : 2,
        "name" : "mongodb3:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 61264,
        "optime" : {
            "ts" : Timestamp(1479906593, 31),
            "t" : NumberLong(4)
        },
        "optimeDate" : ISODate("2016-11-23T13:09:53Z"),
        "lastHeartbeat" : ISODate("2016-11-23T13:09:53.776Z"),
        "lastHeartbeatRecv" : ISODate("2016-11-23T13:09:55.493Z"),
        "pingMs" : NumberLong(0),
        "syncingTo" : "XXX:27017",
        "configVersion" : 424888
    }
   ],
   "ok" : 1}

db.isMaster()

mongodb0

rs0:PRIMARY> db.isMaster()
{
"hosts" : [
    "mongodb0:27017"
],
"passives" : [
    "10.150.151.141:27017",
    "10.150.151.155:27017"
],
"setName" : "rs0",
"setVersion" : 424888,
"ismaster" : true,
"secondary" : false,
"primary" : "mongodb0:27017",
"me" : "mongodb0:27017",
"electionId" : ObjectId("7fffffff0000000000000004"),
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2016-11-23T15:12:37.988Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}

mongodb1

rs0:SECONDARY> db.isMaster()
{
"hosts" : [
    "mongodb0:27017"
],
"passives" : [
    "10.150.151.141:27017",
    "10.150.151.155:27017"
],
"setName" : "rs0",
"setVersion" : 424888,
"ismaster" : false,
"secondary" : true,
"primary" : "mongodb0:27017",
"passive" : true,
"me" : "10.150.151.141:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2016-11-23T15:24:32.134Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1

mongodb2

 rs0:SECONDARY> db.isMaster()
 {
 "hosts" : [
    "mongodb0:27017"
 ],
 "passives" : [
    "10.150.151.141:27017",
    "10.150.151.155:27017"
 ],
"setName" : "rs0",
 "setVersion" : 424888,
"ismaster" : false,
"secondary" : true,
"primary" : "mongodb0:27017",
"passive" : true,
"me" : "10.150.151.155:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2016-11-23T15:21:54.971Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}

1 个答案:

答案 0 :(得分:1)

我假设:

  1. 您的驱动程序可以通过10.150.151.141:27017或10.150.151.155:27017或两者连接到您的辅助公共IP。
  2. 您的连接字符串指向上述公共IP之一,或者您的连接字符串指向主要公共IP。
  3. 副本集在其配置中使用内部IP,可供客户端计算机访问:无法连接到“mongodb0”,“mongodb1”或“mongodb2” ”
  4. 您需要使用公共IP重新配置副本集。如果你很好奇为什么会这样,那就在Server Discovery and Monitoring Spec中解释了。但要点是:rs.conf()中成员的主机名列出的内容必须是客户端计算机可访问的公共主机名。