I’m connecting for the first time with Python on Ubuntu 22.04 to the Atlas mongodb. However, I am getting the following error regarding DNS.
I saw some similar threads, but I couldn’t solve my problem. Could you help me please?
from pymongo import MongoClient
import dns.resolver
def get_database():
#Provide the mongodb atlas url to connect python to mongodb using pymongo
CONNECTION_STRING = “mongodb+srv://projegui:@clusterak.mongodb.net/myFirstDatabase”
#Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
client = MongoClient(CONNECTION_STRING)
#Create the database for our example (we will use the same database throughout the tutorial
return client[‘myFirstDatabase’]
#This is added so that many files can reuse the function get_database()
if name == “main”:
#Get the database
dbname = get_database()
ERROR:
/usr/bin/python3.10 /home/rodrigo/opt/gui/pymongo_get_database.py
Traceback (most recent call last):
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 89, in _resolve_uri
results = _resolve(
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 43, in _resolve
return resolver.resolve(*args, **kwargs)
File “/usr/local/lib/python3.10/dist-packages/dns/resolver.py”, line 1368, in resolve
return get_default_resolver().resolve(
File “/usr/local/lib/python3.10/dist-packages/dns/resolver.py”, line 1190, in resolve
(request, answer) = resolution.next_request()
File “/usr/local/lib/python3.10/dist-packages/dns/resolver.py”, line 691, in next_request
raise NXDOMAIN(qnames=self.qnames_to_try, responses=self.nxdomain_responses)
dns.resolver.NXDOMAIN: The DNS query name does not exist: _mongodb._tcp.clusterak.mongodb.net.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/rodrigo/opt/gui/pymongo_get_database.py”, line 19, in
dbname = get_database()
File “/home/rodrigo/opt/gui/pymongo_get_database.py”, line 10, in get_database
client = MongoClient(CONNECTION_STRING)
File “/usr/local/lib/python3.10/dist-packages/pymongo/mongo_client.py”, line 736, in init
res = uri_parser.parse_uri(
File “/usr/local/lib/python3.10/dist-packages/pymongo/uri_parser.py”, line 542, in parse_uri
nodes = dns_resolver.get_hosts()
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 121, in get_hosts
_, nodes = self._get_srv_response_and_hosts(True)
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 101, in _get_srv_response_and_hosts
results = self._resolve_uri(encapsulate_errors)
File “/usr/local/lib/python3.10/dist-packages/pymongo/srv_resolver.py”, line 97, in _resolve_uri
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: The DNS query name does not exist: _mongodb._tcp.clusterak.mongodb.net.
The result of commands
nslookup -type=SRV _mongodb._tcp.cluster0.abcd0.mongodb.net
host -t SRV _mongodb._tcp.cluster0.abcd0.mongodb.net
rodrigo@-T440p:~/opt/gui$ nslookup -type=SRV _mongodb._tcp.clusterak.mp9ylv1.mongodb.net
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
_mongodb._tcp.clusterak.mp9ylv1.mongodb.net service = 0 0 27017 ac-w3xikpp-shard-00-00.mp9ylv1.mongodb.net.
_mongodb._tcp.clusterak.mp9ylv1.mongodb.net service = 0 0 27017 ac-w3xikpp-shard-00-01.mp9ylv1.mongodb.net.
_mongodb._tcp.clusterak.mp9ylv1.mongodb.net service = 0 0 27017 ac-w3xikpp-shard-00-02.mp9ylv1.mongodb.net.
Authoritative answers can be found from:
rodrigo@-T440p:~/opt/gui$ host -t SRV _mongodb._tcp.clusterak.mp9ylv1.mongodb.net
_mongodb._tcp.clusterak.mp9ylv1.mongodb.net has SRV record 0 0 27017 ac-w3xikpp-shard-00-02.mp9ylv1.mongodb.net.
_mongodb._tcp.clusterak.mp9ylv1.mongodb.net has SRV record 0 0 27017 ac-w3xikpp-shard-00-01.mp9ylv1.mongodb.net.
_mongodb._tcp.clusterak.mp9ylv1.mongodb.net has SRV record 0 0 27017 ac-w3xikpp-shard-00-00.mp9ylv1.mongodb.net