【问题标题】:MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connection timed out]MongoNetworkError:第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError:连接超时]
【发布时间】:2021-01-12 09:24:07
【问题描述】:

我已经安装了 mongodb 4.2.3 版,但是我无法连接我的克隆项目。显示以下错误

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connection timed out]

但我可以将数据库与我的本地项目连接起来,还可以通过执行事务进行检查。 mongo 命令输出如下所示

MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a6f6b036-ad62-4e38-b0d2-50bfda540016") }
MongoDB server version: 4.4.3
---
The server generated these startup warnings when booting: 
        2021-01-12T12:43:25.733+05:30: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-01-12T12:43:31.420+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> 

我的mongodb连接代码如下

// Connect to mongodb
var connect = function () {
  var options = {
    keepAlive: 1,
    autoReconnect: true,
    useNewUrlParser: true,
    useUnifiedTopology:true,
    serverSelectionTimeoutMS:60000,
    socketTimeoutMS:60000
  };
  mongoose.connect(config.db, options);
};

connect();
mongoose.connection.on("error", console.log);
mongoose.connection.on("disconnected", connect);

我也尝试过增加超时但没有解决方案。

【问题讨论】:

    标签: mongodb npm npm-start


    【解决方案1】:

    我最初遇到这个问题可能是由于系统缓慢。我整天都在为这个问题苦苦挣扎。但是当我重新启动系统时我解决了。所以最后重新启动系统以及 mongod 服务解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2022-07-27
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多