|
| 1 | +.. _golang-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 encounter when |
| 14 | +using the {+driver-long+} to connect to a MongoDB deployment. |
| 15 | + |
| 16 | +.. note:: |
| 17 | + |
| 18 | + This page addresses only connection issues. If you encounter any other issues |
| 19 | + with MongoDB or the driver, visit the following resources: |
| 20 | + |
| 21 | + - The :ref:`Frequently Asked Questions (FAQ) <golang-faq>` for the |
| 22 | + {+driver-short+} |
| 23 | + - The :ref:`Issues & Help <golang-issues-and-help>` page, which has |
| 24 | + information about reporting bugs, contributing to the driver, and |
| 25 | + finding additional resources |
| 26 | + - The `MongoDB Community Forums <https://community.mongodb.com>`__ for |
| 27 | + questions, discussions, or general technical support |
| 28 | + |
| 29 | +Connection Error |
| 30 | +~~~~~~~~~~~~~~~~ |
| 31 | + |
| 32 | +The following error message is a general message indicating that the driver |
| 33 | +cannot connect to a server on the specified hostname or port: |
| 34 | + |
| 35 | +.. code-block:: none |
| 36 | + :copyable: false |
| 37 | + |
| 38 | + Error: couldn't connect to server 127.0.0.1:27017 |
| 39 | + |
| 40 | +The following sections describe methods that may help resolve the issue. |
| 41 | + |
| 42 | +.. _golang-troubleshooting-connection-string-port: |
| 43 | + |
| 44 | +Check Connection String |
| 45 | +----------------------- |
| 46 | + |
| 47 | +Verify that the hostname and port number in the connection string are both |
| 48 | +accurate. In the sample error message, the hostname is ``127.0.0.1`` and the |
| 49 | +port is ``27017``. The default port value for a MongoDB instance is |
| 50 | +``27017``, but you can configure MongoDB to communicate on another port. |
| 51 | + |
| 52 | +.. _golang-troubleshooting-connection-firewall: |
| 53 | + |
| 54 | +Configure Firewall |
| 55 | +------------------ |
| 56 | + |
| 57 | +Assuming that your MongoDB deployment uses the default port, verify that port |
| 58 | +``27017`` is open in your firewall. If your deployment uses a different port, |
| 59 | +verify the correct port is open in your firewall. |
| 60 | + |
| 61 | +.. warning:: |
| 62 | + |
| 63 | + Do not open a port in your firewall unless you are sure that it is the port |
| 64 | + used by your MongoDB instance. |
| 65 | + |
| 66 | +Authentication Error |
| 67 | +~~~~~~~~~~~~~~~~~~~~ |
| 68 | + |
| 69 | +The {+driver-short+} can fail to connect to a MongoDB instance if |
| 70 | +the authorization is not configured correctly. In these cases, the driver raises |
| 71 | +an error message similar to one of the following messages: |
| 72 | + |
| 73 | +.. code-block:: none |
| 74 | + :copyable: false |
| 75 | + |
| 76 | + Command failed with error 18 (AuthenticationFailed): 'Authentication |
| 77 | + failed.' on server localhost:27017. |
| 78 | + |
| 79 | +.. code-block:: none |
| 80 | + :copyable: false |
| 81 | + |
| 82 | + connection() error occurred during connection handshake: auth error: |
| 83 | + sasl conversation error: unable to authenticate using mechanism |
| 84 | + "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed. |
| 85 | + |
| 86 | +The following sections describe methods that may help resolve the issue. |
| 87 | + |
| 88 | +.. _golang-troubleshooting-connection-string-auth: |
| 89 | + |
| 90 | +Check Connection String |
| 91 | +----------------------- |
| 92 | + |
| 93 | +An invalid connection string is the most common cause of authentication |
| 94 | +issues when attempting to connect to MongoDB. |
| 95 | + |
| 96 | +.. tip:: |
| 97 | + |
| 98 | + For more information about connection strings, |
| 99 | + see :ref:`Connection URI <golang-connection-uri>` in the Connection Guide. |
| 100 | + |
| 101 | +If your connection string contains a username and password, ensure that they |
| 102 | +are in the correct format. |
| 103 | + |
| 104 | +.. note:: |
| 105 | + |
| 106 | + If the username or password includes any of the following characters, they |
| 107 | + must be `percent encoded <https://tools.ietf.org/html/rfc3986#section-2.1>`__: |
| 108 | + |
| 109 | + .. code-block:: none |
| 110 | + |
| 111 | + : / ? # [ ] @ |
| 112 | + |
| 113 | +When connecting to a replica set, you should include all of the hosts |
| 114 | +in the replica set in your connection string. Separate each of the hosts |
| 115 | +in the connection string with a comma. This enables the driver to establish a |
| 116 | +connection if one of the hosts is unreachable. |
| 117 | + |
| 118 | +.. _golang-troubleshooting-connection-auth-mechanism: |
| 119 | + |
| 120 | +Verify the Authentication Mechanism |
| 121 | +----------------------------------- |
| 122 | + |
| 123 | +Ensure that your credentials and authentication mechanism are correct. You can |
| 124 | +store your authentication credentials in environment variables or you can pass |
| 125 | +them to the ``SetAuth()`` method. |
| 126 | + |
| 127 | +To learn more about authentication, see the |
| 128 | +:ref:`golang-authentication-mechanisms` guide. |
| 129 | + |
| 130 | +.. _golang-troubleshooting-connection-admin: |
| 131 | + |
| 132 | +Verify User Is in Authentication Database |
| 133 | +----------------------------------------- |
| 134 | + |
| 135 | +To successfully authenticate a connection by using a username and password, |
| 136 | +the username must be defined in the authentication database. The default |
| 137 | +authentication database is the ``admin`` database. To use a different database |
| 138 | +for authentication, specify the ``authSource`` in the connection string. The |
| 139 | +following example instructs the driver to use ``users`` as the authentication |
| 140 | +database: |
| 141 | + |
| 142 | +.. code-block:: go |
| 143 | + :copyable: true |
| 144 | + |
| 145 | + uri := "mongodb://<username>:<password>@<hostname>:<port>/?authSource=users" |
| 146 | + client := mongo.Connect(uri) |
| 147 | + |
| 148 | +Error Sending Message |
| 149 | +~~~~~~~~~~~~~~~~~~~~~ |
| 150 | + |
| 151 | +When the driver fails to send a command after you make a request, |
| 152 | +it often displays the following general error message: |
| 153 | + |
| 154 | +.. code-block:: none |
| 155 | + :copyable: false |
| 156 | + |
| 157 | + com.mongodb.MongoSocketWriteException: Exception sending message |
| 158 | + |
| 159 | +The following sections describe methods that may help resolve the issue. |
| 160 | + |
| 161 | +Check Connection String |
| 162 | +----------------------- |
| 163 | + |
| 164 | +Verify that the connection string in your app is accurate. For more information |
| 165 | +about verifying your connection string, see |
| 166 | +:ref:`Connection Error <golang-troubleshooting-connection-string-port>` |
| 167 | +and :ref:`Authentication Error <golang-troubleshooting-connection-string-auth>`. |
| 168 | + |
| 169 | +Verify the Authentication Mechanism |
| 170 | +----------------------------------- |
| 171 | + |
| 172 | +Make sure you are using the correct authentication mechanism and credentials. |
| 173 | +For more information about authentication errors, see |
| 174 | +:ref:`Authentication Error <golang-troubleshooting-connection-auth-mechanism>`. |
| 175 | + |
| 176 | +Verify User Is in Authentication Database |
| 177 | +----------------------------------------- |
| 178 | + |
| 179 | +Verify the user is in the correct authentication database. For more |
| 180 | +information about the authentication database, see |
| 181 | +:ref:`Authentication Error <golang-troubleshooting-connection-admin>`. |
| 182 | + |
| 183 | +Configure Firewall |
| 184 | +------------------ |
| 185 | + |
| 186 | +The firewall needs to have an open port for communicating with the MongoDB |
| 187 | +instance. For more information about configuring the firewall, see |
| 188 | +:ref:`Connection Error <golang-troubleshooting-connection-firewall>`. |
| 189 | + |
| 190 | +.. _golang-troubleshooting-connection-number-connections: |
| 191 | + |
| 192 | +Check the Number of Connections |
| 193 | +------------------------------- |
| 194 | + |
| 195 | +Each ``MongoClient`` instance supports a maximum number of concurrent open |
| 196 | +connections in its connection pool. The configuration parameter ``maxPoolSize`` |
| 197 | +defines this value and is set to ``100`` by default. If there are already a |
| 198 | +number of open connections equal to ``maxPoolSize``, the server waits until |
| 199 | +a connection becomes available. If this wait time exceeds the ``maxIdleTimeMS`` |
| 200 | +value, the driver responds with an error. For more information about how |
| 201 | +connection pooling works, see |
| 202 | +:ref:`How Does Connection Pooling Work in the Go Driver? <golang-faq-connection-pool>` |
| 203 | +in the FAQ. |
| 204 | + |
| 205 | +Timeout Error |
| 206 | +~~~~~~~~~~~~~ |
| 207 | + |
| 208 | +Sometimes when you send a request through the driver to the server, the request |
| 209 | +times out. When this happens, you might receive an error message |
| 210 | +similar to the following message: |
| 211 | + |
| 212 | +.. code-block:: none |
| 213 | + :copyable: false |
| 214 | + |
| 215 | + timed out while checking out a connection from connection pool: context canceled |
| 216 | + |
| 217 | +If you receive this error, try the following methods to resolve the |
| 218 | +issue. |
| 219 | + |
| 220 | +Set Timeout Option |
| 221 | +------------------ |
| 222 | + |
| 223 | +The ``Client`` supports a single ``Timeout`` option that controls the amount of |
| 224 | +time a single operation can take to execute. You can set this value by using |
| 225 | +the ``SetTimeout()`` method or by specifying the ``timeoutMS`` option in your |
| 226 | +connection string. |
| 227 | + |
| 228 | +The following example sets the single timeout value to 5 seconds using the |
| 229 | +connection string option: |
| 230 | + |
| 231 | +.. code-block:: go |
| 232 | + |
| 233 | + uri := "mongodb://<username>:<password>@<hostname>:27017/?timeoutMS=5000" |
| 234 | + client := mongo.Connect(uri) |
| 235 | + |
| 236 | +Check the Number of Connections |
| 237 | +------------------------------- |
| 238 | + |
| 239 | +The number of connections to the server may exceed ``maxPoolSize``. For more |
| 240 | +information about checking the number of connections, see |
| 241 | +:ref:`Error Sending Message <golang-troubleshooting-connection-number-connections>`. |
0 commit comments