node.js - Heroku + MongoLab 插件与 Node.js 应用程序的连接错误 - 错误 : connect ECONNREFUSED 127. 0.0.1:27017

标签 node.js mongodb heroku mongoose mlab

当尝试使用 MongoLab 插件启动 Heroku 上托管的 Node.js 应用程序时, 连接到 MongoDB 时,我在 Heroku 日志上收到以下错误。尽管我已经设置了 heroku 环境变量,但它似乎正在尝试连接到本地 mongo。

我的完整代码位于 GitHub 上:https://github.com/yhagio/meetup_planner

My gist for this issue

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:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5b5a4b6b6b2aab7a1a58196d5d0dcddd5d1cb888a8b828a898487cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>: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:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6232233131352d302622061152575b5a52564c0f0d0c050d0e03004c010d0f" rel="noreferrer noopener nofollow">[email protected]</a>: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

StackOverflow 上提出的类似问题:

请注意,我可以通过以下方式连接到 mongo shell:

$ mongo ds059804.mongolab.com:59804/heroku_tb6frdh6 -u USERNAME -p PASSWORD

我认为凭据很好。

最佳答案

我终于明白了。这是connect-mongo和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
  }),

  ...

关于node.js - Heroku + MongoLab 插件与 Node.js 应用程序的连接错误 - 错误 : connect ECONNREFUSED 127. 0.0.1:27017,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967094/

相关文章:

javascript - 在 Node.js 中查找两个数组的交集

node.js - Node-amqp 和 socket.io 奇怪的行为

javascript - 聊天应用程序无法在端口 80 上运行 socket.io 和 node.js./express

javascript - 检查 MongoDB 的登录凭据不起作用

MongoDB:数组字段中不同值的总数

node.js - 无法通过 heroku-cli 和 github 将 nodejs 部署到 heroku

heroku - 以编程方式更改 heroku dataclips 的数据库

ssl - 安装 Lets Encrypt SSL 证书后,我可以安全地删除 Expedited SSL 吗?

node.js - 将 n8n 部署到 Heroku

javascript - 对于 Node Js Server 如何引用不同文件夹中的静态 html 文件