Docs Menu

Docs HomeJava Sync Driver

Connection Troubleshooting

On this page

  • Server Connection Errors
  • Check the Connection String
  • Configure Firewall
  • Check the Number of Connections
  • Authentication Errors
  • Check the Credentials Formatting
  • Verify the Authentication Mechanism
  • Verify User Is in Authentication Database
  • DNS Resolution Errors
  • Check Database Deployment Availability
  • Check the Network Addresses
  • Security Certificate Errors
  • Timeout Errors
  • Set maxConnectionTimeoutMS
  • Set maxConnectionLifeTime and maxConnectionIdleTime
  • Miscellaneous Errors
  • Monitor Thread Exception
  • Certificate Request Exception
  • Debugging Tips
  • Verbose Logging for TLS/SSL

This page offers potential solutions to issues that you might encounter when using the MongoDB Java Driver to connect to a MongoDB deployment.



When an issue occurs when you attempt to connect to a server, the Java driver returns an error message. If this error resembles the following message, it indicates that the driver cannot connect to a MongoDB deployment:

Error: couldn't connect to server

The following sections describe methods that might help resolve the issue.

Verify that the hostname and port number in the connection string are both accurate. In the sample error message, the hostname is and the port is 27017. The default port value for an instance of MongoDB Server is 27017, but you can configure MongoDB to listen on another port.

When connecting to a replica set, include all the replica set hosts in your connection string. Separate each of the hosts in the connection string with a comma. This enables the driver to establish a connection if one of the hosts is unreachable.

To learn how to specify multiple replica set hosts, see the Connect to a Replica Set section of the Connection Guide.

If your MongoDB deployment is hosted behind a firewall, ensure the port on which MongoDB listens is open in the firewall. If your deployment listens on the default network port, ensure that port 27017 is open in the firewall. If your deployment listens on a different port, ensure that port is open on the firewall.


Do not open a firewall port unless you are sure that it is the one that your MongoDB deployment listens on.

Each MongoClient instance supports a maximum number of concurrent open connections in its connection pool. The configuration parameter maxPoolSize defines this value and is set to 100 by default. If the number of open connections is equal to maxPoolSize, the server waits until a connection becomes available. If this wait time exceeds the maxIdleTimeMS value, the driver responds with an error.

To learn more about how connection pooling works in the driver, see How Does Connection Pooling Work in the Java Driver? in the FAQ.

The Java driver may be unable connect to a MongoDB deployment if the authorization is not configured correctly. In these cases, the driver raises an error message similar to the following message:

Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017.

The following sections describe methods that may help resolve the issue.

One of the most common causes of authentication issues is invalid credentials formatting in the MongoDB connection string.


For more information about using connection strings, see Connection URI in the Connection Guide.

If your connection string contains a username and password, ensure that they are correctly formatted.


If your username or password includes any of the following characters, you must percent-encode it:

: / ? # [ ] @

Use your percent-encoded username and password in your connection string.

Ensure that your credentials and authentication mechanism are correct. You can specify your authentication credentials in the options of your connection string.

If you construct a client by using a MongoCredential, the builder method corresponds to the authentication mechanism. The following code shows the builder method for the SCRAM-SHA-256 authentication mechanism:

MongoCredential credential = MongoCredential.createScramSha256Credential("<username>", "<authenticationDb>", "<password>");

To learn more about specifying authentication mechanisms, see the Authentication Mechanisms and Enterprise Authentication Mechanisms guides.

When using a username and password-based authentication method, the username must be defined in the authentication database.

The default authentication database is the admin database. To use a different database for authentication, specify the authSource option in the connection string.

The following example instructs MongoDB to use the users database as the authentication database:

MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=users");

The Java driver may be unable to resolve your DNS connection. When this happens, you might receive an error message similar to the following message:

com.mongodb.MongoSocketWriteException: Exception sending message

If the driver reports this error, try the methods in the following sections to resolve the issue.

If you are connecting to MongoDB Atlas and your driver cannot find the DNS host of the Atlas database deployment, the database deployment might be paused or deleted.

Ensure that the database deployment exists in Atlas. If the cluster is paused, you can resume the cluster in the Atlas UI or the Atlas command line interface.

To learn how to resume a cluster, see Resume One Cluster in the Atlas documentation.

Verify that the network addresses or hostnames in your connection string are accurate.

If your deployment is hosted on MongoDB Atlas, you can follow the Connect to Your Cluster tutorial to find your Atlas connection string.

If you use Java version 8 or earlier, you might need to add a certificate to your trust store. You can either upgrade to a later version of the JDK or read the Security FAQ instructions in the Atlas documentation for information on how to add the certificate.

When you send messages through the driver to the server, sometimes the messages take a while to respond. When this happens, you might receive an error message similar to one of the following messages:

Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}.
No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description

If you receive one of these errors, try the following methods to resolve the issue.

The maxConnectionTimeoutMS option indicates the time the Java driver waits for a connection before timing out. The default value is 10000. You can increase this value or set it to 0 if you want the driver to never timeout.

Consider setting maxConnectionLifeTime and maxConnectionIdleTime. These parameters configure how long the driver maintains a connection to a MongoDB instance. For more information about these parameters, see Connection Pool Settings.

This section shows connection errors that do not fall into a broader category.

INFO: Exception in monitor thread while connecting to server

To resolve this error, you must install certificates as described under Security Certificate Errors. extension (5) should not be presented in certificate_request

This is a known issue in certain versions of the JDK that can occur when attempting to connect by using the TLS 1.3 protocol.

If you encounter this error when connecting to your MongoDB instance or cluster, update your JDK to one of the following patch versions or newer:

  • JDK 11.0.7

  • JDK 13.0.3

  • JDK 14.0.2

To learn more about this issue, see the issue description in the OpenJDK Bug system tracker issue.

While not related to a specific error message, this section includes information that can help in the process of troubleshooting connection issues.

You can use the system property to enable debug-level logging related to all connections, including those established by using TLS/SSL.

Enabling debug-level logging can help you diagnose the root problem of connection issues. To learn more about the TLS/SSL logging messages, see the Debugging SSL/TLS Connections Java documentation.

←  FAQIssues & Help →