Dotnet driver - can't connect to replica set

Hi all, wondering if anybody can help diagnose an issue connecting to a replica set using the .NET driver.

Environment
1 x Ubuntu Server running MongoDB (3.6.2) replica set on ports 27001/27002/27003.
1 x Windows Server running app using MongoDB dotnet driver (2.12.2)

Both are dedicated servers running on a private network

Issue
The windows app can connect directly to the primary (27002) using the following connection string, this works fine and allows reads and writes without problem.

mongodb://username:password@ubuntu_host:27002/db_to_connect_to

I tried the following connection string combinations to connect to the replica set:

mongodb://username:password@ubuntu_host:27001,ubuntu_host:27002,ubuntu_host:27003/db_to_connect_to?replicaSet=replica_set_name

mongodb://username:password@ubuntu_host:27001,ubuntu_host:27002,ubuntu_host:27003/?authSource=db_to_connect_to&replicaSet=replica_set_name

Also tried using the IP address for the Ubuntu host instead of the domain name, but get the same error.

Error

System.TimeoutException
A timeout occurred after 30000 ms selecting a server using CompositeServerSelector {
    Selectors = MongoDB.Driver.MongoClient + AreSessionsSupportedServerSelector, LatencyLimitingServerSelector {
        AllowedLatencyRange = 00: 00: 00.0150000
    }
}.
Client view of cluster state is {
        ClusterId: "1",
        ConnectionMode: "ReplicaSet",
        Type: "ReplicaSet",
        State: "Disconnected",
        Servers: [{
                    ServerId: "{ ClusterId : 1, EndPoint : "
                    127.0 .0 .1: 27001 " }",
                    EndPoint: "127.0.0.1:27001",
                    ReasonChanged: "Heartbeat",
                    State: "Disconnected",
                    ServerVersion: ,
                    TopologyVersion: ,
                    Type: "Unknown",
                    HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: 
                        No connection could be made because the target machine actively refused it 127.0.0.1:27001 at ...

The no connection error makes sense as it is trying to connect to 127.0.0.1 and on the Windows server this would refuse the connection, but why is the driver telling it to use the locahost instead of the host provided in the connection string.

Any thoughts?

Thanks,
James

Thank you for your question James,

Could you please

  1. Share the replica set configuration (output from rs.conf()). Please reduct any private information.
  2. Make sure that the replica set instances are bound to non-local addresses.

Thanks

2 Likes

Hi Boris,

Thanks so much for pointing me in the right direction. The replica set members have localahost entries…

Can I just check my understanding, so the driver uses the connection string to establish a connection based on any available host in the connection string, then once connected the server reports back the replica configuration that is used to establish the link?

I can’t change the replica set configuration at the moment as it will need to be scheduled during down time but presumably by updating the config following this tutorial https://docs.mongodb.com/manual/tutorial/change-hostnames-in-a-replica-set/
it should then fix the issue.

Replica set configuration output

    "members" : [
    		{
    			"_id" : 0,
    			"host" : "127.0.0.1:27001",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 1,
    			"tags" : {
    				
    			},
    			"slaveDelay" : 0,
    			"votes" : 1
    		},
    		{
    			"_id" : 1,
    			"host" : "127.0.0.1:27002",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 1,
    			"tags" : {
    				
    			},
    			"slaveDelay" : 0,
    			"votes" : 1
    		},
    		{
    			"_id" : 2,
    			"host" : "127.0.0.1:27003",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 1,
    			"tags" : {
    				
    			},
    			"slaveDelay" : 0,
    			"votes" : 1
    		}
    	],

Thanks again,
James

Hi James,
Yes, driver uses the reported configuration for further connection establishments and updates. Changing the host addresses to non-local ones should resolve the issue.

Thanks!

1 Like

Hi Borris, thanks for your help and explaining how it works.

1 Like

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