简体   繁体   English

Strapi Heroku 部署无法连接到 MongoDB

[英]Strapi Heroku deployment fails to connect to MongoDB

I am trying to deploy my Strapi app to Heroku with a mongoDB but receive this error every time I try to deploy.我正在尝试使用 mongoDB 将我的 Strapi 应用程序部署到 Heroku 但每次尝试部署时都会收到此错误。

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

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.该应用程序在本地工作,我遵循了许多教程 1、2) ,包括许多版本的官方 Strapi 文档以使其正常工作,但它们都导致了这个错误。 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.我查看了类似的堆栈问题/Strapi github 问题,看看我的问题是否可以通过调整他们的答案来解决,但没有任何效果。

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 .我已确保我的heroku config database_url 和 database_name 与我的database_uri字符串匹配,并且我使用yarn create strapi-app backend创建了 Strapi 应用程序,然后选择了 mongo,确保不使用--quickstart

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我的完整的 Strapi 应用程序,包括database.js, server.js, *config/environments/production/database.json可以在这里查看https://github.com/KyleSFraser/Portfolio/tree/master/backend

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.我非常感谢任何能解决这个反复出现的问题并使用 Heroku 和 Strapi 成功部署我的 mongoDB 的指针。

EDIT Removing previous config vars now that it is working编辑删除以前的配置变量,因为它正在工作

Adding uri: env('DATABASE_URI' || ''), to my database.js file has solved the issue, not sure why it was missing in the first place.添加uri: env('DATABASE_URI' || ''),到我的database.js文件已经解决了这个问题,不知道为什么它首先丢失。

For anyone with a similar issue, here is my database.js and server.js for reference;对于任何有类似问题的人,这是我的database.jsserver.js供参考;

database.js数据库.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服务器.js

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

Finally make sure all you Heroku config vars are set to match the relevant breakdown of your mongoDB/database URI最后确保您所有的 Heroku 配置变量都设置为匹配您的 mongoDB/数据库 URI 的相关细分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM