|
| 1 | +.. _java-connection-troubleshooting: |
| 2 | + |
| 3 | +================================ |
| 4 | +Connection Troubleshooting Guide |
| 5 | +================================ |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +This page offers potential solutions to issues you might see when |
| 16 | +connecting to a MongoDB instance or replica set while using the |
| 17 | +{+driver-long+}. |
| 18 | + |
| 19 | +.. note:: |
| 20 | + |
| 21 | + This page lists only connection issues. If you are having any other issues |
| 22 | + with MongoDB, consider the following resources: |
| 23 | + |
| 24 | + - The :ref:`Frequently Asked Questions (FAQ) <java-faq>` for the Java driver |
| 25 | + - The :ref:`Issues & Help <java-issues-and-help>` topic for information about |
| 26 | + reporting bugs, contributing to the driver, and additional resources |
| 27 | + - The `MongoDB Community Forums <https://community.mongodb.com>`__ for |
| 28 | + questions, discussions, or general technical support |
| 29 | + |
| 30 | +Connection Error |
| 31 | +~~~~~~~~~~~~~~~~ |
| 32 | + |
| 33 | +The following error message is a general message indicating that the driver |
| 34 | +cannot connect to a server on the specified hostname or port: |
| 35 | + |
| 36 | +.. code-block:: none |
| 37 | + :copyable: false |
| 38 | + |
| 39 | + Error: couldn't connect to server 127.0.0.1:27017 |
| 40 | + |
| 41 | +If you receive this error, try the following methods to resolve the issue. |
| 42 | + |
| 43 | +.. _java-connection-string-port: |
| 44 | + |
| 45 | +Check Connection String |
| 46 | +----------------------- |
| 47 | + |
| 48 | +Verify that the hostname and port number in the connection string are both |
| 49 | +accurate. In the sample error message, the hostname is ``127.0.0.1`` and the |
| 50 | +port is ``27017``. The default port value for a MongoDB instance is |
| 51 | +``27017``, but you can configure MongoDB to communicate on another port. |
| 52 | + |
| 53 | +.. _java-connection-firewall: |
| 54 | + |
| 55 | +Configure Firewall |
| 56 | +------------------ |
| 57 | + |
| 58 | +Assuming that your MongoDB deployment uses the default port, verify that your |
| 59 | +firewall has port ``27017`` open. If your deployment is using a different port, |
| 60 | +verify that port is open in your firewall. |
| 61 | + |
| 62 | +.. important:: |
| 63 | + |
| 64 | + Do not open ports in your firewall unless you are sure that is the port used |
| 65 | + by your MongoDB instance. |
| 66 | + |
| 67 | +Authentication Error |
| 68 | +~~~~~~~~~~~~~~~~~~~~ |
| 69 | + |
| 70 | +The {+driver-short+} can fail to connect to a MongoDB instance if |
| 71 | +the authorization is not configured correctly. This often results in an error |
| 72 | +message similar to the following: |
| 73 | + |
| 74 | +.. code-block:: none |
| 75 | + :copyable: false |
| 76 | + |
| 77 | + Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. |
| 78 | + |
| 79 | +If you receive this error, try the following methods to resolve the issue. |
| 80 | + |
| 81 | +.. _java-connection-string-auth: |
| 82 | + |
| 83 | +Check Connection String |
| 84 | +----------------------- |
| 85 | + |
| 86 | +An invalid connection string is the most common cause of authentication |
| 87 | +issues when attempting to connect to MongoDB. |
| 88 | + |
| 89 | +.. note:: |
| 90 | + |
| 91 | + For more information about using connection strings with the Java driver, |
| 92 | + see :ref:`Connection URI <connection-uri>` in the Connection Guide. |
| 93 | + |
| 94 | +If your connection string contains a username and password, ensure that they |
| 95 | +are in the correct format. |
| 96 | + |
| 97 | +.. note:: |
| 98 | + |
| 99 | + If the username or password includes any of the following characters, they |
| 100 | + must be `percent encoded <https://tools.ietf.org/html/rfc3986#section-2.1>`__: |
| 101 | + |
| 102 | + .. code-block:: none |
| 103 | + |
| 104 | + : / ? # [ ] @ |
| 105 | + |
| 106 | + |
| 107 | +If your MongoDB deployment is on MongoDB Atlas, you can check your connection |
| 108 | +string by using the :ref:`Connect to MongoDB Atlas <connect-atlas-java-driver>` |
| 109 | +code example. Make sure to replace the connection string in the example. |
| 110 | + |
| 111 | +When connecting to a replica set, you should include all of the hosts |
| 112 | +in the replica set in your connection string. Separate each of the hosts |
| 113 | +in the connection string with a comma. This enables the driver to establish a |
| 114 | +connection if one of the hosts is unreachable. |
| 115 | + |
| 116 | +.. _java-connection-admin: |
| 117 | + |
| 118 | +Verify User Is in Authentication Database |
| 119 | +----------------------------------------- |
| 120 | + |
| 121 | +To successfully authenticate a connection by using a username and password, |
| 122 | +the username must be defined in the authentication database. The default |
| 123 | +authentication database is the ``admin`` database. To use a different database |
| 124 | +for authentication, specify the ``authSource`` in the connection string. The |
| 125 | +following example instructs the driver to use ``users`` as the authentication |
| 126 | +database: |
| 127 | + |
| 128 | +.. code-block:: java |
| 129 | + :copyable: false |
| 130 | + |
| 131 | + MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=users"); |
| 132 | + |
| 133 | +Error Sending Message |
| 134 | +~~~~~~~~~~~~~~~~~~~~~ |
| 135 | + |
| 136 | +When you end a request through the driver and it is unable to send the command, |
| 137 | +it often displays the following general error message: |
| 138 | + |
| 139 | +.. code-block:: none |
| 140 | + :copyable: false |
| 141 | + |
| 142 | + com.mongodb.MongoSocketWriteException: Exception sending message |
| 143 | + |
| 144 | +If you receive this error, try the following methods to resolve the issue. |
| 145 | + |
| 146 | +Check Connection String |
| 147 | +----------------------- |
| 148 | + |
| 149 | +Verify that the connection string in |
| 150 | +your app is accurate. This is described under :ref:`Connection Error <java-connection-string-port>` |
| 151 | +and :ref:`Authentication Error <java-connection-string-auth>`. |
| 152 | + |
| 153 | +Verify User Is in Authentication Database |
| 154 | +----------------------------------------- |
| 155 | + |
| 156 | +The user needs to be recognized in your |
| 157 | +authentication database. This is described under :ref:`Authentication |
| 158 | +Error <java-connection-admin>`. |
| 159 | + |
| 160 | +Configure Firewall |
| 161 | +------------------ |
| 162 | + |
| 163 | +The firewall needs to have an open port for communicating with the MongoDB |
| 164 | +instance. This is described under :ref:`Connection Error <java-connection-firewall>`. |
| 165 | + |
| 166 | +.. _java-connection-number-connections: |
| 167 | + |
| 168 | +Check the Number of Connections |
| 169 | +------------------------------- |
| 170 | + |
| 171 | +Each ``MongoClient`` instance supports a maximum number of concurrent open |
| 172 | +connections in its connection pool. The configuration parameter ``maxPoolSize`` |
| 173 | +defines this value and is set to ``100`` by default. If there are already a |
| 174 | +number of open connections equal to ``maxPoolSize``, the server waits until |
| 175 | +a connection becomes available. If this wait time exceeds the ``maxIdleTimeMS`` |
| 176 | +value, the driver responds with an error. |
| 177 | + |
| 178 | +.. _java-connection-certificate: |
| 179 | + |
| 180 | +Install Certificate |
| 181 | +------------------- |
| 182 | + |
| 183 | +If you are using Java version 8 or earlier, you might need to manually add a |
| 184 | +certificate to your trust store. You can either upgrade to a later version of |
| 185 | +the JDK or see our `Security FAQ |
| 186 | +<https://www.mongodb.com/docs/atlas/reference/faq/security/#java-users>`__ for |
| 187 | +information about how to add the certificate. |
| 188 | + |
| 189 | +Timeout Error |
| 190 | +~~~~~~~~~~~~~ |
| 191 | + |
| 192 | +Sometimes when you send messages through the driver to the server, the messages |
| 193 | +take a while to respond. When this happens, you might receive an error message |
| 194 | +similar to one of the following error messages: |
| 195 | + |
| 196 | +.. code-block:: none |
| 197 | + :copyable: false |
| 198 | + |
| 199 | + Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. |
| 200 | + |
| 201 | +.. code-block:: none |
| 202 | + :copyable: false |
| 203 | + |
| 204 | + No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description |
| 205 | + |
| 206 | +If you receive one of these errors, try the following methods to resolve the |
| 207 | +issue. |
| 208 | + |
| 209 | +Set ``maxConnectionTimeoutMS`` |
| 210 | +------------------------------ |
| 211 | + |
| 212 | +The ``maxConnectionTimeoutMS`` option indicates the amount of time the Java |
| 213 | +driver waits for a connection |
| 214 | +before timing out. The default value is ``10000``. You can increase this value |
| 215 | +or set it to ``0`` if you want the driver to never timeout. |
| 216 | + |
| 217 | +Set ``maxConnectionLifeTime`` and ``maxConnectionIdleTime`` |
| 218 | +----------------------------------------------------------- |
| 219 | + |
| 220 | +Consider setting ``maxConnectionLifeTime`` and |
| 221 | +``maxConnectionIdleTime``. These parameters configure how long a connection |
| 222 | +can be maintained with a MongoDB instance. For more information about these |
| 223 | +parameters, see :ref:`Connection Pool Settings <mcs-connectionpool-settings>`. |
| 224 | + |
| 225 | +Check the Number of Connections |
| 226 | +------------------------------- |
| 227 | + |
| 228 | +You might have too many open connections. The solution to this is described |
| 229 | +under :ref:`Error Sending Message <java-connection-number-connections>`. |
| 230 | + |
| 231 | +Install Certificate |
| 232 | +------------------- |
| 233 | + |
| 234 | +If you are using an older version of Java, you might need to manually install |
| 235 | +some certificates as described under |
| 236 | +:ref:`Error Sending Message <java-connection-certificate>`. |
| 237 | + |
| 238 | +Miscellaneous Errors |
| 239 | +~~~~~~~~~~~~~~~~~~~~ |
| 240 | + |
| 241 | +This section shows connection errors that do not fall into a broader category. |
| 242 | +Each section lists the error message and then the specific steps you should |
| 243 | +take to resolve it. |
| 244 | + |
| 245 | +Monitor Thread Exception |
| 246 | +------------------------ |
| 247 | + |
| 248 | +.. code-block:: none |
| 249 | + :copyable: false |
| 250 | + |
| 251 | + INFO: Exception in monitor thread while connecting to server ssc-cluster-01-shard-00-02.9cbnp.mongodb.net:27017 |
| 252 | + |
| 253 | +To resolve this error, you need to manually install certificates as described |
| 254 | +under :ref:`Error Sending Message <java-connection-certificate>`. |
| 255 | + |
| 256 | +Certificate Request Exception |
| 257 | +----------------------------- |
| 258 | + |
| 259 | +.. code-block:: none |
| 260 | + :copyable: false |
| 261 | + |
| 262 | + javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request |
| 263 | + |
| 264 | +This is a `known error <https://bugs.openjdk.java.net/browse/JDK-8236039>`__ |
| 265 | +that can occur when using the TLS 1.3 protocol with specific versions of the |
| 266 | +JDK. If you encounter this error when connecting to your MongoDB instance or |
| 267 | +cluster, update your JDK to one of the following patch versions or newer: |
| 268 | + |
| 269 | +- JDK 11.0.7 |
| 270 | +- JDK 13.0.3 |
| 271 | +- JDK 14.0.2 |
| 272 | + |
| 273 | +Additional Tips |
| 274 | +~~~~~~~~~~~~~~~ |
| 275 | + |
| 276 | +While not related to a specific error message, this section includes |
| 277 | +additional information that can be useful when attempting to troubleshoot |
| 278 | +connection issues. |
| 279 | + |
| 280 | +Get Log Information for TLS/SSL |
| 281 | +------------------------------- |
| 282 | + |
| 283 | +When using TLS/SSL, you can use the ``-Djavax.net.debug=all`` system property |
| 284 | +to view additional log statements. This can help when attempting to debug any |
| 285 | +connection issues. See `the Oracle guide to debugging TLS/SSL connections |
| 286 | +<https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/ReadDebug.html>`__ |
| 287 | +for more information. |
0 commit comments