【问题标题】:Connection to Mongodb fails from Strapi on Heroku从 Heroku 上的 Strapi 连接到 Mongodb 失败
【发布时间】:2020-05-27 21:48:12
【问题描述】:

我将 Strapi CMS 部署到 Heroku,但出现此错误

连接到 Mongo 数据库时出错。服务器选择在 30000 毫秒后超时

日志:

2020-05-27T17:43:55.398256+00:00 heroku[web.1]: Starting process with command `npm start`
2020-05-27T17:43:58.724121+00:00 app[web.1]: 
2020-05-27T17:43:58.724143+00:00 app[web.1]: > strapi-oskogen-mongodb@0.1.0 start /app
2020-05-27T17:43:58.724143+00:00 app[web.1]: > strapi start
2020-05-27T17:43:58.724143+00:00 app[web.1]: 
2020-05-27T17:44:02.234160+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
2020-05-27T17:44:02.234179+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2020-05-27T17:44:02.234732+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
2020-05-27T17:44:02.234879+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
2020-05-27T17:44:02.235021+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
2020-05-27T17:44:32.238852+00:00 app[web.1]: [2020-05-27T17:44:32.238Z] debug ⛔️ Server wasn't able to start properly.
2020-05-27T17:44:32.253150+00:00 app[web.1]: [2020-05-27T17:44:32.253Z] error Error connecting to the Mongo database. Server selection timed out after 30000 ms

我的环境设置:

database.js

** server.js **

** response.js **

** 配置变量 **

站点在本地主机上运行良好,具有开发和生产环境。所以它连接到 Atlas 上的 MongoDB 并没有问题。

我没有在 Heroku 上安装任何插件。

** 包.json **

在 Atlas 方面,我在白名单中打开了所有 IP。

有什么想法吗?谢谢! :)

【问题讨论】:

  • 您解决了这个问题吗?我目前遇到同样的问题,无法解决。
  • 尝试更改 Server.js ---> 端口 443 使其工作。 module.exports = ({ env }) => ({ host: env('HOST', '0.0.0.0'), port: env.int('PORT', 443), });如果它不起作用,我可以给你我的配置。
  • 感谢您的回复,但没有运气。如果您能分享您的配置将不胜感激,因为我非常困惑为什么这不起作用。我的配置都在my own stack question
  • 我现在得走了。我可以在周末回答吗? :)
  • 太棒了,非常感谢!周末愉快:)

标签: mongodb heroku strapi heroku-cli


【解决方案1】:

我在 Heroku 上部署 Strapi 3.0.1 的配置,用于开发和生产环境:

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connections: {
    default: {
      connector: "mongoose",
      settings: {
        uri: env("DATABASE_URI"),
        ssl: { rejectUnauthorized: false }
      },
      options: {
        ssl: true,
        authenticationDatabase: "",
        useUnifiedTopology: true,
        pool: {
          min: 0,
          max: 10,
          idleTimeoutMillis: 30000,
          createTimeoutMillis: 30000,
          acquireTimeoutMillis: 30000
        }
      },
    },
  },
});

server.js

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 443), <-- 443 was critical to make it work on production
});

.env - 本地文件

DATABASE_URI="mongodb+srv://OdegXXXXXUser:OXXXX20@odXXXXcluster-h9o2e.mongodb.net/odeXXXXXndb?retryWrites=true&w=majority"
HOST="0.0.0.0"
PORT="1337"

Heroku 上的变量:

DATABASE_URI 与本地主机上的相同,相同的数据库。

我希望它对任何人都有帮助:-)

【讨论】:

    【解决方案2】:

    将此作为您的 database.json

    {
      "defaultConnection": "default",
      "connections": {
        "default": {
          "connector": "mongoose",
          "settings": {
            "client": "mongo",
            "host": "${process.env.DATABASE_HOST}",
            "port": "${process.env.DATABASE_PORT}",
            "database": "${process.env.DATABASE_NAME}",
            "username": "${process.env.DATABASE_USERNAME}",
            "password": "${process.env.DATABASE_PASSWORD}",
          },
          "options": {}
        }
      }
    }
    

    还要确保将 package-lock.json 添加到您的 git.ignore 文件中。 然后做

    git add .
    git commit -m "(message) "
    git push heroku master
    

    然后

    heroku open 也启动您的应用服务器

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 2011-04-11
      • 2017-02-11
      • 2021-06-07
      相关资源
      最近更新 更多