Connection error on Heroku + MongoLab addon with Node.js app - Error: connect ECONNREFUSED 127.0.0.1:27017
Connection error on Heroku + MongoLab addon with Node.js app - Error: connect ECONNREFUSED 127.0.0.1:27017
当尝试使用 MongoLab 插件启动 Node.js 托管在 Heroku 上的应用程序时,
连接到 MongoDB 时,我在 Heroku 日志中收到以下错误。看起来它正在尝试连接到本地 mongo,即使我已经设置了 heroku 环境变量。
我的全部代码都在 GitHub 上:https://github.com/yhagio/meetup_planner
Heroku 日志
2015-11-28T22:22:58.460531+00:00 heroku[web.1]: Starting process with command `node server.js`
2015-11-28T22:22:59.353911+00:00 app[web.1]: Server started at port number: 38701
2015-11-28T22:22:59.303795+00:00 app[web.1]: Connecting to DB : mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:22:59.371719+00:00 app[web.1]:
2015-11-28T22:22:59.371723+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:22:59.371725+00:00 app[web.1]: process.nextTick(function() { throw err; })
2015-11-28T22:22:59.371725+00:00 app[web.1]: ^
2015-11-28T22:22:59.371729+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:22:59.371730+00:00 app[web.1]: at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:22:59.371731+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:22:59.371732+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.052255+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:00.880672+00:00 app[web.1]: Connecting to DB : mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:23:00.939388+00:00 app[web.1]:
2015-11-28T22:23:00.939392+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:23:00.939393+00:00 app[web.1]: process.nextTick(function() { throw err; })
2015-11-28T22:23:00.939397+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:23:00.939394+00:00 app[web.1]: ^
2015-11-28T22:23:00.939398+00:00 app[web.1]: at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:23:00.939399+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:23:00.939400+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.917276+00:00 app[web.1]: Server started at port number: 42573
2015-11-28T22:23:01.626965+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-28T22:23:01.614846+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:13.224552+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=warm-retreat-5113.herokuapp.com request_id=54404d8a-7744-49ae-b744-4417abd4b347 fwd="166.62.227.154" dyno= connect= service= status=503 bytes=
2015-11-28T22:23:13.581322+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=warm-retreat-5113.herokuapp.com request_id=063c0fef-8260-46b5-a0f6-b4d546e9a60b fwd="166.62.227.154" dyno= connect= service= status=503 bytes=
这是我连接到 MongoDB 的代码(来自 Heroku 的 MongoLab 插件)
server/db_connection.js
var mongoose = require('mongoose');
var config = require('../express-config');
var userModel = require('./models/user');
var eventModel = require('./models/event');
function makeDefaultConnection() {
var uri = process.env.MONGOLAB_URI || '127.0.0.1/meetupplanner';
console.log('Connecting to DB : ', uri);
var conn = mongoose.createConnection(uri);
conn.on('error', function(err){
console.log('Connection Error ::: ', err);
});
conn.model('User', userModel.userSchema);
conn.model('Event', eventModel.eventSchema);
return conn;
}
module.exports.defaultConnection = makeDefaultConnection();
简介
web: node server.js
在Server.js
...
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log('Server started at port number: ', port);
});
在 Heroku 上添加 MongoLab 附加组件后,我确认我的 MONGOLAB_URI 已在 Heroku 上设置(设置 > 配置变量)
参考:https://devcenter.heroku.com/articles/mongolab
Whosebug 上提出的类似问题:
- can't connect to mongolab with node.js on heroku
- Use Heroku addon mongolab with node.js
请注意,我可以通过以下方式连接到 mongo shell:
$ mongo ds059804.mongolab.com:59804/heroku_tb6frdh6 -u USERNAME -p PASSWORD
我觉得凭据没问题。
作为下一步,您为什么不这样做,以确保配置变量正在通过
var uri = process.env.MONGOLAB_URI || config.dbHostName;
console.log('===== Connecting to DB ... =====', uri);
var conn = mongoose.createConnection(uri,...
我终于明白了。是connect-mongo with session.
的中间件配置
在我的配置中,它是:
app.use(session({
...
// ===== Following 3 lines are trying to connect to local db =====
store: new MongoStore({
'db': 'meetupplanner'
}),
// ===== So, I replaced 3 lines above with following =====
store: new MongoStore({
url: process.env.MONGOLAB_URI
}),
...
当尝试使用 MongoLab 插件启动 Node.js 托管在 Heroku 上的应用程序时, 连接到 MongoDB 时,我在 Heroku 日志中收到以下错误。看起来它正在尝试连接到本地 mongo,即使我已经设置了 heroku 环境变量。
我的全部代码都在 GitHub 上:https://github.com/yhagio/meetup_planner
Heroku 日志
2015-11-28T22:22:58.460531+00:00 heroku[web.1]: Starting process with command `node server.js`
2015-11-28T22:22:59.353911+00:00 app[web.1]: Server started at port number: 38701
2015-11-28T22:22:59.303795+00:00 app[web.1]: Connecting to DB : mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:22:59.371719+00:00 app[web.1]:
2015-11-28T22:22:59.371723+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:22:59.371725+00:00 app[web.1]: process.nextTick(function() { throw err; })
2015-11-28T22:22:59.371725+00:00 app[web.1]: ^
2015-11-28T22:22:59.371729+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:22:59.371730+00:00 app[web.1]: at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:22:59.371731+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:22:59.371732+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.052255+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:00.880672+00:00 app[web.1]: Connecting to DB : mongodb://USERNAME:PASSWORD@ds059804.mongolab.com:59804/heroku_tb6frdh6
2015-11-28T22:23:00.939388+00:00 app[web.1]:
2015-11-28T22:23:00.939392+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T22:23:00.939393+00:00 app[web.1]: process.nextTick(function() { throw err; })
2015-11-28T22:23:00.939397+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T22:23:00.939394+00:00 app[web.1]: ^
2015-11-28T22:23:00.939398+00:00 app[web.1]: at Object.exports._errnoException (util.js:860:11)
2015-11-28T22:23:00.939399+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T22:23:00.939400+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T22:23:00.917276+00:00 app[web.1]: Server started at port number: 42573
2015-11-28T22:23:01.626965+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-28T22:23:01.614846+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T22:23:13.224552+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=warm-retreat-5113.herokuapp.com request_id=54404d8a-7744-49ae-b744-4417abd4b347 fwd="166.62.227.154" dyno= connect= service= status=503 bytes=
2015-11-28T22:23:13.581322+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=warm-retreat-5113.herokuapp.com request_id=063c0fef-8260-46b5-a0f6-b4d546e9a60b fwd="166.62.227.154" dyno= connect= service= status=503 bytes=
这是我连接到 MongoDB 的代码(来自 Heroku 的 MongoLab 插件) server/db_connection.js
var mongoose = require('mongoose');
var config = require('../express-config');
var userModel = require('./models/user');
var eventModel = require('./models/event');
function makeDefaultConnection() {
var uri = process.env.MONGOLAB_URI || '127.0.0.1/meetupplanner';
console.log('Connecting to DB : ', uri);
var conn = mongoose.createConnection(uri);
conn.on('error', function(err){
console.log('Connection Error ::: ', err);
});
conn.model('User', userModel.userSchema);
conn.model('Event', eventModel.eventSchema);
return conn;
}
module.exports.defaultConnection = makeDefaultConnection();
简介
web: node server.js
在Server.js
...
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log('Server started at port number: ', port);
});
在 Heroku 上添加 MongoLab 附加组件后,我确认我的 MONGOLAB_URI 已在 Heroku 上设置(设置 > 配置变量)
参考:https://devcenter.heroku.com/articles/mongolab
Whosebug 上提出的类似问题:
- can't connect to mongolab with node.js on heroku
- Use Heroku addon mongolab with node.js
请注意,我可以通过以下方式连接到 mongo shell:
$ mongo ds059804.mongolab.com:59804/heroku_tb6frdh6 -u USERNAME -p PASSWORD
我觉得凭据没问题。
作为下一步,您为什么不这样做,以确保配置变量正在通过
var uri = process.env.MONGOLAB_URI || config.dbHostName;
console.log('===== Connecting to DB ... =====', uri);
var conn = mongoose.createConnection(uri,...
我终于明白了。是connect-mongo with session.
的中间件配置在我的配置中,它是:
app.use(session({
...
// ===== Following 3 lines are trying to connect to local db =====
store: new MongoStore({
'db': 'meetupplanner'
}),
// ===== So, I replaced 3 lines above with following =====
store: new MongoStore({
url: process.env.MONGOLAB_URI
}),
...