Node.js cannot resolve the mongodb+srv address of an Atlas cluster
Error: querySrv EREFUSED _mongodb._tcp.cluster0.example.mongodb.netShort 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.
Fix it fast
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.net2Match what you see, then jump to its fix
- 1
dig 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
The 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
DNS changes are not allowed on this network or serverSRV lookup cannot be fixed, so use the standard connection stringFix - 4
The SRV record is not found on any DNS server, including a public oneThe cluster is paused or deletedFix
- 1
What does the error look like?
Error: querySrv EREFUSED _mongodb._tcp.cluster0.example.mongodb.netError: 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.net2Fix
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.netConfirmed by mongodb.com
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 270172Fix
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@latest3Verify
nc reports the connection succeeded.
nc -zv shard-00-00.example.mongodb.net 27017Confirmed by mongodb.com
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-142Fix
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
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.net2Fix
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 > Resume3Verify
dig returns an SRV answer within a few minutes of resuming.
dig SRV _mongodb._tcp.cluster0.example.mongodb.netConfirmed by mongodb.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix