Why is PyMongo 3 giving ServerSelectionTimeoutError?

后端 未结 20 956
我在风中等你
我在风中等你 2020-11-30 01:21

I\'m using:

  • Python 3.4.2
  • PyMongo 3.0.2
  • mongolab running mongod 2.6.9
  • uWSGI 2.0.10
  • CherryPy 3.7.0
  • nginx 1.6.2
相关标签:
20条回答
  • 2020-11-30 02:05

    In my case

    • I was using Mongo Atlas
    • I got another IP adress after a router reboot

    hence I had to add that IP to the whitelist on Mongo Atlas settings via

    MongoAtlas website -> Network Access -> IP Whitelist -> Add IP Address -> Add Current IP Address
    

    then wait for IP Address's status to change to Active and then try to run the app again

    0 讨论(0)
  • 2020-11-30 02:06

    I had the same problem with Pymongo 3.5 Turns out replacing localhost with 127.0.0.1 or corresponding ip address of your mongodb instance solves the problem.

    0 讨论(0)
  • 2020-11-30 02:06

    I'm using pymongo 3.2 and I run into the same error, however it was a missconfiguration in my case. After enabling authorization, I forgot to update the port in the url which ended up in a connection timout. Probably it is worth to mention that ?authSource might be required as it is typically different than the database storing the application data.

    0 讨论(0)
  • 2020-11-30 02:10

    That error has occurred because there is no MongoDB server running in the background. To run the MongoDB server open cmd or anaconda prompt and type this:-

    "C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"
    

    then run

    import pymongo
    myclient = pymongo.MongoClient()    
    mydb = myclient["mydatabase"]
    myclient.list_database_names()
    
    0 讨论(0)
  • 2020-11-30 02:16

    I solved this by installing dnspython (pip install dnspython). The issue is that: "The "dnspython" module must be installed to use mongodb+srv:// URIs"

    0 讨论(0)
  • 2020-11-30 02:18

    pymongo 3 will not tell you your connection failed when you instantiate your client. You may not be connected.

    https://api.mongodb.com/python/3.5.1/api/pymongo/mongo_client.html

    "it no longer raises ConnectionFailure if they are unavailable .. You can check if the server is available like this:"

    from pymongo.errors import ConnectionFailure
    client = MongoClient()
    try:
        # The ismaster command is cheap and does not 
    require auth.
        client.admin.command('ismaster')
    except ConnectionFailure:
        print("Server not available")
    
    0 讨论(0)
提交回复
热议问题