首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >timeoutError - MongoServerSelectionError: connect ::1:27017 - [NodeJS + MongoDB]

timeoutError - MongoServerSelectionError: connect ::1:27017 - [NodeJS + MongoDB]
EN

Stack Overflow用户
提问于 2021-11-20 11:31:10
回答 3查看 6.8K关注 0票数 2

免责声明:类似的主题没有为我的问题提供有效的解决方案!

manually)

  • Established

  • 重新启动MongoDB服务器(在错误发生时继续运行)使用windows上的MongoDB服务器作为服务(启动它通过MongoDB Shell CLI包建立默认连接(mongodb://127.0.0.1:27017/directConnection=true&serverSelectionTimeoutMS=2000 )

  • ,称为npm安装和npm启动(通过windows资源监视器列出了

  • MongoDB是running

  • Checked,端口27017是使用TCP占用的,而不受firewall

  • Checked的限制)我没有使用VPN,也没有使用代理连接( interfere.

  • Then ),我打开了(app.listen(3000);)

正在监听的http://localhost:3000/

然而,仍然会出现以下错误:

代码语言:javascript
运行
复制
const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);                                     ^
 
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (\node_modules\mongodb\lib\sdam\topology.js:330:38)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  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: {},
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 536295834,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (\node_modules\mongodb\lib\cmap\connect.js:293:20)
            at Socket.<anonymous> (\node_modules\mongodb\lib\cmap\connect.js:267:22)
            at Object.onceWrapper (node:events:510:26)
            at Socket.emit (node:events:390:28)
            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
  }
}

我的依赖关系:

代码语言:javascript
运行
复制
 "dependencies": {
    "ejs": "^3.1.6",
    "express": "^4.17.1",
    "mongodb": "^4.0.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }

财政年度: Node.js v17.0.1

更新:这里是我的database.js文件

代码语言:javascript
运行
复制
const mongodb = require('mongodb');

const MongoClient = mongodb.MongoClient;

let database;

async function connectToDatabase() {
  const client = await MongoClient.connect('mongodb://localhost:27017');
  database = client.db('file-demo');
}

function getDb() {
  if (!database) {
    throw { message: 'Database not connected!' };
  }
  return database;
}

module.exports = {
  connectToDatabase: connectToDatabase,
  getDb: getDb,
};

这里是我的app.js:

代码语言:javascript
运行
复制
const path = require('path');

const express = require('express');

const userRoutes = require('./routes/users');
const db = require('./data/database');

const app = express();

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));

app.use(userRoutes);

db.connectToDatabase().then(function () {
  app.listen(3000);
});
EN

Stack Overflow用户

回答已采纳

发布于 2021-11-23 14:35:12

正如@prasad_所指出的,在要建立到MongoDB服务器连接的文件中,将localhost更改为127.0.0.1可能会有所帮助。尽管它们应该被视为同义词,并且我能够排除一般问题(您可以在类似的问题中找到答案),但这可能只是铬的缓存问题,就像在here中一样。

票数 11
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70045495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档