Search code examples
pythonpymongopymongo-3.x

ServerSelectionTimeoutError Pymongo


I'm trying out pymongo for the first time and I keep getting a ServerSelectionTimeoutError. When using mongo commandline to login I run a command as follows

$ mongo-3.0 --ssl test.net:27080/db_qa --sslAllowInvalidCertificates -u content -p
MongoDB shell version: 3.0.12
Enter password:

and I'm able to connect fine but with pymongo I get the error

pymongo.errors.ServerSelectionTimeoutError: test.net:27080: [Errno 60] Operation timed out

My code is as follows

from pymongo import MongoClient

client = MongoClient('mongodb://content:<password>@test.net:27080/db_qa')
client.server_info()

Solution

  • Your connection string is missing the options that your shell command line provides, namely ssl and option to allow invalid certificate.

    You could add ?ssl=true&ssl_cert_reqs=CERT_NONE after the database name in the string you are passing to MongoClient or see other options for certificate handling on MongoClient page (scroll to "SSL configuration" section)