I have written below code in server.js to connect with mongo db-
// server.js
const route =require('./routes/yourRoute');
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 5000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
// MongoDB Connection
mongoose.connect('mongodb://0.0.0.0/my-mern-db', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB connected'))
.catch((err) => console.error(err));
// Routes (Create these later)
app.use('/api/todo', route);
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
I am getting below errors –
Server is running on port 5000
MongooseServerSelectionError: connect ECONNREFUSED 0.0.0.0:27017
at _handleConnectionErrors (C:Userssagar.dhanorkarsourcereposmernappnode_modulesmongooselibconnection.js:805:11)
at NativeConnection.openUri (C:Userssagar.dhanorkarsourcereposmernappnode_modulesmongooselibconnection.js:780: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) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { '0.0.0.0:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
3
Answers
Ensure that your MongoDB server is running. You can start MongoDB by running the
mongod
command in your terminal. Make sure it’s running on the default port (27017) unless you have specified a custom port in your MongoDB configuration.Verify the MongoDB connection URI in your
mongoose.connect
method. The URI you provided, mongodb://0.0.0.0/my-mern-db, seems to be using the address ‘0.0.0.0’ as the MongoDB server address. You should replace ‘0.0.0.0’ with the actual address of your MongoDB server. If MongoDB is running on the same machine as your Node.js application, you can use ‘localhost’ or ‘127.0.0.1’ as the server address.Verify that MongoDB is running on the correct port. The default port is 27017. If you’ve configured MongoDB to use a different port, make sure to specify it in the connection URI.
The error you’re seeing, MongooseServerSelectionError: connect ECONNREFUSED 0.0.0.0:27017, means that your application was unable to make a connection to MongoDB at the address 0.0.0.0:27017.
Here are several possible reasons for this error and how to fix them:
MongoDB is not running: Ensure that MongoDB is running on the same machine. If you have MongoDB installed locally, you can start it with the mongod command.
Incorrect IP Address: The IP 0.0.0.0 usually means "all IPv4 addresses on the local machine". If you’re running your Node.js programme on a different machine than MongoDB, you’ll need to replace 0.0.0.0 with the IP address of the machine running MongoDB.
Firewall or Network Restrictions: Ensure that there are no firewalls or network configurations blocking connections on port 27017.
MongoDB Bind IP Configuration: By default, MongoDB binds to 127.0.0.1 (localhost). If you want to allow connections from outside localhost, you’ll need to change your MongoDB configuration to bind to a different IP or 0.0.0.0 (for all interfaces). This can be done by changing the mongod.conf or mongodb.conf file (depending on your setup) and setting the bindIp option under net. If you change this, remember to restart the MongoDB service.
net:
bindIp: 0.0.0.0
To figure out what’s wrong, start with the most basic checks:
connect to it ?
from connecting?
Your MongoDB connection string should have to improve.
Just use one of these.
or
sometimes, the MongoDB server refused to build a connection using "localhost".