当连接到MongoDB副本集时,我能够从辅助节点读取,但之后,当使用相同的连接写入时,我收到以下错误:
pymongo.errors.ServerSelectionTimeoutError: No primary available for writesPython 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" : 1mongodb2
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 }When connection to a MongoDB replica set, I'm able to read from the secondaries, but afterwards, when writing using the same connection, I receive the following error:
pymongo.errors.ServerSelectionTimeoutError: No primary available for writesPython 3.4.3 Pymongo 3.2.2
My connection:
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')IMPORTANT: When removing both 'replicaSet' and 'readPrefence' fields, the connection works.
In the server:
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" : 1mongodb2
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 }最满意答案
我假设:
您的驱动程序可以通过10.150.151.141:27017或10.150.151.155:27017或两者连接到您的辅助公共IP。 您的连接字符串指向上述公共IP之一的辅助字符串,或者您的连接字符串指向主要公共IP。 副本集在其配置中使用内部IP,客户端计算机无法访问它:它无法连接到“mongodb0”,“mongodb1”或“mongodb2”。您需要使用公共IP重新配置副本集。 如果你很好奇为什么会这样,那么在服务器发现和监控规范中对它进行了解释。 但要点是: rs.conf()列出的成员主机名必须是客户端机器可访问的公共主机名。
I hypothesize that:
Your driver can connect to your secondary's public IP at either 10.150.151.141:27017 or 10.150.151.155:27017 or both. Your connection string points to one of the above public IPs for secondaries, or your connection string points to the public IP of the primary. The replica set uses internal IPs in its configuration which are not accessible to your client machine: it cannot connect to "mongodb0", "mongodb1", or "mongodb2".You need to reconfigure your replica set using public IPs. If you're curious why this is so, it's explained in the Server Discovery and Monitoring Spec. But the gist is this: whatever is listed for members' hostnames in rs.conf() must be public hostnames accessible to your client machine.
更多推荐
发布评论