Strapi Heroku部署无法连接到MongoDB [英] Strapi Heroku deployment fails to connect to MongoDB

查看:265
本文介绍了Strapi Heroku部署无法连接到MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mongoDB将Strapi应用程序部署到Heroku,但是每次尝试部署时都会收到此错误。

I am trying to deploy my Strapi app to Heroku with a mongoDB but receive this error every time I try to deploy.

debug ⛔️ Server wasn't able to start properly.
2020-06-11T10:38:53.257748+00:00 app[web.1]: [2020-06-11T10:38:53.257Z] error Error connecting to the Mongo database. Server selection timed out after 30000 ms
2020-06-11T10:38:53.268085+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-11T10:38:53.268583+00:00 app[web.1]: npm ERR! errno 1
2020-06-11T10:38:53.270443+00:00 app[web.1]: npm ERR! backend@0.1.0 start: `strapi start`
2020-06-11T10:38:53.270697+00:00 app[web.1]: npm ERR! Exit status 1

该应用程序在本地运行,我遵循了许多教程(1 2),包括许多版本官方Strapi文档为了使其正常工作,但它们均会导致此错误。我看过类似的Stack问题/ Strapi github问题,看是否可以通过改编他们的答案来解决我的问题,但是没有用。

The app works locally and I have followed numerous tutorials (1, 2), including many versions of the offical Strapi documentation in order to get this working but they all result in this error. I have looked at similar Stack questions / Strapi github Issues to see if my issue can be solved by adapting their answers but nothing works.

我确保我的 heroku config database_url和database_name匹配我的 database_uri 字符串,然后我用 yarn创建了bandi应用程序create create stripi-app后端,然后选择mongo,请确保不要使用-quickstart

I have ensured my heroku config database_url and database_name matches my database_uri string, and I created the strapi app with yarn create strapi-app backend then selected mongo, making sure not to use --quickstart.

我的完整应用程序,包括 database.js,server.js,* config / environments / production / database.json 可以在此处查看 https://github.com/KyleSFraser/Portfolio/tree/主/后端

My full strapi app, including database.js, server.js, *config/environments/production/database.json can be viewed here https://github.com/KyleSFraser/Portfolio/tree/master/backend

我一直在不断尝试使用尝试部署的文档或教程来重建后端。我非常感谢您提供任何指针来解决此重复出现的问题,并使用Heroku&成功地部署mongoDB。

I've been going in circles constantly rebuilding the backend using the docs or tutorials trying to deploy. I'd really appreciate any pointers to solve this recurring issue and successfully deploy my mongoDB using Heroku & Strapi.

编辑
删除以前的配置变量,因为它可以正常工作

EDIT Removing previous config vars now that it is working

推荐答案

uri:env('DATABASE_URI'||''),添加到我的数据库中。 js 文件已解决了该问题,不确定为什么一开始就找不到它。

Adding uri: env('DATABASE_URI' || ''), to my database.js file has solved the issue, not sure why it was missing in the first place.

对于有类似问题的任何人,这是我的 database.js 服务器。 js 供参考;

For anyone with a similar issue, here is my database.js and server.js for reference;

database.js

  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'mongoose',
      settings: {
        uri: env('DATABASE_URI' || ''),
        host: env('DATABASE_HOST', '127.0.0.1'),
        srv: env.bool('DATABASE_SRV', false),
        port: env.int('DATABASE_PORT', 27017),
        database: env('DATABASE_NAME', '(backend)'),
        username: env('DATABASE_USERNAME', ''),
        password: env('DATABASE_PASSWORD', ''),
      },
      options: {
        authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
        ssl: env.bool('DATABASE_SSL', false),
      },
    },
  },
});

server.js

server.js

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
});

最后确保所有的Heroku配置变量都设置为与mongoDB /数据库URI的相关分类相匹配

Finally make sure all you Heroku config vars are set to match the relevant breakdown of your mongoDB/database URI

这篇关于Strapi Heroku部署无法连接到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆