英文:
MongoDB doesn't connect. [MongooseServerSelectionError: connect ECONNREFUSED ::1:27017]
问题
以下是翻译好的部分:
以下是来自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
<details>
<summary>英文:</summary>
***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*
> https://stackoverflow.com/questions/70045495/timeouterror-mongoserverselectionerror-connect-econnrefused-127017-node
> https://stackoverflow.com/questions/50173080/mongonetworkerror-failed-to-connect-to-server-localhost27017-on-first-connec
> https://stackoverflow.com/questions/69840504/mongooseserverselectionerror-connect-econnrefused-127017
> https://stackoverflow.com/questions/76304154/mongooseserverselectionerror-connect-econnrefused-127017-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.
</details>
# 答案1
**得分**: 1
我还遇到了与 node 18.16.0 相似的连接问题。我将它降级到了 16.13.0,然后它正常工作了。
**编辑**:
我已经让它在 node 18.16.0 上工作了。从 node 17 开始有一个重大变化,它默认使用 IPv6 而不是 IPv4。我不得不修改我的 mongod.conf 文件以适应这一变化。这些指令相当简单(https://www.mongodb.com/docs/manual/reference/configuration-options/#net-options)。我使用的是 node 驱动程序,而不是 mongoose。与数据库版本的兼容性也有一些问题,我也不得不解决这些问题。
<details>
<summary>英文:</summary>
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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论