[JAVA-4344] server hostname is always 127.0.0.1 if MongoClient is initialized by uri "mongodb://user:pass@hostname:port/db?replicaSet=rs0" Created: 12/Oct/21  Updated: 27/Oct/23  Resolved: 23/Oct/21

Status: Closed
Project: Java Driver
Component/s: Monitoring
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Pavol Slany Assignee: Ross Lawley
Resolution: Works as Designed Votes: 0
Labels: external-user
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

If connectionUri contains replicaSet, client bad resolved server hosts.

Follow simple code:

    String uri = "mongodb://user:password@182.18.4.3:27017/dbname?replicaSet=rs0";
    MongoClient client = MongoClients.create(uri);
    MongoDatabase dbTest = client.getDatabase("test");
    System.out.println(dbTest.listCollectionNames().first());

.. throws exception (look address: address=127.0.0.1:27019):

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@747f281. Client view of cluster state is {type=REPLICA_SET, servers=[{address=127.0.0.1:27019, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}, {address=127.0.0.1:27018, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}, {address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]
	at com.mongodb.internal.connection.BaseCluster.createTimeoutException(BaseCluster.java:405)
	at com.mongodb.internal.connection.BaseCluster.selectServer(BaseCluster.java:118)
	at com.mongodb.internal.connection.AbstractMultiServerCluster.selectServer(AbstractMultiServerCluster.java:50)
	at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:139)
	at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:276)
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:176)
	at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135)
	at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
	at com.mongodb.client.internal.MongoIterableImpl.first(MongoIterableImpl.java:103)
	at com.mongodb.client.internal.MappingIterable.first(MappingIterable.java:50)
	at test.main(test.java:11)



 Comments   
Comment by Pavol Slany [ 25/Oct/21 ]

Dear Ross Lawley,

thank you very much for your really useful information. It helped me to understand, why host and port was different. My test was created in docker image and addresses in image and hosted computer are diferrent.

Sorry for this ticket.

Best regards

Comment by Ross Lawley [ 12/Oct/21 ]

HI pavol.slany@annotation.sk

Thank you for reaching out. As this sounds like a support issue, I wanted to give you some resources to get this question answered more quickly in future:

  • Our MongoDB support portal, located at support.mongodb.com
  • Our MongoDB community portal, located here
  • If you are an Atlas customer, there is free support offered 24/7 in the lower right hand corner of the UI

Just in case you have already opened a support case and are not receiving sufficient help, please let me know and I can facilitate escalating your issue.

The driver monitors replica set changes top provide resilience and allow for targeted reads. To do that the driver uses the replica set configuration as the source of truth. So in this case, its connecting using the host in your MongoDB connection string, then it is using the replica set configuration information so that it can monitor and connect to the other replica set nodes. I believe your replica set nodes have been configured to use local ips rather than an ip that is accessible to your application.

In short the hostname name in your replica set configuration must be resolvable for every host in the replica set as well as to the application / MongoDB Java Driver.

I hope that helps,

Ross Lawley

Generated at Thu Feb 08 09:01:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.