以前部署到 Heroku 时可以工作的 KeystoneJS V4 应用程序现在停止工作 - MongoDB Atlas

Posted

技术标签:

【中文标题】以前部署到 Heroku 时可以工作的 KeystoneJS V4 应用程序现在停止工作 - MongoDB Atlas【英文标题】:KeystoneJS V4 app that used to work when deploying to Heroku is now stopped working - MongoDB Atlas 【发布时间】:2021-09-07 06:48:37 【问题描述】:

由于堆栈更改,Heroku 删除了几个月前与 MongoDB Atlas 完美连接的 KeystoneJS V4 应用程序。现在我正在尝试使用正确的配置部署相同的应用程序,但出现部署错误。这是我得到的错误:

2021-06-23T17:14:40.418968+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-23T17:14:43.000000+00:00 app[api]: Build succeeded
2021-06-23T17:14:46.464645+00:00 heroku[web.1]: Starting process with command `node keystone.js`
2021-06-23T17:14:49.588440+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2021-06-23T17:14:49.588949+00:00 app[web.1]: designed for a production environment, as it will leak
2021-06-23T17:14:49.588950+00:00 app[web.1]: memory, and will not scale past a single process.
2021-06-23T17:14:49.711527+00:00 app[web.1]: ------------------------------------------------
2021-06-23T17:14:49.711682+00:00 app[web.1]: Mongoose connection "error" event fired with:
2021-06-23T17:14:49.712891+00:00 app[web.1]: Error [MongoError]: failed to connect to server [cluster-1z703hln.tt6wq.mongodb.net:27017] on first connect
2021-06-23T17:14:49.712894+00:00 app[web.1]:     at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:326:35)
2021-06-23T17:14:49.712894+00:00 app[web.1]:     at Pool.emit (events.js:314:20)
2021-06-23T17:14:49.712894+00:00 app[web.1]:     at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:272:12)
2021-06-23T17:14:49.712895+00:00 app[web.1]:     at Object.onceWrapper (events.js:421:26)
2021-06-23T17:14:49.712895+00:00 app[web.1]:     at Connection.emit (events.js:314:20)
2021-06-23T17:14:49.712896+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:175:49)
2021-06-23T17:14:49.712896+00:00 app[web.1]:     at Object.onceWrapper (events.js:421:26)
2021-06-23T17:14:49.712897+00:00 app[web.1]:     at Socket.emit (events.js:314:20)
2021-06-23T17:14:49.712897+00:00 app[web.1]:     at emitErrorNT (internal/streams/destroy.js:92:8)
2021-06-23T17:14:49.712897+00:00 app[web.1]:     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2021-06-23T17:14:49.712899+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:84:21)
2021-06-23T17:14:49.713715+00:00 app[web.1]: Error: KeystoneJS (My-App) failed to start - Check that you are running `mongod` in a separate process.
2021-06-23T17:14:49.713716+00:00 app[web.1]:     at NativeConnection.<anonymous> (/app/node_modules/keystone/lib/core/openDatabaseConnection.js:59:10)
2021-06-23T17:14:49.713716+00:00 app[web.1]:     at NativeConnection.emit (events.js:314:20)
2021-06-23T17:14:49.713717+00:00 app[web.1]:     at Immediate.<anonymous> (/app/node_modules/mongoose/lib/connection.js:296:19)
2021-06-23T17:14:49.713717+00:00 app[web.1]:     at processImmediate (internal/timers.js:461:21)
2021-06-23T17:14:49.801512+00:00 heroku[web.1]: Process exited with status 1
2021-06-23T17:14:49.940361+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-23T17:14:49.950336+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-23T17:14:57.803099+00:00 heroku[web.1]: Starting process with command `node keystone.js`
2021-06-23T17:15:02.052669+00:00 heroku[web.1]: Process exited with status 1
2021-06-23T17:15:02.247835+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-23T17:15:01.788675+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2021-06-23T17:15:01.788702+00:00 app[web.1]: designed for a production environment, as it will leak
2021-06-23T17:15:01.788703+00:00 app[web.1]: memory, and will not scale past a single process.
2021-06-23T17:15:01.971420+00:00 app[web.1]: ------------------------------------------------
2021-06-23T17:15:01.971542+00:00 app[web.1]: Mongoose connection "error" event fired with:
2021-06-23T17:15:01.975012+00:00 app[web.1]: Error [MongoError]: failed to connect to server [cluster-1z703hln.tt6wq.mongodb.net:27017] on first connect
2021-06-23T17:15:01.975013+00:00 app[web.1]:     at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:326:35)
2021-06-23T17:15:01.975014+00:00 app[web.1]:     at Pool.emit (events.js:314:20)
2021-06-23T17:15:01.975014+00:00 app[web.1]:     at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:272:12)
2021-06-23T17:15:01.975014+00:00 app[web.1]:     at Object.onceWrapper (events.js:421:26)
2021-06-23T17:15:01.975015+00:00 app[web.1]:     at Connection.emit (events.js:314:20)
2021-06-23T17:15:01.975015+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:175:49)
2021-06-23T17:15:01.975016+00:00 app[web.1]:     at Object.onceWrapper (events.js:421:26)
2021-06-23T17:15:01.975016+00:00 app[web.1]:     at Socket.emit (events.js:314:20)
2021-06-23T17:15:01.975016+00:00 app[web.1]:     at emitErrorNT (internal/streams/destroy.js:92:8)
2021-06-23T17:15:01.975017+00:00 app[web.1]:     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2021-06-23T17:15:01.975017+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:84:21)
2021-06-23T17:15:01.975867+00:00 app[web.1]: Error: KeystoneJS (My-APP) failed to start - Check that you are running `mongod` in a separate process.
2021-06-23T17:15:01.975868+00:00 app[web.1]:     at NativeConnection.<anonymous> (/app/node_modules/keystone/lib/core/openDatabaseConnection.js:59:10)
2021-06-23T17:15:01.975868+00:00 app[web.1]:     at NativeConnection.emit (events.js:314:20)
2021-06-23T17:15:01.975868+00:00 app[web.1]:     at Immediate.<anonymous> (/app/node_modules/mongoose/lib/connection.js:296:19)
2021-06-23T17:15:01.975869+00:00 app[web.1]:     at processImmediate (internal/timers.js:461:21)

请有人帮我解决这个问题。不知道为什么我会收到这个错误。我有正确的 MONGODB_URI(在另一个应用上可以正常工作)。

【问题讨论】:

【参考方案1】:

该错误 (failed to connect to server) 直接来自 Mongoose,因此这是数据库连接的问题。原因可能是……

网络

Atlas 默认阻止所有流量;对于 Heroku 部署,您需要通过配置 access list 来打开它。在这种情况下,它应该包含类似0.0.0.0/32 的内容以允许所有 IP。 (我知道你说过可以从另一个应用程序访问数据库,但仍然值得检查。)

授权

URI 是否包含用户名密码?它应该是……

mongodb+srv://<username>:<password>@cluster-1z703hln.tt6wq.mongodb.net/my-app?retryWrites=true&w=majority

.. 或类似,不是 Atlas 为 CLI 建议的较短形式,例如。

mongodb+srv://cluster-1z703hln.tt6wq.mongodb.net/my-app

当您尝试从开发机器上的mongo 客户端连接时会发生什么?它可能会提供更有用的错误消息。

【讨论】:

谢谢。网络 - 是的,它已经配置为使用 0.0.0.0/0 访问数据库以允许所有 IP。 Auth - 是的,URI 格式正确,UN & PW 正确。用于访问另一个应用程序的相同。唯一的区别是,先前的错误之一建议删除 +srv。所以它以 mongodb:// 开头 - 我之前遇到过这个问题,在删除 +srv 之后,它才让连接到数据库。 @billgajen 你的意思是0.0.0.0/32 对吗? 0.0.0.0/0 将阻止一切。除此之外,我没有想法。您是否尝试在您的开发机器上从 Mongo 客户端(如 mongo cli)进行连接? 嗨@molomby,最后这个答案帮助解决了这个问题:***.com/questions/59778351/…。谢谢

以上是关于以前部署到 Heroku 时可以工作的 KeystoneJS V4 应用程序现在停止工作 - MongoDB Atlas的主要内容,如果未能解决你的问题,请参考以下文章

Heroku 中的 Node.js 应用程序在 Foreman 上工作,但在部署时不工作?

为啥 datatables.net 在我的 Rails 开发环境中可以工作,但在部署到 Heroku 时却不行?

尝试将流式应用程序部署到 Heroku 时出现 AttributeError

Heroku 当我在线部署我的应用程序时它崩溃但在我的本地机器上它可以正常工作

React 应用在部署到 Heroku 后没有得到 API 结果

Heroku Discord Bot 构建但在部署时无法工作