Search code examples
mongodbherokustrapiheroku-cli

Connection to Mongodb fails from Strapi on Heroku


I deployed Strapi CMS to Heroku, but I get this error

Error connecting to the Mongo database. Server selection timed out after 30000 ms

Log:

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

My environments settings:

database.js

enter image description here

** server.js **

enter image description here

** response.js **

enter image description here

** config vars **

enter image description here

Site works well on localhost with both dev and prod environment. So it connects to MongoDB on Atlas and no problem with that.

I do not have any addons installed on Heroku.

** packages.json **

enter image description here

On Atlas side I opened all IPs in white list.

Any idea? Thank you! :)


Solution

  • My configuration for deployment of Strapi 3.0.1 on Heroku, both for develop and production environments:

    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 - local file

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

    Vars on Heroku:

    enter image description here

    DATABASE_URI er identical as on localhost, the same database.

    I hope it will help to anybody :-)