Hi, I have setup a proxies to connect Mongodb cluster endpoints. The mapping is something like this
localhost:12345 —> mongodb-cluster-endpoint-1:27017
localhost:12346 —> mongodb-cluster-endpoint-2:27017
I am using PySpark and the URI is
Mongodb driver resolves the canonical addresses and instead of using localhost; it start using mongodb-cluster-endpoint-1:27017 as endpoint which is not reachable and thus connection fails.
Is there any way where I can make mongodb stop to discover canonical address or restrict it removing localhost from client view of cluster?
Hi @Muhammad_Imran_Tariq welcome to the community!
Even though you’re using PySpark, this address resolution is uniform across all MongoDB drivers, so you should see a similar issue when connecting with any driver.
The main issue is when you’re connecting to a replica set, when a driver connects to one of the nodes, it will grab the content of rs.conf() and try to connect to all the nodes in the replica set using the addresses defined in the config.
localhost won’t work in this case since the driver will auto-discover all the nodes addresses from the config instead of using what you have given it.
The drivers connects in this manner to provide high availability. That is, if the primary is down, the driver can monitor the set’s election and connect to the new primary quickly. However this process does require that the driver can connect to the canonical names as defined in the replica set config.
The recommended solution is to ensure that
mongodb-cluster-endpoint is solvable from your client side using DNS. However if this is not practical or possible, perhaps you can modify your
/etc/hosts to map
mongodb-cluster-endpoint addresses to