Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

I have installed mongodb version 4.2.3 but I am unable to connect my cloned project. showing below error

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

But I am able to connect database with my local project and also checked by performing transactions. mongo commmand output shown below

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()
---
> 

My mongodb connection code is below

// 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);

I have also tried by increasing timeout but no resolution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was initially facing this issue maybe due to the slow system. I struggled with this issue full day long. But my resolved when I restarted my system. So finally restarting the system as well as the mongod service resolved my issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...