|
| 1 | +.. _kotlin-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 | +This page offers potential solutions to issues you might see when |
| 14 | +connecting to a MongoDB instance or replica set while using the |
| 15 | +{+driver-long+}. |
| 16 | + |
| 17 | +.. note:: |
| 18 | + |
| 19 | + This page lists only connection issues. If you are having any other issues |
| 20 | + with MongoDB, consider the following resources: |
| 21 | + |
| 22 | + - The :ref:`Frequently Asked Questions (FAQ) <kotlin-faq>` for the {+driver-short+} |
| 23 | + - The :ref:`Issues & Help <kotlin-issues-and-help>` topic for information about |
| 24 | + reporting bugs, contributing to the driver, and additional resources |
| 25 | + - The `MongoDB Community Forums <https://community.mongodb.com>`__ for |
| 26 | + questions, discussions, or general technical support |
| 27 | + |
| 28 | +Connection Error |
| 29 | +~~~~~~~~~~~~~~~~ |
| 30 | + |
| 31 | +The following error message is a general message indicating that the driver |
| 32 | +cannot connect to a server on the specified hostname or port: |
| 33 | + |
| 34 | +.. code-block:: none |
| 35 | + :copyable: false |
| 36 | + |
| 37 | + Error: couldn't connect to server 127.0.0.1:27017 |
| 38 | + |
| 39 | +If you receive this error, try the following methods to resolve the issue. |
| 40 | + |
| 41 | +.. _kotlin-connection-string-port: |
| 42 | + |
| 43 | +Check Connection String |
| 44 | +----------------------- |
| 45 | + |
| 46 | +Verify that the hostname and port number in the connection string are both |
| 47 | +accurate. In the sample error message, the hostname is ``127.0.0.1`` and the |
| 48 | +port is ``27017``. The default port value for a MongoDB instance is |
| 49 | +``27017``, but you can configure MongoDB to communicate on another port. |
| 50 | + |
| 51 | +.. _kotlin-connection-firewall: |
| 52 | + |
| 53 | +Configure Firewall |
| 54 | +------------------ |
| 55 | + |
| 56 | +Assuming that your MongoDB deployment uses the default port, verify that your |
| 57 | +firewall has port ``27017`` open. If your deployment is using a different port, |
| 58 | +verify that port is open in your firewall. |
| 59 | + |
| 60 | +.. important:: |
| 61 | + |
| 62 | + Do not open ports in your firewall unless you are sure that is the port used |
| 63 | + by your MongoDB instance. |
| 64 | + |
| 65 | +Authentication Error |
| 66 | +~~~~~~~~~~~~~~~~~~~~ |
| 67 | + |
| 68 | +The {+driver-short+} can fail to connect to a MongoDB instance if |
| 69 | +the authorization is not configured correctly. This often results in an error |
| 70 | +message similar to the following: |
| 71 | + |
| 72 | +.. code-block:: none |
| 73 | + :copyable: false |
| 74 | + |
| 75 | + Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. |
| 76 | + |
| 77 | +If you receive this error, try the following methods to resolve the issue. |
| 78 | + |
| 79 | +.. _kotlin-connection-string-auth: |
| 80 | + |
| 81 | +Check Connection String |
| 82 | +----------------------- |
| 83 | + |
| 84 | +An invalid connection string is the most common cause of authentication |
| 85 | +issues when attempting to connect to MongoDB. |
| 86 | + |
| 87 | +.. note:: |
| 88 | + |
| 89 | + For more information about using connection strings with the {+driver-short+}, |
| 90 | + see :ref:`Connection URI <connection-uri>` in the Connection Guide. |
| 91 | + |
| 92 | +If your connection string contains a username and password, ensure that they |
| 93 | +are in the correct format. |
| 94 | + |
| 95 | +.. note:: |
| 96 | + |
| 97 | + If the username or password includes any of the following characters, they |
| 98 | + must be `percent encoded <https://tools.ietf.org/html/rfc3986#section-2.1>`__: |
| 99 | + |
| 100 | + .. code-block:: none |
| 101 | + |
| 102 | + : / ? # [ ] @ |
| 103 | + |
| 104 | + |
| 105 | +If your MongoDB deployment is on MongoDB Atlas, you can check your connection |
| 106 | +string by using the :ref:`Atlas Connection Example <connect-atlas-kotlin-driver>`. |
| 107 | +Make sure to replace the connection string in the example with yours. |
| 108 | + |
| 109 | +When connecting to a replica set, you should include all of the hosts |
| 110 | +in the replica set in your connection string. Separate each of the hosts |
| 111 | +in the connection string with a comma. This enables the driver to establish a |
| 112 | +connection if one of the hosts is unreachable. |
| 113 | + |
| 114 | +.. _kotlin-connection-admin: |
| 115 | + |
| 116 | +Verify User Is in Authentication Database |
| 117 | +----------------------------------------- |
| 118 | + |
| 119 | +To successfully authenticate a connection by using a username and password, |
| 120 | +the username must be defined in the authentication database. The default |
| 121 | +authentication database is the ``admin`` database. To use a different database |
| 122 | +for authentication, specify the ``authSource`` in the connection string. The |
| 123 | +following example instructs the driver to use ``users`` as the authentication |
| 124 | +database: |
| 125 | + |
| 126 | +.. code-block:: kotlin |
| 127 | + :copyable: false |
| 128 | + |
| 129 | + val mongoClient = |
| 130 | + MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=users") |
| 131 | + |
| 132 | +.. _kotlin-error-sending-message: |
| 133 | + |
| 134 | +Error Sending Message |
| 135 | +~~~~~~~~~~~~~~~~~~~~~ |
| 136 | + |
| 137 | +When you send a request through the driver and it is unable to send the command, |
| 138 | +it often displays the following general error message: |
| 139 | + |
| 140 | +.. code-block:: none |
| 141 | + :copyable: false |
| 142 | + |
| 143 | + com.mongodb.MongoSocketWriteException: Exception sending message |
| 144 | + |
| 145 | +If you receive this error, try the following methods to resolve the issue. |
| 146 | + |
| 147 | +Check Connection String |
| 148 | +----------------------- |
| 149 | + |
| 150 | +Verify that the connection string in |
| 151 | +your app is accurate. This is described under :ref:`Connection Error <kotlin-connection-string-port>` |
| 152 | +and :ref:`Authentication Error <kotlin-connection-string-auth>`. |
| 153 | + |
| 154 | +Verify User Is in Authentication Database |
| 155 | +----------------------------------------- |
| 156 | + |
| 157 | +The user needs to be recognized in your |
| 158 | +authentication database. This is described under :ref:`Authentication |
| 159 | +Error <kotlin-connection-admin>`. |
| 160 | + |
| 161 | +Configure Firewall |
| 162 | +------------------ |
| 163 | + |
| 164 | +The firewall needs to have an open port for communicating with the MongoDB |
| 165 | +instance. This is described under :ref:`Connection Error <kotlin-connection-firewall>`. |
| 166 | + |
| 167 | +.. _kotlin-connection-number-connections: |
| 168 | + |
| 169 | +Check the Number of Connections |
| 170 | +------------------------------- |
| 171 | + |
| 172 | +Each ``MongoClient`` instance supports a maximum number of concurrent open |
| 173 | +connections in its connection pool. The configuration parameter ``maxPoolSize`` |
| 174 | +defines this value and is set to ``100`` by default. If there are already a |
| 175 | +number of open connections equal to ``maxPoolSize``, the server waits until |
| 176 | +a connection becomes available. If this wait time exceeds the ``maxIdleTimeMS`` |
| 177 | +value, the driver responds with an error. |
| 178 | + |
| 179 | +Timeout Error |
| 180 | +~~~~~~~~~~~~~ |
| 181 | + |
| 182 | +Sometimes when you send messages through the driver to the server, the messages |
| 183 | +take a while to respond. When this happens, you might receive an error message |
| 184 | +similar to one of the following error messages: |
| 185 | + |
| 186 | +.. code-block:: none |
| 187 | + :copyable: false |
| 188 | + |
| 189 | + Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. |
| 190 | + |
| 191 | +.. code-block:: none |
| 192 | + :copyable: false |
| 193 | + |
| 194 | + No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description |
| 195 | + |
| 196 | +If you receive one of these errors, try the following methods to resolve the |
| 197 | +issue. |
| 198 | + |
| 199 | +Set ``maxConnectionTimeoutMS`` |
| 200 | +------------------------------ |
| 201 | + |
| 202 | +The ``maxConnectionTimeoutMS`` option indicates the amount of time the |
| 203 | +{+driver-short+} waits for a connection before timing out. The default |
| 204 | +value is ``10000``. You can increase this value or set it to ``0`` if |
| 205 | +you want the driver to never timeout. |
| 206 | + |
| 207 | +Set ``maxConnectionLifeTime`` and ``maxConnectionIdleTime`` |
| 208 | +----------------------------------------------------------- |
| 209 | + |
| 210 | +Consider setting ``maxConnectionLifeTime`` and |
| 211 | +``maxConnectionIdleTime``. These parameters configure how long a connection |
| 212 | +can be maintained with a MongoDB instance. For more information about these |
| 213 | +parameters, see :ref:`Connection Pool Settings <mcs-connectionpool-settings>`. |
| 214 | + |
| 215 | +Check the Number of Connections |
| 216 | +------------------------------- |
| 217 | + |
| 218 | +You might have too many open connections. The solution to this is described |
| 219 | +under :ref:`Error Sending Message <kotlin-error-sending-message>`. |
| 220 | + |
| 221 | +Additional Tips |
| 222 | +~~~~~~~~~~~~~~~ |
| 223 | + |
| 224 | +While not related to a specific error message, this section includes |
| 225 | +additional information that can be useful when attempting to troubleshoot |
| 226 | +connection issues. |
| 227 | + |
| 228 | +Get Log Information for TLS/SSL |
| 229 | +------------------------------- |
| 230 | + |
| 231 | +When using TLS/SSL, you can use the ``-Djavax.net.debug=all`` system property |
| 232 | +to view additional log statements. This can help when attempting to debug any |
| 233 | +connection issues. See `the Oracle guide to debugging TLS/SSL connections |
| 234 | +<https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/ReadDebug.html>`__ |
| 235 | +for more information. |
0 commit comments