我将一个NodeJS应用程序部署到了Heroku,并使用MLab设置了一个MongoDB实例。鉴于不清楚默认数据库用户的密码(它从未要求设置密码),我创建了一个具有管理员特权的新用户,称为mknerr。我像这样设置MONGOLAB_URI变量:
heroku config:set MONGOLAB_URI=mongodb://mknerr:{password}@ds159631.mlab.com:59631/heroku_ws1jw516?authSource=heroku_ws1jw516&w=1
我尝试使用mongo ds159631.mlab.com:59631/heroku_ws1jw516 -u mknerr -p {password}
在本地测试连接,但是尽管已经运行npm mongodb,但我收到错误“ -bash:mongo:找不到命令”。我不再浪费时间试图弄清楚mongo的安装位置,而是切换到了一个我知道已经安装mongo的Cloud9实例,并运行与上面相同的命令。
似乎证实我的凭据是正确的:
MongoDB shell version v3.6.5
connecting to: mongodb://ds159631.mlab.com:59631/heroku_ws1jw5l6
MongoDB server version: 3.6.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
rs-ds159631:PRIMARY>
但是,当我尝试加载应用程序时,它将引发503错误。运行heroku logs --tail
后,我看到:
2018-07-30T15:04:41.171411+00:00 heroku[web.1]: State changed from up to starting
2018-07-30T15:04:42.187835+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-07-30T15:04:42.324261+00:00 heroku[web.1]: Process exited with status 143
2018-07-30T15:04:43.802715+00:00 heroku[web.1]: Starting process with command `npm start`
2018-07-30T15:04:40.930637+00:00 app[api]: Set MONGOLAB_URI config vars by user {me}
2018-07-30T15:04:46.562425+00:00 app[web.1]:
2018-07-30T15:04:46.562447+00:00 app[web.1]: > todos_api@1.0.0 start /app
2018-07-30T15:04:46.562449+00:00 app[web.1]: > node index.js
2018-07-30T15:04:46.562450+00:00 app[web.1]:
2018-07-30T15:04:47.302228+00:00 app[web.1]: App is running on Port 21448
2018-07-30T15:04:47.339759+00:00 app[web.1]: ERROR connecting to: mongodb://mknerr:{password}@ds159631.mlab.com:59631/heroku_ws1jw516?authsSource=heroku_ws1jw516. MongoError: Authentication failed.
2018-07-30T15:04:47.341632+00:00 app[web.1]: (node:20) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
2018-07-30T15:04:47.341635+00:00 app[web.1]: at /app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:598:61
2018-07-30T15:04:47.341637+00:00 app[web.1]: at authenticateStragglers (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:516:16)
2018-07-30T15:04:47.341638+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:552:5)
2018-07-30T15:04:47.341640+00:00 app[web.1]: at emitMessageHandler (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:309:10)
2018-07-30T15:04:47.341642+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:452:17)
2018-07-30T15:04:47.341644+00:00 app[web.1]: at emitOne (events.js:116:13)
2018-07-30T15:04:47.341646+00:00 app[web.1]: at Socket.emit (events.js:211:7)
2018-07-30T15:04:47.341647+00:00 app[web.1]: at addChunk (_stream_readable.js:263:12)
2018-07-30T15:04:47.341649+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:250:11)
2018-07-30T15:04:47.341650+00:00 app[web.1]: at Socket.Readable.push (_stream_readable.js:208:10)
2018-07-30T15:04:47.341652+00:00 app[web.1]: at TCP.onread (net.js:597:20)
2018-07-30T15:04:47.341703+00:00 app[web.1]: (node:20) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
2018-07-30T15:04:47.341773+00:00 app[web.1]: (node:20) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
为什么尽管我已经确认我的凭据是正确的,但为什么仍无法连接并引发身份验证错误?