MongoDB无法连接,[MongooseServerSelectionError:connect ECONNREFUSED::1:27017]

dm7nw8vv  于 2023-06-29  发布在  Go
关注(0)|答案(1)|浏览(165)

注意:我已经浏览了以下类似的线程,并尝试了以下选项:

1.* 重新启动MongoDB服务。*
1.* 将localhost替换为0.0.0.0。*
1.* 将本地主机替换为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

以下是index.js文件的代码:

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

**Nodjs版本:v18.13.0
**MongoDB版本:**6.0.5
**Mongoose版本:**7.2.2
我得到的错误是:

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
}

我是一个初学者,寻求您的帮助。

rslzwgfq

rslzwgfq1#

我也有类似的连接问题与节点18.16.0。我把它降级到16.13.0,它工作得很好。

编辑:我让它在18.16.0节点上工作。存在起始节点17使用IPv6而不是IPv4作为默认的突破性改变。我不得不为此修改我的mongod.conf。说明非常简单(https://www.mongodb.com/docs/manual/reference/configuration-options/#net-options)。我用的是node driver,不是mongoose。还有一些与DB版本兼容的问题,我也必须解决。

相关问题