|
| 1 | +.. _node-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) <node-faq>` for the |
| 22 | + {+driver-short+} |
| 23 | + - The :ref:`Issues & Help <node-issues-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 indicates that the driver cannot connect to a server |
| 33 | +on the specified hostname or port. Multiple situations can generate this error |
| 34 | +message. In this sample error message, the hostname is ``127.0.0.1`` and the |
| 35 | +port is ``27017``: |
| 36 | + |
| 37 | +.. code-block:: none |
| 38 | + :copyable: false |
| 39 | + |
| 40 | + Error: couldn't connect to server 127.0.0.1:27017 |
| 41 | + |
| 42 | +The following sections describe actions you can take to potentially resolve the |
| 43 | +issue. |
| 44 | + |
| 45 | +.. _node-troubleshooting-connection-string-port: |
| 46 | + |
| 47 | +Check Your Connection String |
| 48 | +---------------------------- |
| 49 | + |
| 50 | +Verify that the hostname and port number in the connection string are both |
| 51 | +accurate. The default port value for a MongoDB instance is |
| 52 | +``27017``, but you can configure MongoDB to communicate on another port. |
| 53 | + |
| 54 | +.. _node-troubleshooting-connection-firewall: |
| 55 | + |
| 56 | +Configure Your Firewall |
| 57 | +----------------------- |
| 58 | + |
| 59 | +Verify that the ports your MongoDB deployment listens on are not blocked by a |
| 60 | +firewall on the same network. MongoDB uses port ``27017`` by default. To learn |
| 61 | +more about the default ports MongoDB uses and how to change them, see |
| 62 | +:manual:`Default MongoDB Port </reference/default-mongodb-port/>`. |
| 63 | + |
| 64 | +.. warning:: |
| 65 | + |
| 66 | + Do not open a port in your firewall unless you are sure it's the port |
| 67 | + used by your MongoDB deployment. |
| 68 | + |
| 69 | +ECONNREFUSED Error |
| 70 | +~~~~~~~~~~~~~~~~~~ |
| 71 | + |
| 72 | +If the connection is refused when the driver attempts to connect to the MongoDB |
| 73 | +instance, it generates this error message: |
| 74 | + |
| 75 | +.. code-block:: none |
| 76 | + :copyable: false |
| 77 | + |
| 78 | + MongoServerSelectionError: connect ECONNREFUSED <IPv6 address>:<port> |
| 79 | + |
| 80 | +The following sections describe actions you can take to potentially resolve the |
| 81 | +issue. |
| 82 | + |
| 83 | +Ensure MongoDB and Your Client Use the Same Protocol |
| 84 | +---------------------------------------------------- |
| 85 | + |
| 86 | +In Node.js v17 and later, the DNS resolver uses ``IPv6`` by default when both |
| 87 | +the client and host support both. For example, if MongoDB uses IPv4 and your |
| 88 | +client uses IPv6, the driver returns the previous error message. |
| 89 | + |
| 90 | +You can configure your MongoDB deployment to use ``IPv6`` mode when starting |
| 91 | +with ``mongod`` or ``mongos``. For more information about how to specify |
| 92 | +``IPv6`` mode, see |
| 93 | +:manual:`IP Binding </core/security-mongodb-configuration/>` in the server |
| 94 | +manual. |
| 95 | + |
| 96 | +As an alternative, you can explicitly use ``IPv4`` with your client by |
| 97 | +specifying ``family: 4`` as an |
| 98 | +`option to your MongoClient <{+api+}/interfaces/MongoClientOptions.html#family>`__. |
| 99 | + |
| 100 | +.. code-block:: js |
| 101 | + |
| 102 | + const client = new MongoClient(uri, { |
| 103 | + family: 4, |
| 104 | + }); |
| 105 | + |
| 106 | +ECONNRESET Error |
| 107 | +~~~~~~~~~~~~~~~~ |
| 108 | + |
| 109 | +If the connection is reset when the driver calls ``client.connect()``, it |
| 110 | +generates this error message: |
| 111 | + |
| 112 | +.. code-block:: none |
| 113 | + :copyable: false |
| 114 | + |
| 115 | + MongoServerSelectionError: connect ECONNRESET ::<IP address>:<port> |
| 116 | + |
| 117 | +The following section describes a method that may help resolve the issue. |
| 118 | + |
| 119 | +Control the Number of File Descriptors |
| 120 | +-------------------------------------- |
| 121 | + |
| 122 | +A file descriptor is a unique identifier associated with an open process. In most |
| 123 | +operating systems, each open connection from the driver is associated with a |
| 124 | +file descriptor. Operating systems typically have a limit on the number of file |
| 125 | +descriptors used by a single process. An ``ECONNRESET`` error can occur |
| 126 | +if the number of connections exceeds this limit. |
| 127 | + |
| 128 | +You can set the maxmimum number of connections by setting ``maxPoolSize``. To |
| 129 | +resolve this error, you can decrease the number of maximum allowed connections |
| 130 | +by setting the value of ``maxPoolSize``. Alternatively, you could increase the |
| 131 | +file descriptor limit in your operating system. |
| 132 | + |
| 133 | +.. warning:: |
| 134 | + |
| 135 | + Changing the configuration of your operating system should always be done |
| 136 | + with caution. |
| 137 | + |
| 138 | +Authentication Error |
| 139 | +~~~~~~~~~~~~~~~~~~~~ |
| 140 | + |
| 141 | +The {+driver-short+} can fail to connect to a MongoDB instance if |
| 142 | +the authorization is not configured correctly. If you are using ``SCRAM-SHA-256`` |
| 143 | +for authentication and the driver fails to connect, the driver might raise an |
| 144 | +error message similar to one of the following messages: |
| 145 | + |
| 146 | +.. code-block:: none |
| 147 | + :copyable: false |
| 148 | + |
| 149 | + Command failed with error 18 (AuthenticationFailed): 'Authentication |
| 150 | + failed.' on server <hostname>:<port>. |
| 151 | + |
| 152 | +.. code-block:: none |
| 153 | + :copyable: false |
| 154 | + |
| 155 | + connection() error occurred during connection handshake: auth error: |
| 156 | + sasl conversation error: unable to authenticate using mechanism |
| 157 | + "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed. |
| 158 | + |
| 159 | +The following sections describe actions you can take to potentially resolve the |
| 160 | +issue. |
| 161 | + |
| 162 | +.. _node-troubleshooting-connection-string-auth: |
| 163 | + |
| 164 | +Check Your Connection String |
| 165 | +---------------------------- |
| 166 | + |
| 167 | +An invalid connection string is the most common cause of authentication |
| 168 | +issues when attempting to connect to MongoDB using ``SCRAM-SHA-256``. |
| 169 | + |
| 170 | +.. tip:: |
| 171 | + |
| 172 | + For more information about connection strings, |
| 173 | + see :ref:`Connection URI <node-connection-uri>` in the Connection Guide. |
| 174 | + |
| 175 | +If your connection string contains a username and password, ensure that they |
| 176 | +are in the correct format. If the username or password includes any of the |
| 177 | +following characters, they must be |
| 178 | +`percent encoded <https://tools.ietf.org/html/rfc3986#section-2.1>`__: |
| 179 | + |
| 180 | +.. code-block:: none |
| 181 | + |
| 182 | + : / ? # [ ] @ |
| 183 | + |
| 184 | +The following example shows how to percent encode "#MyP@assword?": |
| 185 | + |
| 186 | +.. code-block:: javascript |
| 187 | + |
| 188 | + console.log(encodeURIComponent('#MyP@assword?')); |
| 189 | + |
| 190 | +This results in the following output: |
| 191 | + |
| 192 | +.. code-block:: none |
| 193 | + |
| 194 | + "%23MyP%40assword%3F" |
| 195 | + |
| 196 | +.. _node-troubleshooting-connection-admin: |
| 197 | + |
| 198 | +Verify the User Is in the Authentication Database |
| 199 | +------------------------------------------------- |
| 200 | + |
| 201 | +To successfully authenticate a connection by using a username and password with |
| 202 | +``SCRAM-SHA-256``, the username must be defined in the authentication database. |
| 203 | +The default authentication database is the ``admin`` database. To use a different |
| 204 | +database for authentication, specify the ``authSource`` in the connection string. |
| 205 | +The following example instructs the driver to use ``users`` as the authentication |
| 206 | +database: |
| 207 | + |
| 208 | +.. code-block:: javascript |
| 209 | + :copyable: true |
| 210 | + |
| 211 | + const { MongoClient } = require("mongodb"); |
| 212 | + const uri = "mongodb://<username>:<password>@<hostname>:<port>/?authSource=users"; |
| 213 | + const client = new MongoClient(uri); |
| 214 | + |
| 215 | +You can check if this is the issue by attempting to connect to a MongoDB |
| 216 | +instance hosted on the local machine with the same code. A deployment on |
| 217 | +the same machine doesn't require any authorization to connect. |
| 218 | + |
| 219 | +Error Sending Message |
| 220 | +~~~~~~~~~~~~~~~~~~~~~ |
| 221 | + |
| 222 | +When the driver fails to send a command after you make a request, |
| 223 | +it may display the following error message: |
| 224 | + |
| 225 | +.. code-block:: none |
| 226 | + :copyable: false |
| 227 | + |
| 228 | + com.mongodb.MongoSocketWriteException: Exception sending message |
| 229 | + |
| 230 | +The following sections describe actions you can take to potentially resolve the |
| 231 | +issue. |
| 232 | + |
| 233 | +Check the User Permissions |
| 234 | +-------------------------- |
| 235 | + |
| 236 | +Verify that you've accessed the MongoDB deployment with the correct user. The |
| 237 | +term "message" in the error can be a command sent by the driver. |
| 238 | +If you are using a user that doesn't have permissions to send the command, the |
| 239 | +driver could generate this error. |
| 240 | + |
| 241 | +Also ensure that the user has the appropriate permissions for the message you |
| 242 | +are sending. MongoDB uses Role-Based Access Control (RBAC) to control access |
| 243 | +to a MongoDB deployment. For more information about how to configure RBAC in MongoDB, |
| 244 | +see :manual:`Default MongoDB Port </core/authorization/>`. |
| 245 | + |
| 246 | +Configure Your Firewall |
| 247 | +----------------------- |
| 248 | + |
| 249 | +The firewall needs to have an open port for communicating with the MongoDB |
| 250 | +instance. For more information about configuring the firewall, see |
| 251 | +:ref:`Configure Your Firewall <node-troubleshooting-connection-firewall>` in |
| 252 | +the Connection Error section. |
| 253 | + |
| 254 | +.. _node-troubleshooting-connection-number-connections: |
| 255 | + |
| 256 | +Check the Number of Connections |
| 257 | +------------------------------- |
| 258 | + |
| 259 | +Each ``MongoClient`` instance supports a maximum number of concurrent open |
| 260 | +connections in its connection pool. You can configure the parameter ``maxPoolSize`` |
| 261 | +which defines this limit. The default value is ``100``. If there are already a |
| 262 | +number of open connections equal to ``maxPoolSize``, the server waits until |
| 263 | +a connection becomes available. If this wait time exceeds the ``maxIdleTimeMS`` |
| 264 | +value, the driver responds with an error. |
| 265 | + |
| 266 | +For more information about how connection pooling works, see |
| 267 | +:ref:`How Does Connection Pooling Work in the Node Driver? <node-faq-connection-pool>` |
| 268 | +in the FAQ. |
| 269 | + |
| 270 | +Too Many Open Connections |
| 271 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 272 | + |
| 273 | +The driver creates the following error message when it attempts to open a |
| 274 | +connection, but it's reached the maximum number of connections: |
| 275 | + |
| 276 | +.. code-block:: none |
| 277 | + :copyable: false |
| 278 | + |
| 279 | + connection refused because too many open connections |
| 280 | + |
| 281 | +The following section describes a method that may help resolve the issue. |
| 282 | + |
| 283 | +Check the Number of Connections |
| 284 | +------------------------------- |
| 285 | + |
| 286 | +If you need to create more open connections, increase ``maxPoolSize``. For more |
| 287 | +information about checking the number of connections, see |
| 288 | +:ref:`Check the Number of Connections <node-troubleshooting-connection-number-connections>` |
| 289 | +in the Error Sending Message section. |
| 290 | + |
| 291 | +Timeout Error |
| 292 | +~~~~~~~~~~~~~ |
| 293 | + |
| 294 | +When the network is not able to deliver a request from the driver to the server |
| 295 | +quickly enough, it can time out. When this happens, you might receive an error message |
| 296 | +similar to the following message: |
| 297 | + |
| 298 | +.. code-block:: none |
| 299 | + :copyable: false |
| 300 | + |
| 301 | + timed out while checking out a connection from connection pool: context canceled |
| 302 | + |
| 303 | +If you receive this error, try the following action to resolve the |
| 304 | +issue. |
| 305 | + |
| 306 | +Set connectTimeoutMS |
| 307 | +-------------------- |
| 308 | + |
| 309 | +The driver may hang when it's unable to establish a connection because it |
| 310 | +takes too long attempting to reach unreachable replica set nodes. You can limit the |
| 311 | +time the driver spends attempting to establish the connection by using the |
| 312 | +``connectTimeMS`` setting. To learn more about this setting, see the |
| 313 | +:manual:`Timeout Options </reference/connection-string/#timeout-options>` in |
| 314 | +the server manual. |
| 315 | + |
| 316 | +You should ensure the ``connectTimeoutMS`` setting is not lower than |
| 317 | +the highest network latency you have to a member of the set. If one of the |
| 318 | +secondary members has a latency of 10000 milliseconds, setting the |
| 319 | +``connectTimeoutMS`` to 9000 prevents the driver from ever connecting to that |
| 320 | +member. |
| 321 | + |
| 322 | +The following example sets ``connectTimeoutMS`` to 10000 milliseconds. |
| 323 | + |
| 324 | +.. code-block:: javascript |
| 325 | + |
| 326 | + const client = new MongoClient(uri, { |
| 327 | + connectTimeoutMS: 10000, |
| 328 | + }); |
| 329 | + |
| 330 | +Check the Number of Connections |
| 331 | +------------------------------- |
| 332 | + |
| 333 | +The number of connections to the server may exceed ``maxPoolSize``. For more |
| 334 | +information about checking the number of connections, see |
| 335 | +:ref:`Check the Number of Connections <node-troubleshooting-connection-number-connections>` |
| 336 | +in the Error Sending Message section. |
0 commit comments