skip to Main Content

I have a weird issue while trying to connect to a remote Mongo replica set.

This is my setup:

  • on a remote machine I have three instances of MongoDB running on different ports, each belonging to the same replocal replica Set.
  • the remote MongoDB is configured to bindIp 172.31.32.81,127.0.0.1 (both external and internal IPs)

From my local machine (running MacOS Ventura) i can connect to a single instance, but not to the cluster.

When I try to connect to the cluster (either via mongosh, or via Mongo Compass or even Studio 3T) I have the same error: it seems I am using a wrong syntax and the client tries to connect always to localhost, where of course I have no MongoDB running.

Here is a sample:

connecting to single instance, primary

$ mongosh "mongodb://172.31.32.81:27017"        
Current Mongosh Log ID: 649b09bca15bc473c95db139
Connecting to:          mongodb://172.31.32.81:27017/?directConnection=true&appName=mongosh+1.10.1
Using MongoDB:          6.0.6
Using Mongosh:          1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   ...(edited out , not relevant)...
------

replocal [direct: secondary] test> 

as you can see I have the expected prompt from the remote MongoDB

connect to replica set

$ mongosh "mongodb://172.31.32.81:27017,172.31.32.81:27018,172.31.32.81:27019?replicaSet=replocal&readPreference=secondaryPreferred"   
Current Mongosh Log ID: 649b096a40719e3e693048bd
Connecting to:          mongodb://172.31.32.81:27017,172.31.32.81:27018,172.31.32.81:27019/?replicaSet=replocal&readPreference=secondaryPreferred&appName=mongosh+1.10.1
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

Why is it trying to connect to localhost (aka 127.0.0.1)?

Now comes the interesting part: if I change the port of the first server to something other than 27017 I got of course an error, since no service is listening on that port, but the error is related to the correct IP, and not 127.0.0.1:

$ mongosh "mongodb://172.31.32.81:27016,172.31.32.81:27018,172.31.32.81:27019?replicaSet=replocal&readPreference=secondaryPreferred"  
Current Mongosh Log ID: 649b0d86045a207db47e25f2
Connecting to:          mongodb://172.31.32.81:27016,172.31.32.81:27018,172.31.32.81:27019/?replicaSet=replocal&readPreference=secondaryPreferred&appName=mongosh+1.10.1
MongoNetworkError: connect ECONNREFUSED 172.31.32.81:27016

Am I using the wrong connection string?
It seems that the porto 27017 always assumes 127.0.0.1, disregarding the specified IP address.
I have tried with hostnames, the result is the same.

Thanks for any help,
best regards

2

Answers


  1. When connecting to a replica set, the driver:

    • uses the host in the connection string as a seed to make an initial connection.
    • runs the isMaster or hello command on that initial connection to get the full list of host:port replica set members and their current status
    • drops the initial connections
    • connects to each of the members discovered in step #2
    • during operations, automatically monitors all of the members, sending operations to the primary even if a different node becomes primary

    MongoDB drivers use unified topology by default, which permits the driver to automatically detect if it is connecting to a standalone instance, replica set, or sharded cluster.

    In your situation it would appear that the nodes were added to the replica set using "localhost" or a hostname that resolves to localhost, so when the client receives the list of host:ports contained in the replica set configuration, they are all different ports on localhost.

    To resolve this situation you will need to reconfigure the replica set with hostnames that the client machine can resolve to the proper IP address.

    Login or Signup to reply.
  2. Running all ReplicaSet members on the same host does not make much sense, unless for testing and development.

    As far as I remember, there is an internal limitation in MongoDB. If you run several members on the same host then all members must be configured as localhost:<port>.

    I would suggest to use bindIp: localhost or bindIpAll: true depending on your requirements, instead of bindIp: 172.31.32.81,127.0.0.1.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search