I have a weird issue while trying to connect to a remote Mongo replica set.
This is my setup:
replocal
replica Set.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
When connecting to a replica set, the driver:
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.