Access mongodb via External IP

I have a replication set which is configured using private IPs, and we are able to access inside the VPC. All are fine,

But when I tried to access via the Public IP of the replica set like node1_ip,node2_ip,node_ip/?replicaSet=dev-mongo-cluster then it is not working. There is network level issues(the port is opened to our IP address)

But If I try to access a single node using Public IP without mentioning the replica set then it is working.
Any idea how to resolve this?

The IP addresses used to configure a replica set must be accessible from all the members of the replica set and all the clients connecting to the replica set.

When a client connect to a replica set it reads the replica set configuration and establish connections to all members.

When a client connect directly to a single node, the replica set configuration is not considered.

1 Like

Hi @Bhuvanesh_R ,

As @steevej mentioned, with a replica set connection clients use the hostnames listed in the replica set configuration per the Server Discovery and Monitoring (SDAM) specification implemented by official MongoDB drivers.

The rationale for this behaviour is to support failover and zero-downtime reconfiguration, which are key features of a replica set connection. Establishing a connection to a single replica set member (aka a “direct connection”) does not include failover or discovery of the replica set configuration, so will work if you are port forwarding via a different inbound hostname/IP/port combination.

If you want to establish a remote connection to a replica set hosted on a private network and take advantage of failover and automatic reconfiguration, the most straightforward approach would be to have your clients connect using via the VPC or a VPN.

Regards,
Stennie

2 Likes

Thanks for the 2 links.

1 Like

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