【问题标题】:Heroku nodejs/mongoose and mLab app crash with timeoutHeroku nodejs/mongoose 和 mLab 应用程序因超时而崩溃
【发布时间】:2017-07-08 08:09:05
【问题描述】:

下面是heroku日志的输出。我尝试设置连接参数

    socketTimeoutMS: 0,
    connectionTimeout: 0

但还是会发生

[web.1]: events.js:160
[web.1]:       throw er; // Unhandled 'error' event
[web.1]:       ^
[web.1]: 
[web.1]: Error: connection timeout
/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:169:17)
........
[web.1]: npm ERR! Linux 3.13.0-105-generic
/.heroku/node/bin/npm" "start"
[web.1]: npm ERR! node v6.9.5
[web.1]: npm ERR! npm  v3.10.10
[web.1]: npm ERR! code ELIFECYCLE

【问题讨论】:

  • 你使用的是什么版本的 mongoose,mlab 上的 mongodb 版本是什么
  • 你确定你给了mongo labs的正确地址和认证密码吗?
  • @RaviShankar 是的。甚至它以前也可以工作。
  • @FemiOladeji mongoose 4.8.3 目前在运行 npm update 之后甚至在我更新之前就存在问题。

标签: node.js heroku mongoose


【解决方案1】:

我为此联系了 mLab。这是他们的支持人员的回应:

从 Heroku 连接时,我们经常建议 30 秒的连接超时。您可以在此处查看我们建议的 Mongoose 配置: https://gist.github.com/mongolab-org/9959376

以下是该 github gist 中的连接配置:

// mongoose 4.3.x
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.                         
});

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 2016-08-27
    • 2013-02-04
    • 2016-03-07
    • 2015-11-24
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多