Not able to connect MongoDB through Node.js on Ubuntu

Hi there,

I am able to connect to Mongo DB through Compass and code on my local system but when I try to deploy the code to an Ubuntu server, it is giving me this error:

ERROR IN CONNECTING TO DATABASE Error: querySrv ENOTFOUND _mongodb._tcp.tha.ovfl8.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (internal/dns/promises.js:172:17) {
errno: undefined,
code: ‘ENOTFOUND’,
syscall: ‘querySrv’,
hostname: ‘_mongodb._tcp.tha.ovfl8.mongodb.net’
}

I am quite sure that this is a problem with Ubuntu because I’ve tried running it on Windows and Mac both and it was working fine.

I’ve whitelisted the IP and my Mongo DB is not dormant.

Can anybody please help? I’m quite new to Mongo DB atlas

Hi @Bhuwan_Devshali and welcome in the MongoDB Community :muscle: !

Before anything else, can you try to connect from this Ubuntu server to MongoDB Atlas using mongosh using the command line provided when you click “connect” in Atlas?

This will make sure that it’s not an issue related to network, VPN, firewalls, etc but currently my guess is that your Ubuntu server can’t connect to port 27017 for some internal security reasons.

Cheers,
Maxime.

1 Like

Head back to the network settings of your cluster and set it to “allow access anywhere” and try again. If it still fails to connect it is DNS/Firewall/VPN settings of your Ubuntu server as @MaBeuLux88_xxx mentioned. Else, you are setting wrong IP address in your whitelist.

1 Like

You URI is wrong. The cluster tha.ovfl8.mongodb.net does not exist.

;QUESTION
tha.ovfl8.mongodb.net. IN ANY
;ANSWER
;AUTHORITY
mongodb.net. 900 IN SOA ns-761.awsdns-31.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 60
;ADDITIONAL

Hi @steevej ,

The URL exists, I’ve changed the URL slightly to post in a public forum.

@Yilmaz_Durmaz I’ve allowed access from Anywhere. Still the same error

Hey @MaBeuLux88_xxx , I am able to connect to Mongo DB from my Ubuntu Server using Mongosh.

you can tag/answer in a single post :wink:

We see this error a lot and it is almost always related to the host environment. let me highlight them.

  • host has strict rules and must be set manually. try this and see if, at least, ports are open: curl http://portquiz.net:27017
  • the other thing is your host’s network is not set correctly. its DNS does not know how to resolve the address. (we lately had such a problem with replit.com containers). I am not a guru on Linux, so cannot point out where to look.

The port was already open.
If I am able to connect through MONGOSH, i should be able to connect via Node js code also. It is not a network issue, it is something else.
Could you please explain the meaning of this error message so that I can debug the issue on my own

ERROR IN CONNECTING TO DATABASE Error: querySrv ENOTFOUND _mongodb._tcp.thoughtsutra.ovfl8.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (internal/dns/promises.js:172:17) {
errno: undefined,
code: ‘ENOTFOUND’,
syscall: ‘querySrv’,
hostname: ‘_mongodb._tcp.thoughtsutra.ovfl8.mongodb.net’
}

It is hard to help if you hide critical details.

When the error is DNS ENOTFOUND, it is kind of critical to see what is the connection string.

The cluster thoughtsutra.ovfl8.mongodb.net does not exist so you get ENOTFOUND.

Yes you should, but I am pretty sure you cannot with thoughtsutra.ovfl8.mongodb.net in your URI.

If your URI really has thoughtsutra.ovfl8.mongodb.net as the cluster, it is a network issue.

another reason that comes to mind is that you are using an older driver version (which we keep forgetting to ask). What driver version are you trying to use?

there are two connection string versions: old mongodb:// and new mongodb+srv://. in the old format, you need to give the address of each cluster member. in the new one, only cluster address is given and it is resolved to member addresses with an SRV record resolver. old drivers do not understand this new string format and fail to resolve cluster addresses.

from the same location you get your connection string (connect with application) select the oldest nodejs driver version and use that mongodb:// connection string and report back so we would know if that is the issue.

Since the code is ENOTFOUND for syscall querySrv with a host name that starts with _mongodb._tcp the URI is of the form