4

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()
user3626708
  • 727
  • 2
  • 8
  • 24

3 Answers3

11

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)

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
3

So what worked for me was my refreshing my current IP which changed under the "setup connection security" tab

0

I report my experience in which based on: https://www.mongodb.com/docs/atlas/troubleshoot-connection/#connection-string-issues

username = quote_plus('<username>')
password = quote_plus('<password>')
cluster = '<clusterName>'
authSource = '<authSource>'
authMechanism = '<authMechanism>'
uri = 'mongodb+srv://' + username + ':' + password + '@' + cluster + '/?authSource=' + authSource + '&authMechanism=' + authMechanism
client = pymongo.MongoClient(uri)
client.server_info()

Basically adding +srv to the connection string seems to be using SSL implicitly.

Vzzarr
  • 4,600
  • 2
  • 43
  • 80