Cannot connect to replica set via mongo compass

Thank you steevej .even iam also facing same issue.

I have the same issue that running replica set within local docker containers while failing to connect with my local Compass outside of the docker containers.

I failed to use Atlas because my company blocked 27017 port for security reasons. To request the other team to whitelist 27017 for Atlas usage is not a option in my situation.

Since Mongo provided a detailed procedure for deploy Mongo Cluster in docker, I would hope Mongo can also provides a detailed procedure that helps out how local Compass (outside of container) connect replica sets within docker container.

1 Like

They did not specify how to connect because there is nothing special to do. The information is already there.

From the link you supplied you created a replica set named myReplicaSet which has 3 nodes, the nodes are named mongo1, mongo2 and mongo3, and they all listen on port 27017. The URI to connect to a replica set is of the form:

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

Just have to replace host1, 
, hostN with your hosts. You may leave the port number since you are using the default one.

Running all instances of your replica set within the same physical host is risky. Because if you loose your machine you loose your data.

I appreciate the connection string page you shared here.

However the same issue repeated as this post-owner posted here. The views of this post has been at 4.3k in this forum, one of the most review posts here, and the same questions repeated over other technical forum too so that seems like a lot of people like me has the same issue even though we followed that connection string format.

I had been read many other posts all over internet like yours, unfortunately none of them works. That’s why I am still hoping Mongo can provide a procedure instruction.

I am doing POC for now so creating cluster inside one local machine is not my current concern for now. But thank you for the help though.

If the following connection string does not work please provide the error you are getting.

mongodb://mongo1,mongo2,mongo3/?replicaSet=myReplicaSet

What base OS are you using?

I appreciate your patient. Still doesn’t work

My OS is:
|Edition|Windows 10 Pro|

|Version |22H2|
|Installed on |‎8/‎8/‎2022|
|OS build|19045.2486|
|Experience | Windows Feature Experience Pack 120.2212.4190.0|

So far I had failed trying on the connection strings:

mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=myReplicaSet&tls=false

mongodb://mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=myReplicaSet&tls=false

mongodb://mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=myReplicaSet&readPreference=primary&appname=MongoDB%20Compass&directConnection=false&ssl=false

mongodb://172.20.0.1:27017,172.20.0.2:27018,172.20.0.3:27019/?replicaSet=myReplicaSet&readPreference=primary&directConnection=false&ssl=false

The above docker container IP addresses acquired by command

docker inspect container_id (9f64ba814343)

And more or less the /options here and there where I prefer not to exhaust all of my trials. The Compass always returns me error:

getaddrinfo ENOTFOUND mongo1/2/3

Note: I followed the Mongo instruction so we have common ground here at least.

This is not what I proposed. The replica set is not configured as as localhost. So you are specifying the wrong host. From the link you supplied the replica set is:

docker exec -it mongo1 mongosh --eval "rs.initiate({
 _id: \"myReplicaSet\",
 members: [
   {_id: 0, host: \"mongo1\"},
   {_id: 1, host: \"mongo2\"},
   {_id: 2, host: \"mongo3\"}
 ]
})"

This is again not what I proposed. I mentioned

and

So you are mentioning the wrong ports.

As already mentioned you are specifying the wrong ports.

I have mentioned that your replica set, if configured like the link you shared is configured with

So with 127.0.0.1 you are mentioning the wrong hosts.

The conclusion is that you have tried many things. The one thing you have not tried is the one I have mentioned:

Since you have tried many things except the one I shared I am not too sure that you

because you do not follow the only suggestion I offer. Please share the output of

docker ps 
and
docker exec -it mongo1 mongosh --eval "rs.status()"

Hopefully, they match what is documented in the link you shared.

Share the screenshot of the connection string that generated the Compass error

I had the same issue as the OP and it has been brought up many times here and on SF.

In digging in to my situation I was running a 3 node replicaset with docker-compose in docker for Mac and trying to connect with Compass. Was getting the never ending ENOTFOUND <dns_service_name>.

When adding all 3 localhost entries to Compass with different ports I noticed in the replicaset logs that Compass actually connected to all 3 nodes using localhost but then changed the connection to dns_service_name and disconnected, giving the ENOTFOUND error. The replicasets connect to each other using that dns_service_name. I tried modifying the replicaset init to reflect localhost and also 127.0.0.1 and then they couldnt connect to each other.

Perhaps there is a way to create a new replicaset init file that enables localhost in Compass but dns_service_name between replicasets? IDK

I ended up adding all 3 dns_service_name entries to my etc/hosts file on my MacOS dev host.

127.0.0.1 rs101
127.0.0.1 rs102
127.0.0.1 rs103

Then had to flush the dns with this
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

After all of that I connected with this in Compass:

mongodb://rs101:27017,rs102:28017,rs103:29017/?replicaSet=rs&ssl=false&authMechanism=DEFAULT&serverSelectionTimeoutMS=2000

There are other ways to do this such as adding a Nginx Reverse Proxy to the Docker-Compose file but this was quicker.

Hope this helps someone save a lot of wasted hours debugging that ENOTFOUND error.

1 Like

I used a similar technique to connect Compass to a remote server that had 3 instances of Mongo running as a replicated set with docker-compose. In my case, I modified /etc/hosts file to add the remote IP of the server hosting the docker containers as aliases for each named instance of mongo1, mongo2, and mongo3

± % more /etc/hosts                                                                                                  !5622
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
192.168.1.141   mongo1
192.168.1.141   mongo2
192.168.1.141   mongo3

mongodb://root:example@mongo1,mongo2,mongo3/?replicaSet=rs0 this solved my problem with username and password provided

I also had the similar issue when I was trying to run replica set using the docker compose, I was not able to connect to the database,

This worked out for me, I added this into the /etc/hosts file

127.0.0.1       mongo1
127.0.0.1       mongo2
127.0.0.1       mongo3

then connected using
mongodb://root:example@mongo1,mongo2,mongo3/?replicaSet=rs0

The work around that doesn’t need anyone changing their hosts file is to ask devs to modify their connection string for their DB managing software of preference to use the following string:

mongodb://localhost:27017/?replicaSet=local&directConnection=true

Really strange how the flag lets the db actually reply to the connection request as it was listening correctly on the given port. Anyway, much better than fiddling with the hosts file.

Asking devs to edit their /etc/hosts file on their local machine is quite an embarrassing “solution”.

What is the actual solution to this problem from MongoDB¼ that doesn’t rely on hacks? It seems mongo ignores the --bind_ip_all flag when running with a replica set.