Reputation: 1403
I've doing a POC on mongo replica sets in a local containerized environment. I have been able to create a simple mongodb replica set and initialize it. The three nodes are running on the default port (27017) internally and are mapped/exposed using ports 35000, 35001, and 35002. The container names are mongo0, mongo1, and mongo2 respectively. The replica set is named 'rst'. I have added entries in my hosts file to map names mongo(0..2) to 127.0.0.1.
When I initate the replica set, I get an OK response and I am able to connect to each node with Compass using direct connect option (directConnection=true) 3500N port. e.g.: mongodb://mongo0:35000/&directConnection=true
However, when I attempt to connect using mongodb://mongo0:35000/&replicaSet=rst?
, I am receiving the following error:
connect ECONNREFUSED 127.0.0.1:27017
I also tried: mongodb://mongo0:35000,mongo1:35001,mongo2:35002?replicaSet=rst
with the same result.
I should note that from within one of the nodes, this connection string works to connect to mongosh: mongosh mongodb://mongo0,mongo1,mongo2?replicaSet=rst
.
I know why it's trying to connect to 127.0.0.1 (hosts mapping) but I don't understand why it's trying to connect on port 27017 given that I specified a different port in the connection string. Is there some other connection option? An explanation for this behavior would be greatly appreciated.
Upvotes: 0
Views: 1025
Reputation: 1403
I see that @Joe has already answered this at least a couple of times. For anyone who comes across while troubleshooting a similar issue, here are those links:
I also found this which offers some solutions:
For resolving the port number discrepancy, the simple solution is to use the non-standard port in the mongo container. That is, start it on whatever port will be mapped to it when exposing the port.
For managing the host names, I like the solution in the last question above of using etc/hosts
inside the container to create an alias(es) that matches the DNS name(s) that the client will see. Then that hostname can be used for bind_ip and to initialize the replicaSet.
Upvotes: 0