Resolving MongooseServerSelectionError: connect ECONNREFUSED::1.27017 at Connection.openUri
~ Written By Akash Srinivasan
Table of contents
Resolving MongooseServerSelectionError: connect ECONNREFUSED::1.27017 at Connection.openUri
As a Node.js developer who uses Mongoose to work with MongoDB databases, you know that errors can happen anytime and anywhere in your code. Some errors are easy to fix, while others can be tricky and frustrating. Fortunately, Node.js and Mongoose provide some tools and techniques to help you handle errors effectively and gracefully.
In this blog post, we'll explore MongooseServerSelectionError: connect ECONNREFUSED::1.27017 at Connection.openUri Error, and how to detect, report, and recover from them. We'll also sprinkle some memes and jokes to lighten up the mood and make learning more fun.
Reason Of This Error
When we open the DataBaseConnection.js
file in our project might be:-
Node.js, Express.js, Mongoose and MongoDB
Next.js, Mongoose and MongoDB etc.....
We can see this code (I am using MongoDB Compass)
const mongoose = require("mongoose")
const connectToDataBase = () => {
mongoose.connect("mongodb://localhost:27017")
.then(() => {
console.log("Connected To DB Sucessfully....")
})
.catch((err) => {
console.log(err)
})
}
module.exports = connectToDataBase
After I had researched a bit on google I found that after the recent node.js update which is 18.1.0v. This is the reason why we are facing this issue let's have a look at our current node.js version.
So as you can see my node.js version is above 18.0.1
How To Resolve This Error (Solution - 1)
Though the error is too big and tedious to read the solution is pretty simple. There is only one step which is:-
Update the mongoDB compass connection URL from
mongodb://localhost:27017
tomongodb://127.0.0.1:27017
ormongodb://0.0.0.0:27017
Let's have a look at the code now:-
const mongoose = require("mongoose")
const connectToDataBase = () => {
mongoose.connect("mongodb://127.0.0.1:27017")
.then(() => {
console.log("Connected To DB Sucessfully....")
})
.catch((err) => {
console.log(err)
})
}
module.exports = connectToDataBase
In case you are using .env
file you can just update the mongoDB url there also.
How To Resolve This Error for MongoDB compass (Solution - 2)
If the above solution doesn't work to you then the problem isn't with your code but you are not connecting your mongoDB compass. To do that open MongoDB Compass and click on the connect button.
After clicking it later run your code.
How To Resolve This Error for MongoDB Atlas (Solution - 3)
If the above two method's didn't work for you this means that you are using MongoDB Atlas and there is some issue in the IP config to resolve it open your MongoDB Atlas Cluster by hitting on to this URL:-https://account.mongodb.com/account/login
After signing in you can see this kind of UI if you have created a project
-
Click On Network Access
Click On Edit
Remove that IP adress
-
And Add this
Later Click On Confirm
And There you go now try running your code again and it's resolved.