Why is PyMongo 3 giving ServerSelectionTimeoutError?

后端 未结 20 957
我在风中等你
我在风中等你 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:18
    • First set up the MongoDB environment.

    • Run this on CMD - "C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"

    • Open another CMD and run this - "C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe"

    And then you can use pymongo [anaconda prompt]

    import pymongo
    from pymongo import MongoClient
    
    client = MongoClient()
    db = client.test_db
    collection = db['test_coll']
    

    Refer - https://docs.mongodb.com/tutorials/install-mongodb-on-windows/

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

    I am not sure if you are using the MongoDB paired with AWS Cloud service. But if you are, I found that you have to specify which IP Address you want MongoDB to have access to.

    So what you need to do is add the IP Address of your host server to allow entry.

    In MongoAtlas, this can be done at this page

    I know there was already a solution to the same issue, but I didn't find a solution that helped my situation, so wanted to post this, so others could benefit if they ever face the same problem that I do.

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

    I've come accross the same problem and finally I found that the client IP is blocked by the firewall of the mongo server.

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

    maybe you can try to add your server ip address into the mongod.conf file. if you use linux(ubuntu) os,you can try my solution:

    1. modify mongod.conf file:

      vi /etc/mongod.conf
      

      and you can add mongodb server ip address behind 127.0.0.1,and save:

      net:
        port:27017
        bindIp:127.0.0.1,mongodb server ip
      
    2. in the teminal:

      sudo service mongod restart

    Now,you can try to connect mongodb by using pymongo MongoClient.

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

    This has been fixed in PyMongo with this pull_request.

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

    I fixed it for myself by downgrading from pymongo 3.0 to 2.8. No idea what's going on.

       flask/bin/pip uninstall pymongo
       flask/bin/pip install pymongo==2.8
    
    0 讨论(0)
提交回复
热议问题