Getting ECONNREFUSED and ReplicaSetNoPrimary errors

Hey guys,
So I have a free cluster on MongoDB Atlas for my node.js app. When I was developing the app, the connection to the database worked perfectly fine. Now I have deployed this app on a server and I get an error to connect to the remote db:
MongoServerSelectionError: connect ECONNREFUSED
and

reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    ...
}

So after some researchs, I decided to add the server’s ip address to the database’s white list, but still getting the same error.

So I’m waiting for some solutions, thanks in advance guys!

Hey @Ewan_Humbert - Firstly, welcome to the community :slight_smile:

So I have a free cluster on MongoDB Atlas for my node.js app. When I was developing the app, the connection to the database worked perfectly fine. Now I have deployed this app on a server and I get an error to connect to the remote db

Regarding the above, could you advise on the following:

  1. If the exact same code is being used (at least for the connection portion)
  2. When the connection initially worked, was the app hosted locally on your device? E.g. on your laptop and connection was working fine to Atlas
  3. When you get the errors connecting from the server where the app is deployed, have you tested connection from the initial development machine to compare if it was working during the same period?
  4. Further details regarding the environment where the app fails to connect. E.g. Is this now hosted on AWS?
  5. Driver version

I would also recommend you to please try performing the initial basic network connectivity tests and provide the output for the cluster you are having trouble connecting to (from the server where you have deployed your app):

  1. ping one of the hosts (prefereably the PRIMARY):
/// example
ping cluster0-shard-00-00.ze4xc.mongodb.net
  1. telnet to one of the hosts on port 27017 :
/// example
telnet cluster0-shard-00-00.ze4cx.mongodb.net 27017

Note: You can find the hostname in the metrics page of your cluster

Additionally, I would recommend to review the Troubleshoot Connection Issues documentation. You may also find the following blog post regarding tips for atlas connectivity useful too.

Regards,
Jason

4 Likes

Hey @Jason_Tran
Thank you so much for your answer!

I finally fixed the problem by authorizing the server to connect with the cluster, that’s pretty much it lol.
For those who face the same issue, and have access to the cPanel, just go to “SSH authorization”, and permit the cluster’s IP addresses that you find in your error message. And that’s pretty much it.

1 Like

Thanks for posting your fix here and i’m glad to hear that you got it working! :slight_smile: It’s interesting to also know that this was deployed via cPanel.

Regards,
Jason

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.