Every node in the replica set must be reachable from every other node and from the client. This means:
- Each node and the client must be able to resolve the hostname of every other node. Answer
- Each node and the client must be able to connect to every other node. and here
The hostnames specified in the replica set configuration take precedence over hostnames specified in the URI/when the client is instantiated. Meaning, the client must be able to reach all of the replica set nodes using the hostnames in the replica set configuration.
A replica set node generally cannot be behind NAT (network address translation). For example, it is generally not possible to have a replica set with one node in a data center (or otherwise somewhere on the internet) and another node on one's home network.
- Get the connection working with
mongo
shell before trying to connect through your application/using a driver. - Review the server logs (
mongod.log
etc.). Depending on the failure they may have important cues.
This section applies when you are running mongod
yourself.
- If your database and your application are not on the same machine,
ensure
bindIp
is appropriately set. - If you are connecting to a replica set, both the hostnames/IP addresses that are specified in the seed list AND the hostnames/IP addresses that are specified in replica set configuration must be resolvable and accessible from the client. The client uses the seed list to discover nodes that are part of the replica set, from that point forward the client uses hostnames that are provided in the replica set configuration and not the seed addresses.
- Get a standalone connection working before troubleshooting connection to a replica set.
- Get the connectivity working when you are running the database locally prior to trying to run the database on a remote server/in the cloud.
- Ensure whitelist is configured with your IP.
- Try 0.0.0.0 as the whitelist.
- If your application is hosted on the internet, you need to whitelist the application's IP address. Heroku IP addresses
- Follow general connectivity troubleshooting
Unlike drivers, mongo
shell does not wait for deployment to become available
and will immediately exit with an error if the deployment is starting up.
DNS resolution of TXT records appears to be broken on your machine. You can use the non-SRV URI to connect to your Atlas deployment instead.
Drivers establish connections to the database in the background. A successful client object instantiation does not mean the client successfully connected to the deployment.
If you are asking a connectivity-related question on Stack Overflow, collect and include in your question all of the following information:
- The server version you are connecting to.
- The
mongo
shell version (can differ from server version), if you are asking about connectivity via the shell. - The MongoDB driver you are using and driver version, if you are asking about connectivity via the driver
- The exact and complete error message you have received.
- Any relevant information from the server log(s).
Transactions require:
- For replica set deployments: MongoDB 4.0+
- For sharded cluster deployments: MongoDB 4.2+
- WiredTiger storage engine
Not supported:
- Standalone servers (of any version)
- Deployments using MMAPv1 storage engine
What doesn't work how? Add to the question:
- What you have done
- What the results were
You are asking a question about how MongoDB behaves but you have not referenced official documentation describing the behavior you are asking about.
Please link to the official documentation from your question that describes the behavior that you are asking about.
Your question may have different answers depending on which version of MongoDB, the driver, etc. you are using. Please include versions of all components you are asking about in your question.
Some of the components are:
- Server
mongo
shell- Driver