Can’t connect to my MongoDB Atlas database with VS Code extension

I am currently struggling to connect to my database through Vscode MongoDB extension.

I used the connection string provided by the ‘Connect’ instructions:
mongodb+srv://test:hello@cluster0.cjsefts.mongodb.net/

But the error I automatically get is the following:
Unable to connect: querySrv EREFUSED _mongodb._tcp.cluster0.cjsefts.mongodb.net

What I have tried

I have check numerous times that my password is the correct, I even tried to change my password but didn’t matter (My passwords didn’t contain any special characters).
I tried to delete the dataabse and create a new one, tried to connect, but got the same response.
Unistall and reinstall Vscode extension.
On my terminal I ran mongostat --uri mongodb+srv://test:hello@cluster0.cjsefts.mongodb.net on my terminal and it succesfully ran, which means the user and password are not the issue.
I used the extension to connect to mongodb://localhost (while previously started mongod server with brew services start mongodb-community@7.0) and it succesfully connected, which means the Vscode extension is not the problem.
I also checked that my IP address was added in the Network Access section I made this with the button ‘Add my IP adress’ when creating the database, I even added the 0.0.0.0/0 to expose the database everywhere in the internet.
I just have one single user named ‘test’ with admin access.
*Restart VScode several times.
I am really stucked and I don’t have any clue what the issue might be. Thanks for your help.

1 Like

Please check that your system antivirus and firewall can block the connection to MongoDB. Make sure that the port you are using should not be blocked.

1 Like

Just some things I’d try.

The connection seems fine from here.

From what I see in SO and the result of the connection, it could be that your system is querying a DNS that does not understand SRV strings, which have to be mapped to proper connection strings (addresses for each node in the replica set.)

I’m unsure whether the string you receive is the result of the DNS query though.
It’s strange that mongostat works.

But I’d look at these first:

  1. DNS Resolution Issue: check the DNS that you are using (the IP.)

  2. Firewall or Network Restrictions: There could be firewall rules or network restrictions preventing your system from querying the DNS SRV record or connecting to the MongoDB cluster. Make sure that your network allows connections (it may be blocking / not allowing ports like 27017, for example.)

did you got the solution? I have similar issue . i check everything firewalls , DNS , everything but still its not working .
this is the error i got .
Error connecting to MongoDB: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist: https://mongodb.prakticum-team.ru/docs/atlas/security-whitelist/
at _handleConnectionErrors (D:\resoluteandrowe\backend\node_modules\mongoose\lib\connection.js:909:11)
at NativeConnection.openUri (D:\resoluteandrowe\backend\node_modules\mongoose\lib\connection.js:860:11)
at runNextTicks (node:internal/process/task_queues:60:5)
at listOnTimeout (node:internal/timers:538:9)
at process.processTimers (node:internal/timers:512:7)
at async connectDb (file:///D:/resoluteandrowe/backend/Config/db.js:7:9) {
reason: TopologyDescription {
type: ‘Unknown’,
servers: Map(1) {
cluster0.hejlr.mongodb.net:27017’ => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}

I posted a suggestion in here not long ago: Error: querySrv ECONNREFUSED MongoDB - #4 by Billy_Bui

Please try it to see if it can help you or not.