skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

    1. 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.

    2. 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.

    3. Firewall or Network Restrictions: Ensure that there are no firewalls or network configurations blocking connections on port 27017.

    4. 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:

    • MongoDB is it running?
    • Can you use a tool like the Mongo shell to
      connect to it ?
    • Are there fences or network problems that keep people
      from connecting?
    Login or Signup to reply.
  3. Your MongoDB connection string should have to improve.
    Just use one of these.

    mongoose.connect("mongodb://localhost:27017/my-mern-db")
    

    or

    mongoose.connect("mongodb://127.0.0.1:27017/my-mern-db")
    

    sometimes, the MongoDB server refused to build a connection using "localhost".

    before connecting to the MongoDB server make sure that the MongoDB
    server is running by task manager if your os is Windows, if it’s not
    running start it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search