Please help connect to MongoDB Atlas

Hi, I am a 20+ years programmer. But I am new to MongoDB Atlas and Python. I am using Python 3.9 and PyMongo 4.0.1: I have trouble connect to a free MongoDB Atlas cluster that I have created based on MongoDB instruction. Please see the attached image, my connection string is:

mongodb+srv://behai:@cluster0.71o6u.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

I have replaced with the password for user “behai” in MongoDB Atlas. and I store it as MONGO_CONNECTION_STRING in my keys.py file.

I connect as:

import pymongo
import keys

client = pymongo.MongoClient( keys.MONGO_CONNECTION_STRING )

And I have the error:

ConfigurationError: All nameservers failed to answer the query cluster0.71o6u.mongodb.net. IN TXT: Server 192.168.0.1 UDP port 53 answered DNS message is malformed.

If I dropped “+srv” from the connection string, I could connect, but later accesses such as creating a database, creating a collection then insert into the collection would result in error:

ServerSelectionTimeoutError: xxx

When I try to ping cluster0 . 71o6u . mongodb . net:

I would get this response:

Ping request could not find host . Please check the name and try again.

It seems that host “cluster0.71o6u.mongodb.net” does not exist?

But when I look into my MongoDB Atlas windows, I can see I have made 6 connections during last 2 hours:

What am I doing wrong, please? Please help me.

Thank you and best regards,

Be Hai Nguyen.

First, a cluster is not a host. You cannot ping the cluster. The DNS lookup failed for some reason, but over here, the DNS entries for your cluster are correct. It means you have a VPN, old DNS, old python DNS library or some other issues with you internet provider.

Use Atlas UI to retrieve non-SRV connection string for your cluster and try with it.

1 Like

Thank you for help Steeve. I appreciate it.

  1. python DNS library – the only one I am aware of is dnspython which is version 2.1.0.

  2. Following your advice, I have got a non-SRV connection string as seen per image attached. I could get through connection, but when I tried insert into a yet-to-exist database and collection, I have a very error: “ServerSelectionTimeoutError… cluster0-shard-00-01 . 71o6u . mongodb. net : 27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:…”

  3. We could also eliminate VPN issue: I don’t use one.

  4. Internet provider – I could pin all three of these:

cluster0-shard-00-01 . 71o6u . mongodb . net
cluster0-shard-00-00 . 71o6u . mongodb . net
cluster0-shard-00-02 . 71o6u . mongodb . net

But I don’t know what that means.

I will keep working on this. If I have a solution, I will report back. I think there is something wrong with my environment.

Thank you and best regards,

Van Be Hai Nguyen.

Hi,

  1. I have managed to resolve my connection and also using the new connection string “mongodb+srv”:

Let me recap: I have pymongo version 4.0.1 and dnspython version 2.1.0 installed.

Following these two trouble-shooting posts:

https : // stackoverflow . com/questions/53576199/all-nameservers-failed-to-answer-udp-port-53-google-cloud-functions-python-3-7-a

https: // forum .omz-software . com/topic/6751/pymongo-errors-configurationerror-resolver-configuration-could-not-be-read-or-specified-no-nameservers/6

We need to do this first before the “import pymongo” statement, so that DNS can be resolved before looking up the cluster shard ( ? ).

import dns.resolver
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers=[‘8.8.8.8’] # this is a google public dns server, use whatever dns server you like here # as a test, dns.resolver.query(‘www.google.com’) should return an answer, not an exception

import pymongo

3 Likes
  1. The issue with “[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)” has also been resolved.

Please see these two trouble-shooting posts:

https: // www . mongodb . com/community/forums/t/keep-getting-serverselectiontimeouterror/126190

https: // stackoverflow . com/questions/69397039/pymongo-ssl-certificate-verify-failed-certificate-has-expired-on-mongo-atlas

What I did was following the suggested steps:

a. Download https: // letsencrypt .org/certs/lets-encrypt-r3.pem

b. rename file .pem to .cer

c. double click and install

d. Restart my Windows 10 PC machine.

I would consider this issue closed.

Thank you and best regards,

Van Be Hai Nguyen.

2 Likes

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