Mongosh works but pymongo fails with same URI

We have a simple setup where mongo is running in a very basic configuration on k8s.

From another pod in the same namespace I can login to mongodb with mongosh and the mongo logs look like this:

{"t":{"$date":"2022-08-04T13:11:13.414+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn417","msg":"client metadata","attr":{"remote":"10.0.3.218:55322","client":"conn417","doc":{"driver":{"name":"nodejs|mongosh","version":"4.8.1"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"5.10.118"},"platform":"Node.js v16.16.0, LE (unified)","version":"4.8.1|1.5.4","application":{"name":"mongosh 1.5.4"}}}}
{"t":{"$date":"2022-08-04T13:11:13.420+00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn417","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"10.0.3.218:55322","extraInfo":{}}}

When using the exact same URI in PyMongo, the connection succeeds, but never authenticates, so it ends up timing out after trying to find mongo at 127.0.0.1.

{"t":{"$date":"2022-08-04T13:14:18.711+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"10.0.3.218:40240","uuid":"fbc15467-2262-4f82-b563-dab6961e65ed","connectionId":421,"connectionCount":4}}
{"t":{"$date":"2022-08-04T13:14:18.712+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn421","msg":"client metadata","attr":{"remote":"10.0.3.218:40240","client":"conn421","doc":{"driver":{"name":"PyMongo","version":"4.2.0"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.10.118"},"platform":"CPython 3.10.4.final.0"}}}
{"t":{"$date":"2022-08-04T13:14:18.713+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn421","msg":"Connection ended","attr":{"remote":"10.0.3.218:40240","uuid":"fbc15467-2262-4f82-b563-dab6961e65ed","connectionId":421,"connectionCount":3}}

Mongosh commands:

mongosh "mongodb://USER:PW@HOST:27017/DB?authSource=admin"
automate> db.mycollection.find()
# Works!!!

PyMongo

In [1]: import pymongo
   ...: from pymongo import MongoClient
   ...: client = MongoClient('mongodb://USER:PW@HOST:27017/DB?authSource=admin')

In [2]: db = client.DB

In [3]: collection = db.mycollection

In [4]: collection.find_one()

# TIMES OUT WITH...
ServerSelectionTimeoutError: Could not reach any servers in [('127.0.0.1', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 30s, Topology Description: <TopologyDescription id: 62ebd9efefa227b026e38b11, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('127.0.0.1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 111] Connection refused')>]>

I was able to complete this connection using the directConnection keyword argument. As far as I understand it, it’s because this is a non-replicated MongoDB pod?

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.