ServerSelectionTimeoutError Pymongo
Asked Answered
K

3

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()
Kahle answered 6/1, 2019 at 18:43 Comment(0)
T
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)

Treillage answered 22/1, 2019 at 18:46 Comment(0)
P
3

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

Paripinnate answered 23/7, 2021 at 16:55 Comment(0)
B
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.

Brae answered 24/4, 2023 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.