部署到Heroku时无法连接到mongo

时间:2018-01-23 06:08:15

标签: javascript mongodb heroku mongoose mlab

我正在尝试将我的节点应用程序部署到Heroku。

我添加了MLab MongoDB添加并设置了配置变量

$ heroku config
=== thevoyageapp Config Vars
MONGODB_URI:            mongodb://heroku_f80ngvbz:btt04p7erog80@ds.mlab.com:11/herokgvbz

并已将process.env.MONGODB_URI添加到我的mongo连接

const mongoose = require('mongoose');
let url = process.env.MONGODB_URI || 'mongodb://localhost/trip';
mongoose.connect(url, { useMongoClient: true });

但我仍然会收到此错误:

2018-01-23T06:00:15.722831+00:00 heroku[web.1]: Starting process with command `node server/index.js`
2018-01-23T06:00:17.808211+00:00 app[web.1]: mongodb://heroku_f80ngvbz:btt04p7erog80cot3ufp1@ds.mlab.com:11/heroku_f80ngvbz
2018-01-23T06:00:17.807864+00:00 app[web.1]: listening on port 3000         ^
2018-01-23T06:00:17.835657+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
2018-01-23T06:00:17.835658+00:00 app[web.1]:     at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:336:35)
2018-01-23T06:00:17.835659+00:00 app[web.1]:     at emitOne (events.js:116:13)
2018-01-23T06:00:17.835663+00:00 app[web.1]:     at Connection.emit (events.js:214:7)
2018-01-23T06:00:17.835660+00:00 app[web.1]:     at Pool.emit (events.js:211:7)
2018-01-23T06:00:17.835661+00:00 app[web.1]:     at Object.onceWrapper (events.js:317:30)
2018-01-23T06:00:17.835661+00:00 app[web.1]:     at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
2018-01-23T06:00:17.835664+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:189:49)
2018-01-23T06:00:17.835662+00:00 app[web.1]:     at emitTwo (events.js:126:13)
2018-01-23T06:00:17.835664+00:00 app[web.1]:     at Object.onceWrapper (events.js:315:30)
2018-01-23T06:00:17.835665+00:00 app[web.1]:     at emitOne (events.js:116:13)
2018-01-23T06:00:17.835666+00:00 app[web.1]:     at Socket.emit (events.js:211:7)
2018-01-23T06:00:17.835667+00:00 app[web.1]:     at emitErrorNT (internal/streams/destroy.js:64:8)
2018-01-23T06:00:17.835668+00:00 app[web.1]:     at _combinedTickCallback (internal/process/next_tick.js:138:11)
2018-01-23T06:00:17.835668+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:180:9)
2018-01-23T06:00:17.940249+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-23T06:00:17.926212+00:00 heroku[web.1]: Process exited with status 1

知道可能是什么问题吗?

1 个答案:

答案 0 :(得分:2)

您很有可能忘记将服务器IP列入白名单以访问MongoDB数据库。您可以在MongoDB Atlas中将该IP列入白名单。