Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js cannot resolve the mongodb+srv address of an Atlas cluster

ErrorError: querySrv EREFUSED _mongodb._tcp.cluster0.example.mongodb.net

Short answer

A mongodb+srv connection string needs a DNS SRV lookup before any connection starts. querySrv EREFUSED means that lookup was refused. The usual causes are a DNS server that does not answer SRV queries, outbound DNS blocked on your network, or a paused cluster. Test the lookup with dig, then switch DNS or use the standard connection string.

  • Applies to MongoDB Atlas, MongoDB Node.js driver 6, Mongoose 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask DNS for the SRV record of your cluster, using the host part of your connection string. If this returns no answer, the problem is DNS, not MongoDB or your password.

    dig SRV _mongodb._tcp.cluster0.example.mongodb.net
  2. 2Match what you see, then jump to its fix

    1. 1dig SRV returns a refused or empty answer on your network, but works when you use a public resolverYour DNS server does not support SRV lookupsFix
    2. 2The SRV answer arrives, but the node hostnames in it do not resolve or do not accept a TCP connectionOutbound DNS is blocked, or node hostnames do not resolveFix
    3. 3DNS changes are not allowed on this network or serverSRV lookup cannot be fixed, so use the standard connection stringFix
    4. 4The SRV record is not found on any DNS server, including a public oneThe cluster is paused or deletedFix

What does the error look like?

  • Error: querySrv EREFUSED _mongodb._tcp.cluster0.example.mongodb.net
  • Error: querySrv ECONNREFUSED _mongodb._tcp.<SRV Record>
  • DNSHostNotFound: Failed to look up service

Cause 1 of 4

Your DNS server does not support SRV lookups

1Check

Some ISP and router DNS servers cannot answer SRV queries. Ask a public resolver the same question and compare.

dig SRV _mongodb._tcp.cluster0.example.mongodb.net
dig @8.8.8.8 SRV _mongodb._tcp.cluster0.example.mongodb.net

2Fix

Set a public DNS server such as Google Public DNS in your network settings, then run the app again.

# In your OS or router network settings, set the DNS servers to
# 8.8.8.8 and 8.8.4.4 (Google Public DNS), then reconnect.

3Verify

dig now returns one SRV answer for each node of the cluster.

dig SRV _mongodb._tcp.cluster0.example.mongodb.net

Confirmed by mongodb.com

Did this fix it?

Cause 2 of 4

Outbound DNS is blocked, or node hostnames do not resolve

1Check

Resolve one node hostname from the SRV answer, then test a TCP connection to it on its port. Ping may be blocked, so it proves nothing on its own.

dig shard-00-00.example.mongodb.net
nc -zv shard-00-00.example.mongodb.net 27017

2Fix

Ask the network owner to allow outbound DNS and TCP port 27017 from the machine that runs the app. If the tests pass but the error stays, update the MongoDB driver.

npm install mongodb@latest   # or: npm install mongoose@latest

3Verify

nc reports the connection succeeded.

nc -zv shard-00-00.example.mongodb.net 27017

Confirmed by mongodb.com

Did this fix it?

Cause 3 of 4

SRV lookup cannot be fixed, so use the standard connection string

1Check

A mongodb+srv string builds its host list from DNS. A standard mongodb:// string lists the hosts itself, so it needs no SRV lookup.

echo $MONGODB_URI | cut -c1-14

2Fix

In Atlas, open Connect, choose your driver and turn off the SRV Connection String toggle, then copy the mongodb:// string into your environment. Percent-encode special characters in the password.

MONGODB_URI="mongodb://<host1>:27017,<host2>:27017,<host3>:27017/?ssl=true&replicaSet=<rs>&authSource=admin"

3Verify

The app connects without a querySrv error.

node -e "require('mongodb').MongoClient.connect(process.env.MONGODB_URI).then(c=>{console.log('ok');return c.close()})"

Confirmed by mongodb.com

Did this fix it?

Cause 4 of 4

The cluster is paused or deleted

1Check

Atlas pauses idle Free clusters after 30 days with no connections. A paused or deleted cluster has no DNS host to find.

dig @8.8.8.8 SRV _mongodb._tcp.cluster0.example.mongodb.net

2Fix

Open the cluster in the Atlas UI. If it is paused, resume it. If it was deleted, create a new one and use its new connection string.

# Atlas UI: Database > your cluster > Resume

3Verify

dig returns an SRV answer within a few minutes of resuming.

dig SRV _mongodb._tcp.cluster0.example.mongodb.net

Confirmed by mongodb.com

Did this fix it?

How do I stop it coming back?

  • Keep the full connection string in an environment variable, so switching between SRV and standard format is a config change.
  • Test a new network (office, VPN, hosting provider) with dig SRV before you deploy to it.
  • Run a small scheduled query against free clusters so they are not paused for inactivity.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.