2

I created a strapi website using yarn create strapi-app velankannimatha and chose the mongo as the database. I created a DB in MongoDB atlas called velankannimathadb.

I pushed the site to Heroku after setting the config vars (i.e. DATABASE_URI and DATABASE_NAME).

But the application keeps crashing. When I checked the logs it says

Server wasn't able to start properly. Error connecting to the Mongo database. Server selection timed out after 30000 ms

error log

I am able to access DB locally (in development mode). It only fails when running from Heroku. Any help is much appreciated

Community
  • 1
  • 1

3 Answers3

0

This is likely due to Mongo Atlas only allowing connections from specific IP addresses. See 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 helpful as well.

jcragun
  • 2,060
  • 10
  • 8
0

You need to change the config/server.js host key from localhost to 0.0.0.0 as Strapi is trying to bind to localhost and Heroku is looking for an exposed server to "proxy"

The previous answer also is likely that your Mongo cluster does need to allow access on 0.0.0.0/0 as heroku dynos change over time and use different IPs each time.

Derrick Mehaffy
  • 805
  • 4
  • 5
  • `// config/server.js module.exports = ({ env }) => ({ host: env('HOST', '0.0.0.0'), port: env.int('PORT', 1337), });` This is how it is now –  Jun 10 '20 at 12:13
0

It's working now. What went wrong was that I was using MongoDB version 2.2.12 or later (as per documentation). When I switched to version 3.6 or later and used the updated connection string, everything worked.

Also, adding the connection string as config vars from terminal causes issues, even if it gets saved, some characters are missing. So I added them from the browser.

This article on medium helped a lot too.