|
| 1 | +.. _java-connection-troubleshooting: |
| 2 | + |
| 3 | +========================== |
| 4 | +Connection Troubleshooting |
| 5 | +========================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: code example, disconnected, deployment |
| 19 | + |
| 20 | +.. sharedinclude:: dbx/connection-troubleshooting.rst |
| 21 | + |
| 22 | + .. replacement:: non-connection-issue-callout |
| 23 | + |
| 24 | + .. note:: |
| 25 | + |
| 26 | + This page addresses only connection issues. If you encounter other |
| 27 | + issues when using MongoDB or the driver, visit the following resources: |
| 28 | + |
| 29 | + - The :ref:`Issues & Help <java-issues-and-help>` page for |
| 30 | + information about reporting bugs, contributing to the driver, and |
| 31 | + finding more resources |
| 32 | + - The `MongoDB Community Forums <{+communityForums+}>`__ for |
| 33 | + questions, discussions, or general technical support |
| 34 | + - The :ref:`Frequently Asked Questions (FAQ) <java-faq>` page for |
| 35 | + answers to common questions about the {+driver-short+} |
| 36 | + |
| 37 | + .. replacement:: server-selection-timeout-error |
| 38 | + |
| 39 | + .. code-block:: none |
| 40 | + :copyable: false |
| 41 | + |
| 42 | + Error: couldn't connect to server 127.0.0.1:27017 |
| 43 | + |
| 44 | + .. replacement:: check-connection-string-anchor |
| 45 | + |
| 46 | + .. _java-connection-string-port: |
| 47 | + |
| 48 | + .. replacement:: multiple-hosts-connection-guide-link |
| 49 | + |
| 50 | + To learn how to specify multiple replica set hosts, see the |
| 51 | + :ref:`Connect to a Replica Set <connect-replica-set>` section of the |
| 52 | + Connection Guide. |
| 53 | + |
| 54 | + .. replacement:: configure-firewall-anchor |
| 55 | + |
| 56 | + .. _java-connection-firewall: |
| 57 | + |
| 58 | + .. replacement:: authentication-error-anchor |
| 59 | + |
| 60 | + .. _java-authentication-error: |
| 61 | + |
| 62 | + .. replacement:: scram-failure-error |
| 63 | + |
| 64 | + .. code-block:: none |
| 65 | + :copyable: false |
| 66 | + |
| 67 | + Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. |
| 68 | + |
| 69 | + .. replacement:: check-credentials-formatting-anchor |
| 70 | + |
| 71 | + .. _java-connection-string-auth: |
| 72 | + |
| 73 | + .. replacement:: learn-more-connection-string-admonition |
| 74 | + |
| 75 | + .. tip:: |
| 76 | + |
| 77 | + For more information about using connection strings, |
| 78 | + see :ref:`Connection URI <connection-uri>` in the Connection Guide. |
| 79 | + |
| 80 | + .. replacement:: percent-encode-example |
| 81 | + |
| 82 | + .. replacement:: verify-authentication-database-anchor |
| 83 | + |
| 84 | + .. _java-verify-auth-db: |
| 85 | + |
| 86 | + .. replacement:: authsource-param-code-block |
| 87 | + |
| 88 | + .. code-block:: java |
| 89 | + :copyable: false |
| 90 | + |
| 91 | + String uri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=users&authMechanism=SCRAM-SHA-256"; |
| 92 | + MongoClient mongoClient = MongoClients.create(uri); |
| 93 | + |
| 94 | + .. replacement:: credentials-provider-alternative-method-description |
| 95 | + |
| 96 | + If you construct a client by using a ``MongoCredential``, the builder method |
| 97 | + corresponds to the authentication mechanism. The following code shows the builder |
| 98 | + method for the ``SCRAM-SHA-256`` authentication mechanism: |
| 99 | + |
| 100 | + .. code-block:: java |
| 101 | + :copyable: false |
| 102 | + |
| 103 | + MongoCredential credential = MongoCredential.createScramSha256Credential("<db_username>", "<authenticationDb>", "<db_password>"); |
| 104 | + |
| 105 | + |
| 106 | + .. replacement:: authentication-guide-reference |
| 107 | + |
| 108 | + To learn more about specifying authentication mechanisms, see the :ref:`authentication-mechanisms` and |
| 109 | + :ref:`enterprise-authentication-mechanisms` guides. |
| 110 | + |
| 111 | + .. replacement:: verify-authentication-mechanism-anchor |
| 112 | + |
| 113 | + .. _java-verify-auth-mechanism: |
| 114 | + |
| 115 | + .. replacement:: authsource-param-code-block |
| 116 | + |
| 117 | + .. code-block:: java |
| 118 | + :copyable: false |
| 119 | + |
| 120 | + MongoClient mongoClient = MongoClients.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=users"); |
| 121 | + |
| 122 | + .. replacement:: dns-resolution-anchor |
| 123 | + |
| 124 | + .. _java-dns-resolution-error: |
| 125 | + |
| 126 | + .. replacement:: dns-error-message |
| 127 | + |
| 128 | + .. code-block:: none |
| 129 | + :copyable: false |
| 130 | + |
| 131 | + com.mongodb.MongoSocketWriteException: Exception sending message |
| 132 | + |
| 133 | + .. replacement:: check-the-number-of-connections-anchor |
| 134 | + |
| 135 | + .. _java-connection-num-connections: |
| 136 | + |
| 137 | + .. replacement:: mongo-client-class |
| 138 | + |
| 139 | + ``MongoClient`` |
| 140 | + |
| 141 | + .. replacement:: max-pool-size-param |
| 142 | + |
| 143 | + ``maxPoolSize`` |
| 144 | + |
| 145 | + .. replacement:: max-pool-size-default |
| 146 | + |
| 147 | + ``100`` |
| 148 | + |
| 149 | + .. replacement:: max-idle-time-param |
| 150 | + |
| 151 | + ``maxIdleTimeMS`` |
| 152 | + |
| 153 | + .. replacement:: connection-pools-learn-more |
| 154 | + |
| 155 | + To learn more about how connection pooling works in the driver, see |
| 156 | + :ref:`java-faq-connection-pool` in the FAQ. |
| 157 | + |
| 158 | + .. replacement:: scram-failure-error |
| 159 | + |
| 160 | + .. code-block:: |
| 161 | + |
| 162 | + Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. |
| 163 | + |
| 164 | + .. replacement:: check-credentials-formatting-anchor |
| 165 | + |
| 166 | + .. _java-troubleshooting-connection-string-auth: |
| 167 | + |
| 168 | +.. _java-connection-certificate: |
| 169 | + |
| 170 | +Security Certificate Errors |
| 171 | +--------------------------- |
| 172 | + |
| 173 | +If you use Java version 8 or earlier, you might need to add a |
| 174 | +certificate to your trust store. You can either upgrade to a later |
| 175 | +version of the JDK or read the :atlas:`Security FAQ |
| 176 | +</reference/faq/security/#java-users>` instructions in the Atlas |
| 177 | +documentation for information on how to add the certificate. |
| 178 | + |
| 179 | +.. _java-timeout-errors: |
| 180 | + |
| 181 | +Timeout Errors |
| 182 | +-------------- |
| 183 | + |
| 184 | +When you send messages through the driver to the server, sometimes the |
| 185 | +messages take a while to respond. When this happens, you might receive an error |
| 186 | +message similar to one of the following messages: |
| 187 | + |
| 188 | +.. code-block:: none |
| 189 | + :copyable: false |
| 190 | + |
| 191 | + Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. |
| 192 | + |
| 193 | +.. code-block:: none |
| 194 | + :copyable: false |
| 195 | + |
| 196 | + No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description |
| 197 | + |
| 198 | +If you receive one of these errors, try the following methods to resolve the |
| 199 | +issue. |
| 200 | + |
| 201 | +Set ``maxConnectionTimeoutMS`` |
| 202 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 203 | + |
| 204 | +The ``maxConnectionTimeoutMS`` option indicates the time the Java driver waits |
| 205 | +for a connection before timing out. The default value is ``10000``. You can |
| 206 | +increase this value or set it to ``0`` if you want the driver to never timeout. |
| 207 | + |
| 208 | +Set ``maxConnectionLifeTime`` and ``maxConnectionIdleTime`` |
| 209 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 210 | + |
| 211 | +Consider setting ``maxConnectionLifeTime`` and |
| 212 | +``maxConnectionIdleTime``. These parameters configure how long the driver |
| 213 | +maintains a connection to a MongoDB instance. For more information about these |
| 214 | +parameters, see :ref:`Connection Pool Settings <mcs-connectionpool-settings>`. |
| 215 | + |
| 216 | +.. _java-server-selection-errors: |
| 217 | + |
| 218 | +Server Selection Timeout Exceptions |
| 219 | +----------------------------------- |
| 220 | + |
| 221 | +Your application might not be able to complete a request even when some servers |
| 222 | +are available, causing the driver to return a server selection timeout |
| 223 | +exception. |
| 224 | + |
| 225 | +This exception is of type ``MongoTimeoutException``. The following |
| 226 | +shows a sample of the exception that occurs if you attempt to send a |
| 227 | +request to a replica set in which the primary is not reachable: |
| 228 | + |
| 229 | +.. code-block:: none |
| 230 | + |
| 231 | + com.mongodb.MongoTimeoutException: |
| 232 | + Timed out while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. |
| 233 | + Client view of cluster state is |
| 234 | + {type=REPLICA_SET, |
| 235 | + servers=[ |
| 236 | + {address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}, |
| 237 | + {address=localhost:27018, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}, |
| 238 | + {address=localhost:27019, type=REPLICA_SET_SECONDARY, roundTripTime=15.0 ms, state=CONNECTED} |
| 239 | + ] |
| 240 | + } |
| 241 | + |
| 242 | +The error includes a view of the cluster state that describes the |
| 243 | +connection state of each node, which can help you identify the source of |
| 244 | +your connection issue. |
| 245 | + |
| 246 | +In the preceding error, the only connected server, ``localhost:27019``, |
| 247 | +is a secondary node. Because of this, the request times out as the |
| 248 | +driver is unable to select a server that satisfies the read preference |
| 249 | +of ``primary``. In this situation, you can still perform read operations |
| 250 | +against the connected secondary node if you set the read preference to |
| 251 | +``secondary``, ``secondaryPreferred``, or ``nearest``. |
| 252 | + |
| 253 | +You can also specify the ``serverSelectionTimeoutMS`` connection option |
| 254 | +to adjust the amount of time in which the driver must select a server. To |
| 255 | +learn more, see the :ref:`java-connection-options` guide. |
| 256 | + |
| 257 | +.. _java-miscellaneous-errors: |
| 258 | + |
| 259 | +Miscellaneous Errors |
| 260 | +-------------------- |
| 261 | + |
| 262 | +This section shows connection errors that do not fall into a broader category. |
| 263 | + |
| 264 | +Monitor Thread Exception |
| 265 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 266 | + |
| 267 | +.. code-block:: none |
| 268 | + :copyable: false |
| 269 | + |
| 270 | + INFO: Exception in monitor thread while connecting to server ssc-cluster-01-shard-00-02.9cbnp.mongodb.net:27017 |
| 271 | + |
| 272 | +To resolve this error, you must install certificates as described under |
| 273 | +:ref:`Security Certificate Errors <java-connection-certificate>`. |
| 274 | + |
| 275 | +Certificate Request Exception |
| 276 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 277 | + |
| 278 | +.. code-block:: none |
| 279 | + :copyable: false |
| 280 | + |
| 281 | + javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request |
| 282 | + |
| 283 | +This is a known issue in certain versions of the JDK that can occur when |
| 284 | +attempting to connect by using the TLS 1.3 protocol. |
| 285 | + |
| 286 | +If you encounter this error when connecting to your MongoDB instance or |
| 287 | +cluster, update your JDK to one of the following patch versions or newer: |
| 288 | + |
| 289 | +- JDK 11.0.7 |
| 290 | +- JDK 13.0.3 |
| 291 | +- JDK 14.0.2 |
| 292 | + |
| 293 | +To learn more about this issue, see the |
| 294 | +`issue description <https://bugs.openjdk.java.net/browse/JDK-8236039>`__ |
| 295 | +in the OpenJDK Bug system tracker issue. |
| 296 | + |
| 297 | +.. _java-debugging-tips: |
| 298 | + |
| 299 | +Debugging Tips |
| 300 | +-------------- |
| 301 | + |
| 302 | +While not related to a specific error message, this section includes |
| 303 | +information that can help in the process of troubleshooting connection |
| 304 | +issues. |
| 305 | + |
| 306 | +Verbose Logging for TLS/SSL |
| 307 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 308 | + |
| 309 | +You can use the ``-Djavax.net.debug=all`` system property to enable |
| 310 | +debug-level logging related to all connections, including those |
| 311 | +established by using TLS/SSL. |
| 312 | + |
| 313 | +Enabling debug-level logging can help you diagnose the root problem of |
| 314 | +connection issues. To learn more about the TLS/SSL logging messages, |
| 315 | +see the `Debugging SSL/TLS Connections <https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/ReadDebug.html>`__ |
| 316 | +Java documentation. |
0 commit comments