无法从 heroku 连接到 Mongodb 数据库
Unable to connect to Mongodb database from heroku
我使用 yarn create strapi-app velankannimatha
创建了一个 strapi 网站并选择了 mongo 作为数据库。我在 MongoDB atlas 中创建了一个名为 velankannimathadb
.
的数据库
我在设置配置变量(即 DATABASE_URI
和 DATABASE_NAME
)后将站点推送到 Heroku。
但是应用程序一直崩溃。当我检查日志时,它说
Server wasn't able to start properly.
Error connecting to the Mongo database. Server selection timed out after 30000 ms
我能够在本地访问数据库(在开发模式下)。它仅在来自 Heroku 的 运行 时失败。
非常感谢任何帮助
这可能是由于 Mongo Atlas 只允许来自特定 IP 地址的连接。参见 the docs. Although not recommended, you could make the cluster available to any IP with 0.0.0.0/0
. You may also find the troubleshooting docs 也很有帮助。
您需要将 config/server.js
主机密钥从 localhost 更改为 0.0.0.0
,因为 Strapi 正在尝试绑定到 localhost,而 Heroku 正在寻找暴露的服务器到 "proxy"
之前的答案也可能是您的 Mongo 集群确实需要允许访问 0.0.0.0/0,因为 heroku dynos 随着时间的推移而变化并且每次都使用不同的 IP。
现在可以使用了。出问题的是我使用的是 MongoDB 版本 2.2.12 or later
(根据 documentation)。当我切换到版本 3.6 or later
并使用更新的连接字符串时,一切正常。
另外,从终端添加连接字符串作为配置变量会导致问题,即使它被保存,一些字符也会丢失。所以我从浏览器添加了它们。
medium 上的这个 article 也帮了大忙。
我使用 yarn create strapi-app velankannimatha
创建了一个 strapi 网站并选择了 mongo 作为数据库。我在 MongoDB atlas 中创建了一个名为 velankannimathadb
.
我在设置配置变量(即 DATABASE_URI
和 DATABASE_NAME
)后将站点推送到 Heroku。
但是应用程序一直崩溃。当我检查日志时,它说
Server wasn't able to start properly. Error connecting to the Mongo database. Server selection timed out after 30000 ms
我能够在本地访问数据库(在开发模式下)。它仅在来自 Heroku 的 运行 时失败。 非常感谢任何帮助
这可能是由于 Mongo Atlas 只允许来自特定 IP 地址的连接。参见 the docs. Although not recommended, you could make the cluster available to any IP with 0.0.0.0/0
. You may also find the troubleshooting docs 也很有帮助。
您需要将 config/server.js
主机密钥从 localhost 更改为 0.0.0.0
,因为 Strapi 正在尝试绑定到 localhost,而 Heroku 正在寻找暴露的服务器到 "proxy"
之前的答案也可能是您的 Mongo 集群确实需要允许访问 0.0.0.0/0,因为 heroku dynos 随着时间的推移而变化并且每次都使用不同的 IP。
现在可以使用了。出问题的是我使用的是 MongoDB 版本 2.2.12 or later
(根据 documentation)。当我切换到版本 3.6 or later
并使用更新的连接字符串时,一切正常。
另外,从终端添加连接字符串作为配置变量会导致问题,即使它被保存,一些字符也会丢失。所以我从浏览器添加了它们。
medium 上的这个 article 也帮了大忙。