Can't connect remote replica set by Public IP

I’ve setup MongoDB replica set on a single machine with 3 different port 27017, 27018, and 27019.

MongoDB connection works fine When I’m connecting from the same machine with the private IP(IPV4).

But, I’m getting an error when I’m trying to access the replica set remotely.

I’m getting below error when I’m trying to connect from the robo3t.

Cannot connect to replica set “Mongo Server”[IP_ADDRESS:27017].
Set’s primary is unreachable.
No member of the set is reachable. Reason: Connect failed

I’ve tried with the robo3t and programming with below connection string.


Note: I can able to connect the standalone MongoDB with the public IP.

Thanks in advance.

When you connect to a replicaset the first node reached will seed the replicaset connection by retreiving data from the replicaset. Usually this is hostnames. Those hostnames need to be resolvable by each node any client that is accessing the replica set.

For a one off you can create entries in your clients hosts files as a workaround. Or ssh tunnel.

If it is a deployment that will be connectible externally then you may need to update what names you are using in the replicaset configuration.


Thanks for your reply but can you please explain this with example and in detail for better understanding?

Hi @Akash_Patel

What @chris meant is that all the replica set nodes addresses must be resolvable by every node in the set and all external clients (see connectivity for examples & recommendations).

Typically it’s recommended to use hostnames instead of IP addresses, so if the IP changes, you don’t have to reconfigure the replica set.

See Deploy a replica set for details and examples, and check out Troubleshoot Replica Sets for tips.

Finally, it is strongly recommended to not expose any database server to the public internet without a thorough security checks and precautions, or if you don’t really need to (e.g. IP whitelisting and enabling auth are practically the bare minimum requirement). This is true for any database servers, not only MongoDB. Please see the Security Checklist.

Best regards,

1 Like

Thanks @kevinadi

i also want to keep mongodb server private for better security.

But if we want to access database securely from GUI application (from specific system) then how can we do that without exposing database IP to public?

Is it possible to connect?

Hi @Akash_Patel

But if we want to access database securely from GUI application (from specific system) then how can we do that without exposing database IP to public?

You may be able to use IP whitelisting to restrict access to the server to certain IP addresses. Having said that, note that although whitelisting is one solution, it’s also best to secure the server using all the security options available in MongoDB as well (see the security checklist).

Best regards,

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