Hasnain
Hasnain

Reputation: 679

MongoDB doesn't connect. [MongooseServerSelectionError: connect ECONNREFUSED ::1:27017]

Note: I have already gone through the following similar threads and tried the following options:

  1. Restarted the MongoDB service.
  2. Replaced localhost by 0.0.0.0.
  3. Replaced local host by 127.0.0.1:27017

timeoutError - MongoServerSelectionError: connect ECONNREFUSED ::1:27017 - [NodeJS + MongoDB]

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 error

Following is the code from index.js file:

const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/db_c");

Nodjs version: v18.13.0

MongoDB version: 6.0.5

Mongoose version: 7.2.2

The error I am getting is:

E:\nodejs\app1\node_modules\mongoose\lib\connection.js:792
    err = new ServerSelectionError();
          ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at _handleConnectionErrors (E:\nodejs\app1\node_modules\mongoose\lib\connection.js:792:11)   
    at NativeConnection.openUri (E:\nodejs\app1\node_modules\mongoose\lib\connection.js:767:11) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 1981963039,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (E:\nodejs\app1\node_modules\mongodb\lib\cmap\connect.js:370:20)            at Socket.<anonymous> (E:\nodejs\app1\node_modules\mongodb\lib\cmap\connect.js:293:22)    
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
PS E:\nodejs\app1> node index.js
E:\nodejs\app1\node_modules\mongoose\lib\connection.js:792
    err = new ServerSelectionError();
          ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at _handleConnectionErrors (E:\nodejs\app1\node_modules\mongoose\lib\connection.js:792:11)   
    at NativeConnection.openUri (E:\nodejs\app1\node_modules\mongoose\lib\connection.js:767:11) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 101790,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (E:\nodejs\app1\node_modules\mongodb\lib\cmap\connect.js:370:20)       
            at Socket.<anonymous> (E:\nodejs\app1\node_modules\mongodb\lib\cmap\connect.js:293:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

PS E:\nodejs\app1> node index.js      
E:\nodejs\app1\node_modules\mongoose\lib\connection.js:792
    err = new ServerSelectionError();
          ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at _handleConnectionErrors (E:\nodejs\app1\node_modules\mongoose\lib\connection.js:792:11)   
    at NativeConnection.openUri (E:\nodejs\app1\node_modules\mongoose\lib\connection.js:767:11) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 945032,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (E:\nodejs\app1\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:370:20)
            at Socket.<anonymous> (E:\nodejs\app1\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:293:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

I am an absolute beginner and seek your help.

Upvotes: 0

Views: 719

Answers (1)

asm
asm

Reputation: 11

I also had the similar connection problem with node 18.16.0. I downgraded it to 16.13.0, and it worked fine.

Edit: I got it to work with node 18.16.0. There is a breaking change starting node 17 that it uses IPv6 instead of IPv4 as default. I had to modify my mongod.conf for that. The instructions are quite simple (https://www.mongodb.com/docs/manual/reference/configuration-options/#net-options). I am using node driver not mongoose. There are issues with compatibility with version of DB that I also had to work out.

Upvotes: 1

Related Questions