MongooseServerSelectionError:连接 ECONNREFUSED ::1:27017 错误

Gop*_*wah 3 mongoose mongodb express mongo-express

import express from 'express'
const app = express();
const port = process.env.PORT || 8000;

import mongoose from 'mongoose';

mongoose.connect("mongodb://localhost:27017/schooldb").then(() => {
    console.log("connection successfully ");
})
app.get('/', (req, res) => {
    res.send('Hello user')
})

app.listen(port, () => {
    console.log(`listening at port http://localhost:${port}`);
})
Run Code Online (Sandbox Code Playgroud)

嗨,我是学习express的新手,我正在尝试将我的mongodb连接到express js,我无法理解此错误 D:\Learn\Learn Express\ new_mongoose\node_modules\mongoose\lib\connection.js:792 err =新的服务器选择错误();^

MongooseServerSelectionError:在 NativeConnection.openUri (D:\Learn\Learn Express\new_mongoose) 的 _handleConnectionErrors (D:\Learn\Learn Express\new_mongoose\node_modules\mongoose\lib\connection.js:792:11) 连接 ECONNREFUSED ::1:27017 \node_modules\mongoose\lib\connection.js:767:11) 在 runNextTicks (node:internal/process/task_queues:60:5) 在 listOnTimeout (node:internal/timers:533:9) 在 process.processTimers (节点:内部/计时器:507:7) { 原因: TopologyDescription { 类型: '未知', 服务器: Map(1) { 'localhost:27017' => ServerDescription { 地址: 'localhost:27017', 类型: '未知', 主机:[],被动:[],仲裁者:[],标签:{},minWireVersion:0,maxWireVersion:0,roundTripTime:-1,lastUpdateTime:656213806,lastWriteDate:0,错误:MongoNetworkError:连接ECONNREFUSED :: 1: 27017 在连接失败错误 (D:\Learn\Learn Express\new_mongoose\node_modules\mongodb\lib\cmap\connect.js:370:20) 在套接字。(D:\Learn\Learn Express\new_mongoose\node_modules\mongodb\lib\cmap\connect.js:293:22) 在 Object.onceWrapper (节点:事件:628:26) 在 Socket.emit (节点:事件:513) :28)在emitErrorNT(节点:内部/流/销毁:151:8)在emitErrorCloseNT(节点:内部/流/销毁:116:3)在process.processTicksAndRejections(节点:内部/进程/task_queues:82:21) {
原因:错误:在 TCPConnectWrap.afterConnect [as oncomplete] 连接 ECONNREFUSED ::1:27017 (node:net:1487:16) { errno: -4078,代码:'ECONNREFUSED',系统调用:'connect',地址:' ::1',端口:27017 },[Symbol(errorLabels)]:Set(1) { 'ResetPool' } },topologyVersion:null,setName:null,setVersion:null,electionId:null,logicSessionTimeoutMinutes:null,主要: null,我:null,'$clusterTime':null } },陈旧:false,兼容:true,heartbeatFrequencyMS:10000,localThresholdMS:15,setName:null,maxElectionId:null,maxSetVersion:null,commonWireVersion:0,逻辑SessionTimeoutMinutes:null },代码:未定义}

我期待控制台上出现连接成功消息,请帮助我

Pra*_*amJ 9

在node.js v18中,localhost使用ipv6地址(::1),并且默认情况下mongodb localhost没有启用ipv6。这就是您面临这个问题的原因。

如果您想使用 ipv4 本地主机地址 ( 127.0.0.1),

  1. 要么替换localhost127.0.0.1.
mongoose.connect("mongodb://127.0.0.1:27017/schooldb").then(() => {
    console.log("connection successfully ");
})
Run Code Online (Sandbox Code Playgroud)
  1. 或者使用family: 4参数。这告诉 Node.js 本地主机使用 ipv4 地址。
mongoose.connect("mongodb://localhost:27017/schooldb",{
    family: 4,
}).then(() => {
    console.log("connection successfully ");
})
Run Code Online (Sandbox Code Playgroud)

或者

如果您想使用 ipv6 地址,则只需mongod--ipv6参数开头即可。这将启用 mongodb ipv6 地址。

mongod --ipv6
Run Code Online (Sandbox Code Playgroud)