Heroku -> db not connecting

MongoServerSelectionError: connect ECONNREFUSED
    at Timeout._onTimeout (D:\Super Bill\SB_API\node_modules\mongodb\lib\sdam\topology.js:330:38)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:27020' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined

means there is no server running at the given address and port number. This or may be you firewall is preventing you from connecting there.

1 Like

Hi Steevej,

In my local nodejs environment working good, I hosted my app in heroku ,
In heroku it is not working , I allowed all IP’s


So you have a mongod running on your heroku host?

If not and you want your app on heroku to talk to your server on your local desktop/server with than you should read about localhost - Wikipedia because there is some knowledge you lack to accomplish this.

Welcome to the MongoDB Community Forums @Mohammadali_Ghassemi !

If your app is deployed on Heroku it will not be able to connect to a database instance on localhost.

You should be using an external hostname for your database connection string, and will need to configure appropriate security measures to limit exposure to your database deployment. Please review the MongoDB Security Checklist – Role-Based Access Control and Encrypted Communication are essential security measures.

For an example using MongoDB Atlas, see How to deploy MongoDB on Heroku.


1 Like

Hello @Stennie ,
thank you for your post,
I am using atlas cluster with encryption , I tested app in local environment with atlas cluster DB connection string ,same app is not working in heroku


If you want to connect to your atlas cluster you must use your atlas cluster connection string.

What you shared is using localhost as the connection string, not your atlas connection string.

@Mohammadali_Ghassemi, any update on this? Did

solve your issue? If so please mark the post as the solution.