Hi There,
Im a new bee to mongoDB. I have created an Ops Manager and onboarded 3 node replica set on it. Im able to connect to DB from GCP VM using single server. However when im trying to connect to replica set it gives below error.
Unable to reach primary for set rs0
Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set
are you able to connect to one of your instances directly to check the replica set configuration?
you can connect to one directly by doing:
mongo --host <primary-node-ip>:27017
you can do that by doing this:
rs.status()
rs.conf()
i made the assumption that you’re on the default port, (you’ll also need to add in the authentication to that command like username and password)
1 Like
Hi Daniel,
Yes, it works when tried connecting using one of the instances directly
Awesome then the next thing is to check network connectivity from your GCP vm to all replica set members. you can do that by using something like telnet:
telnet <node1-ip> 27017
telnet <node1-ip> 27017
telnet <node1-ip> 27017
from that i think you may see that the issue is that the hostname cannot be resolved from your gcp vm. (this is usually the issue).
from the original command that i posted rs.conf shouldve shown entries like:
host: mongodb1:27017
host: mongodb2:27017
so to fix you can either:
option1: add hostname mappings to /etc/hosts on your GCP vm
<node1-ip> mongodb1
<node2-ip> mongodb2
<node3-ip> mongodb3
option2: reconfigure the replica set with ip addresses
cfg = rs.conf()
cfg.members[0].host = "<node1-ip>:27017"
cfg.members[1].host = "<node2-ip>:27017"
cfg.members[2].host = "<node3-ip>:27017"
rs.reconfig(cfg)
important:
port 27017 should be open between all nodes and the vm:
- gcp vm and all mongo nodes (so your vm can connect)
- all mongo nodes with each other (so they can work together)
1 Like
hope you were able to find a solution! let me know if you need more info
Hi Daniel,
Yes, i was missing a Cloud DNS entry for the host which has caused the issue. After adding DNS entry issue is resolved.
2 Likes
dope! always interested to know. have a great day and welcome the community 
2 Likes