MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 But my mongod service is running

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 But my mongod service is running

每次我 运行 npm 在我的后端文件夹中启动时,我的服务器 运行 会在短时间内成功,然后很快就会崩溃。我从命令行返回此错误:

[nodemon] restarting due to changes...
[nodemon] starting `babel-node ./index.js`
The basketball server is running on port: 3000
C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\connection.js:807
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\connection.js:807:32)
    at C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\index.js:342:10
    at C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\index.js:1176:10)
    at Mongoose.connect (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\index.js:341:20)
    at Object.<anonymous> (C:/users/work/documents/codeprojects/learnmern/basketball/backend/index.js:10:10)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at loader (C:\Users\Work\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-register\lib\node.js:144:5) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 258048414,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongodb\src\cmap\connect.ts:514:14)
            at Socket.<anonymous> (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongodb\src\cmap\connect.ts:412:16)
            at Object.onceWrapper (node:events:640:26)
            at Socket.emit (node:events:520:28)
            at Socket.emit (node:domain:475:12)
            at emitErrorNT (node:internal/streams/destroy:164:8)
            at emitErrorCloseNT (node:internal/streams/destroy:129:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21)
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}

Node.js v17.4.0
[nodemon] app crashed - waiting for file changes before starting...

这是一张可能有助于解决问题的图片: screenshot of my index.js file and Mongo Compass

我是后端 Web 开发的新手,在尝试学习 MERN 上的 LINKEDIN 学习课程时遇到此错误。链接在这里:VIDEO 3 FROM THIS COURSE 非常感谢任何愿意花时间帮助我解决问题的人,谢谢!

connect ECONNREFUSED ::1:27017 看来,Mongoose 正在尝试连接到 ipv6,其中您的屏幕截图显示 mongod 正在使用 ipv4

使用 ipv4127.0.0.1