MongoDB mLab猫鼬节点。在一段空闲时间之后连接超时?

时间:2021-12-15 13:00:35

I have a simple Node.js that uses mongoose to connect with Mongo database hosted on mLab.

我有一个简单的节点。使用mongoose连接托管在mLab上的Mongo数据库的js。

Everything seems working just fine: adding new records, querying for existing stuff.

一切似乎都运行得很好:添加新记录,查询现有内容。

Only sometimes, after some period of inactivity, when I look at the console I see the following:

只有在经过一段时间的不活动之后,当我看到控制台时,我才会看到以下内容:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: connection timeout
    at Db.<anonymous> (___PATH___/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:168:17)
    at emitTwo (events.js:106:13)
    at Db.emit (events.js:191:7)
    at Server.listener (___PATH___/node_modules/mongodb/lib/db.js:1786:14)
    at emitOne (events.js:96:13)
    at Server.emit (events.js:188:7)
    at Server.<anonymous> (___PATH___/node_modules/mongodb/lib/server.js:274:14)
    at emitOne (events.js:96:13)
    at Server.emit (events.js:188:7)
    at Pool.<anonymous> (___PATH___/node_modules/mongodb-core/lib/topologies/server.js:334:12)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (___PATH___/node_modules/mongodb-core/lib/connection/pool.js:270:12)
    at Connection.g (events.js:292:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)

Right now it doesn't matter that much to me - I can always restart the app. I'm worried that in production it will cause a lot of headaches so I preemptively ask what's the issue here?

现在,这对我来说并不重要——我总是可以重新启动这个应用。我担心在生产中它会引起很多麻烦,所以我先问一下这里的问题是什么?

Note that initially everything is working fine, it after some time when I get Error: connection timeout

注意,开始时一切都很正常,但在我出错的时候:连接超时。

2 个解决方案

#1


0  

  1. it seems that there is fluctuation in your internet connection. Maybe this is the reason of connection timeout.
  2. 你的网络连接似乎有波动。也许这就是连接超时的原因。
  3. you can handle it by setting the timeout.
  4. 您可以通过设置超时来处理它。

As below :

如下:

var timeout = require('connect-timeout');
app.use(timeout('5s'));`

in your app.js file

app.js文件中

#2


0  

Replace your mongoose connection setting by this code:

用以下代码替换您的mongoose连接设置:

var mongoose = require('mongoose');

/* 
 * Mongoose by default sets the auto_reconnect option to true.
 * We recommend setting socket options at both the server and replica set level.
 * We recommend a 30 second connection timeout because it allows for 
 * plenty of time in most operating environments.
 */
var options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } }, 
                replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };       

var mongodbUri = 'mongodb://user:pass@host:port/db';

mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;             

conn.on('error', console.error.bind(console, 'connection error:'));  

conn.once('open', function() {
  // Wait for the database connection to establish, then start the app.                         
});

#1


0  

  1. it seems that there is fluctuation in your internet connection. Maybe this is the reason of connection timeout.
  2. 你的网络连接似乎有波动。也许这就是连接超时的原因。
  3. you can handle it by setting the timeout.
  4. 您可以通过设置超时来处理它。

As below :

如下:

var timeout = require('connect-timeout');
app.use(timeout('5s'));`

in your app.js file

app.js文件中

#2


0  

Replace your mongoose connection setting by this code:

用以下代码替换您的mongoose连接设置:

var mongoose = require('mongoose');

/* 
 * Mongoose by default sets the auto_reconnect option to true.
 * We recommend setting socket options at both the server and replica set level.
 * We recommend a 30 second connection timeout because it allows for 
 * plenty of time in most operating environments.
 */
var options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } }, 
                replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };       

var mongodbUri = 'mongodb://user:pass@host:port/db';

mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;             

conn.on('error', console.error.bind(console, 'connection error:'));  

conn.once('open', function() {
  // Wait for the database connection to establish, then start the app.                         
});