gpt4 book ai didi

node.js - MongoServerSelectionError:连接 ECONNREFUSED::1:27017

转载 作者:行者123 更新时间:2023-12-03 07:59:34 29 4
gpt4 key购买 nike

当我尝试使用 Mongoose 连接我的应用程序和数据库时遇到问题。已经尝试过我在谷歌上找到的以下解决方案:

在 Windows 上重新启动 MongoDB 服务使用位于 MongoDB 的 bin 文件中的 cmd 手动打开 DB但我解决不了。谁能帮我吗?

这是我的index.js

const { MongoClient } = require('mongodb')
const url = 'mongodb://localhost:27017';
const database = 'e-comm'
const client = new MongoClient(url);

async function getData()
{
let result = await client.connect();
let db = result.db(database);
let collection = db.collection('products');
let response = await collection.find({}).toArray();
console.log(response);
}
getData();

并抛出错误

C:\Users\HP\new node\node_modules\mongodb\lib\sdam\topology.js:292
const timeoutError = new error_1.MongoServerSelectionError(`Server selection
timed out after ${serverSelectionTimeoutMS} ms`, this.description);
^

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users\HP\new node\node_modules\mongodb\lib\sdam\topology.js:29
2:38)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
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: 32573830,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\HP\new node\node_modules\mongodb\lib\cmap\co
nnect.js:387:20)
at Socket.<anonymous> (C:\Users\HP\new node\node_modules\mongodb\lib\cmap\connec
t.js:310: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:1300: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,
[Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.12.1

最佳答案

默认情况下,mongodb 仅监听 IP4 本地主机 127.0.0.1

错误消息表明您正在尝试连接到 IP6 本地主机 ::1

这可能是因为您使用的是主机名 localhost 并且您的本地操作系统正在解析为 IP6 地址::1

要解决此问题,您需要让 mongoose 连接到 mongod 正在监听的相同地址和端口,因此您需要:

a) 更改 mongod 配置,以便除了 127.0.0.1 之外,它还监听 ::1 或代替 127.0.0.1。请参阅net.ipv6

b) 更改 mongoose 配置以使用 IP 地址而不是主机名:const url = 'mongodb://127.0.0.1:27017';

关于node.js - MongoServerSelectionError:连接 ECONNREFUSED::1:27017,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74826965/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com