Home > Enterprise >  MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 in node v17 and mongo is running
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 in node v17 and mongo is running

Time:11-17

Node app is throwing exception while starting

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (/Users/aadak/Workspace/node/ticket-status-tacker/node_modules/mongoose/lib/connection.js:846:32)
    at /Users/aadak/Workspace/node/ticket-status-tacker/node_modules/mongoose/lib/index.js:351:10
    at /Users/aadak/Workspace/node/ticket-status-tacker/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)

I could see mongo is running

aadak@aadak-a01 mongo-backup % brew services list                       
Name              Status  User  File
mongodb-community started aadak ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist

My mongo uri is defined as

"mongoURI": "mongodb://localhost:27017/ticketDB"

code to connect to mongo

const mongoose = require("mongoose");
const config = require("config");
const dbURI = config.get("mongoURI");
const logger = require('./winston');

const connectDB = async () => {
  try {
    await mongoose.connect(dbURI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      useFindAndModify: false,
    });
    logger.info("mongo db connected");
  } catch (err) {
    console.error(err);
    // exit process with failure.
    process.exit(1);
  }
};
module.exports = connectDB;
  • osx version: 11.6.1
  • Node version: 17.0.1
  • Mongo version: 5.0.3

CodePudding user response:

I am using node v17.0.1. Node v17 prefers IPv6 addresses over IPv4.But mongo by default is configured with ipv4.

IPv4 config

net:
  port: 27017
  bindIp: 127.0.0.1

Need to change it to :

  net:
      ipv6: true
      port: 27017
      bindIpAll: true


OSX path : /usr/local/etc/mongod.conf
Ubuntu Path: /etc/mongod.conf

Then restart mongo.

https://dba.stackexchange.com/questions/173781/bind-mongodb-to-ipv4-as-well-as-ipv6/302609#302609

  • Related