简体   繁体   中英

pymongo replicaset NetworkTimeout error

I'm using the following connection string with my MongoClient on pymongo 3.4.0:

client = MongoClient("mongodb://username:password@10.0.5.170:27017,10.0.5.222:27017,10.0.6.16:27017/?replicaSet=enterprise", connect=False)

when I take down my primary and I try to do any CRUD operation, I get a pymongo.errors.NetworkTimeout error. However, this error goes away when I create a new MongoClient with the same uri and try again. How can I ensure seamless failover?

This is the output of db.isMaster() run on the primary

{
    "hosts" : [
        "10.0.5.170:27017",
        "10.0.5.222:27017",
        "10.0.6.16:27017"
    ],
    "setName" : "enterprise",
    "setVersion" : 1,
    "ismaster" : true,
    "secondary" : false,
    "primary" : "10.0.5.170:27017",
    "me" : "10.0.5.170:27017",
    "electionId" : ObjectId("7fffffff0000000000000001"),
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 1000,
    "localTime" : ISODate("2017-02-14T17:38:38.647Z"),
    "maxWireVersion" : 4,
    "minWireVersion" : 0,
    "ok" : 1
}

Once PyMongo connects to all your replica set members, it tries to continue using the same connections for as long as possible. So, once it's connection to the primary, it'll keep using that primary connection for subsequent operations.

If you shut down the primary, PyMongo's next operation will usually reuse the old primary connection and get a network error. (Whether you get an error or what error you see depends on timing and network configuration.) PyMongo records the fact that it must rediscover the primary, then it raises the network error.

The next operation after that, however, PyMongo rediscovers the new primary and uses its new primary connection for the operation.

For more details see my article or my MongoDB World talk, the first two links here:

https://emptysqua.re/blog/server-discovery-and-monitoring-in-mongodb-drivers/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM