Cannot connect to Mongodb Server without adding into Host Files

Hello all,
I have configured 3-node replica set cluster for a pre-prod environment. Replica set is configured to receive connection from all ips. But when I try to connect to the replica set using IP address without adding nodes to the host file, I am unable to connect. But after adding ips to the host file the connection can be established. What is the reason for this?

Hello @Ravindra_Pandey ,

To get better understanding of your use-case, could you please share below mentioned details?

  • MongoDB Version
  • How did you configure the replica set?

  • How are you trying to connect to the host? are you using specific url? can you share the connection url? (redact the sensitive information like username and password before sharing)

  • Why do you want to use IPs instead of setting up hostnames?

  • How can you confirm that you are not connecting to the Database? Are you facing any error? If yes, please share the error message.

  • Please share output of rs.conf() and rs.status().

I would recommend you go through below thread as a similar has been discussed in that post.


1 Like

You will find some useful information in the following post.


Hi @Tarun_Gaur
Thank you for your response!
I am curretnly trying to connect to the replica set through mongodb compass remotely. The main issue is I am able to connect to the replica set when I add the ips and hostname to the host file but if I remove them from the host fine I am unable to connect to the replica set. My replica set are running without any issues. I have configured relica set with the host names.
The error is:

But after adding files to host files the connection can be established using IP as well as hostname.
Can’t we make connection with ip only and not adding to the host file?

The connection string is: