以下是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
答案 0 :(得分:2)
我联系了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.
});