MongoDB connection refused

“I am in a big trouble. Can someone please help me to solve this? When I try to connect MongoDB to my local server, it’s not connecting but the code is still running. So, I decided to change the IP address from “127.0.0.1:27017” to “localhost”. However, this time it shows an error “ECONNREFUSED”. What should I do? I have watched more than 30 videos on YouTube and tried everything, but still have not found a solution. I have also checked after turning off the firewall and tried another network connection. Please help.”

How did you start the mongod server (e.g. what options)?

How did you connect to 127.xxx ? what error did you see?


sorry for the late reply
This is my source code. My terminal is stuck like this

Did you verify that mongodb server is indeed listening on 27017? you can use something like netstat to check that.

And if yes, does mongodb log show anything?

Screenshot 2023-04-28 221208

It says ‘Established’, but my terminal is not showing any further output or response, or it appears to be still running.

I would download the Mongo Shell (mongosh) and try to run the following command to eliminate the nodejs code. This way we can work around some possible issues. (https://www.mongodb.com/docs/mongodb-shell/install/)

mongosh mongodb://127.0.0.1:27017

Another test could be to use MongoDB Compass and connect to 127.0.0.1.

Please post any errors you get when doing these tests

You cold also try following this blog post from MongoDB on connecting with NodeJS and seeing if there is an issue with your code.

Now, I am able to connect with the MongoDB cluster, but I am still unable to connect with my local server.

Thankyou for recommending this blog

1 Like

did your app/code/server work with "127.0.0.1"? if it does, then you may need to change your settings for IPv6 as the "localhost" would be "::1", or check your system for this. there are other discussions about this IPv6 effect on the Forums. check them if you need.

if this is not the case, then you need to supply more info about your configuration.


I solved my problem with this code .Thankyou all for your support :heart:

2 Likes

To make better use of async/await, you can say this "new MongoClient(uri)" is the modern way to use the driver. Keep that in mind when you read old tutorials with lots of “callback” functions.

anyways, with this working code, will you please also use "localhost" instead of "127.0.0.1" to check for IPv6, because part of your resolution is not clear about this statement:

Hello @Sayandh_Ms

Look at this example: if you have any other questions I am available.