Skip to content

Commit ac651ec

Browse files
authored
DOCSP-29490: Created Connection Troubleshooting Guide (#102)
* DOCSP-29490: Initial topic creation * PR feedback * Further PR feedback * Final PR adjustments
1 parent c46f51a commit ac651ec

File tree

4 files changed

+334
-47
lines changed

4 files changed

+334
-47
lines changed

source/connection-troubleshooting.txt

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
.. _csharp-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) <csharp-faq>` for the
22+
{+driver-short+}
23+
- The :ref:`Issues & Help <csharp-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+
.. _csharp-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+
.. _csharp-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+
Authentication Error
70+
~~~~~~~~~~~~~~~~~~~~
71+
72+
The {+driver-short+} can fail to connect to a MongoDB instance if
73+
the authentication mechanism is not configured correctly. If you are using ``SCRAM-SHA-256``
74+
or ``SCRAM-SHA-1`` for authentication and the driver fails to connect, the
75+
driver might raise an error message similar to one of the following messages:
76+
77+
.. code-block:: none
78+
:copyable: false
79+
80+
Command failed with error 18 (AuthenticationFailed): 'Authentication
81+
failed.' on server <hostname>:<port>.
82+
83+
.. code-block:: none
84+
:copyable: false
85+
86+
Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","principalName":
87+
"<user>","<auth database>":"<user>","client":"127.0.0.1:2012",
88+
"result":"UserNotFound: Could not find user}}
89+
90+
.. code-block:: none
91+
:copyable: false
92+
93+
connection() error occurred during connection handshake: auth error:
94+
sasl conversation error: unable to authenticate using mechanism
95+
"SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.
96+
97+
The following sections describe actions you can take to potentially resolve the
98+
issue.
99+
100+
.. _csharp-troubleshooting-connection-string-auth:
101+
102+
Check Your Connection String
103+
----------------------------
104+
105+
An invalid connection string is the most common cause of authentication
106+
issues when attempting to connect to MongoDB using connection strings and
107+
``SCRAM-SHA-256`` or ``SCRAM-SHA-1``.
108+
109+
.. tip::
110+
111+
For more information about connection strings,
112+
see :ref:`Connection URI <csharp-connection-uri>` in the Connection Guide.
113+
114+
If your connection string contains a username and password, ensure that they
115+
are in the correct format. If the username or password includes any of the
116+
following characters, they must be
117+
`percent encoded <https://tools.ietf.org/html/rfc3986#section-2.1>`__:
118+
119+
.. code-block:: none
120+
121+
: / ? # [ ] @
122+
123+
The following example shows how to percent encode "#MyPassword?":
124+
125+
.. code-block:: csharp
126+
127+
Console.WriteLine(System.Web.HttpUtility.UrlEncode("#MyPaassword?"));
128+
129+
This results in the following output:
130+
131+
.. code-block:: none
132+
133+
%23MyPassword%3F
134+
135+
.. _csharp-troubleshooting-connection-mongoclientsettings:
136+
137+
Verify the MongoClientSettings Properties
138+
-----------------------------------------
139+
140+
You can use a ``MongoClientSettings`` object to configure the settings when
141+
attempting to connect to a MongoDB deployment. You can use the ``Credential``
142+
property to set authentication information. If the credential information is not
143+
correct, you will receive authentication errors when you attempt to connect to
144+
your MongoDB deployment.
145+
146+
.. _csharp-troubleshooting-connection-admin:
147+
148+
Verify the User Is in the Authentication Database
149+
-------------------------------------------------
150+
151+
To successfully authenticate a connection by using a username and password with
152+
``SCRAM-SHA-256`` or ``SCRAM-SHA-1``, the username must be defined in the
153+
authentication database. The default authentication database is the ``admin``
154+
database. To use a different database for authentication, specify the
155+
``authSource`` option in the connection string. The following example instructs the
156+
driver to use ``users`` as the authentication database:
157+
158+
.. code-block:: csharp
159+
:copyable: true
160+
161+
using MongoDB.Driver;
162+
163+
// Connection URI
164+
const string connectionUri = "mongodb://<username>:<password>@<hostname>:<port>/?authSource=users";
165+
166+
// Create a new client and connect to the server
167+
var client = new MongoClient(connectionUri);
168+
169+
You can also set configuration settings by creating a ``MongoClientSettings``
170+
object and passing that to the ``MongoClient`` constructor. You can use the
171+
``Credential`` property to set the login credentials including specifying the
172+
authentication database. For more information about using ``MongoClientSettings``
173+
as well as some examples, see
174+
:ref:`Using MongoClientSettings <csharp-mongo-client-settings>`.
175+
176+
You can check if this is the issue by attempting to connect to a MongoDB
177+
instance hosted on the local machine with the same code. A deployment on
178+
the same machine doesn't require any authorization to connect.
179+
180+
Error Sending Message
181+
~~~~~~~~~~~~~~~~~~~~~
182+
183+
When the driver fails to send a command after you make a request,
184+
it may display the following error message:
185+
186+
.. code-block:: none
187+
:copyable: false
188+
189+
com.mongodb.MongoSocketWriteException: Exception sending message
190+
191+
The following sections describe actions you can take to potentially resolve the
192+
issue.
193+
194+
Check the User Permissions
195+
--------------------------
196+
197+
Verify that you've accessed the MongoDB deployment with the correct user. The
198+
term "message" in the error can be a command sent by the driver.
199+
If you are using a user that doesn't have permissions to send the command, the
200+
driver could generate this error.
201+
202+
Also ensure that the user has the appropriate permissions for the message you
203+
are sending. MongoDB uses Role-Based Access Control (RBAC) to control access
204+
to a MongoDB deployment. For more information about how to configure RBAC in MongoDB,
205+
see :manual:`Default MongoDB Port </core/authorization/>`.
206+
207+
Configure Your Firewall
208+
-----------------------
209+
210+
The firewall needs to have an open port for communicating with the MongoDB
211+
instance. For more information about configuring the firewall, see
212+
:ref:`Configure Your Firewall <csharp-troubleshooting-connection-firewall>` in
213+
the Connection Error section.
214+
215+
.. _csharp-troubleshooting-connection-number-connections:
216+
217+
Check the Number of Connections
218+
-------------------------------
219+
220+
Each ``MongoClient`` instance supports a maximum number of concurrent open
221+
connections in its connection pool. You can configure the parameter ``MaxConnectionPoolSize``
222+
which defines this limit. The default value is ``100``. If there are already a
223+
number of open connections equal to ``MaxConnectionPoolSize``, the server waits until
224+
a connection becomes available. If this wait time exceeds the ``MaxConnectionIdleTime``
225+
value, the driver responds with an error.
226+
227+
For more information about how connection pooling works, see
228+
:ref:`How Does Connection Pooling Work in the {+driver-short+}? <csharp-faq-connection-pool>`
229+
in the FAQ.
230+
231+
Too Many Open Connections
232+
~~~~~~~~~~~~~~~~~~~~~~~~~
233+
234+
The driver creates the following error message when it attempts to open a
235+
connection, but it's reached the maximum number of connections:
236+
237+
.. code-block:: none
238+
:copyable: false
239+
240+
connection refused because too many open connections
241+
242+
The following section describes a method that may help resolve the issue.
243+
244+
Check the Number of Connections
245+
-------------------------------
246+
247+
If you need to create more open connections, increase ``MaxConnectionPoolSize``. For more
248+
information about checking the number of connections, see
249+
:ref:`Check the Number of Connections <csharp-troubleshooting-connection-number-connections>`
250+
in the Error Sending Message section.
251+
252+
Timeout Error
253+
~~~~~~~~~~~~~
254+
255+
When the network is not able to deliver a request from the driver to the server
256+
quickly enough, it can time out. When this happens, you might receive an error message
257+
similar to the following message:
258+
259+
.. code-block:: none
260+
:copyable: false
261+
262+
timed out while checking out a connection from connection pool: context canceled
263+
264+
If you receive this error, try the following action to resolve the
265+
issue.
266+
267+
Set connectTimeoutMS
268+
--------------------
269+
270+
The driver may hang when it's unable to establish a connection because the driver
271+
takes too long attempting to reach unreachable replica set nodes. You can limit the
272+
time the driver spends attempting to establish the connection by using the
273+
``connectTimeMS`` setting. To learn more about this setting, see the
274+
:manual:`Timeout Options </reference/connection-string/#timeout-options>` in
275+
the Server manual.
276+
277+
You should ensure the ``connectTimeoutMS`` setting is not lower than
278+
the highest network latency you have to a member of the set. If one of the
279+
secondary members has a latency of 10000 milliseconds, setting the
280+
``connectTimeoutMS`` to 9000 prevents the driver from ever connecting to that
281+
member.
282+
283+
You can set this option on the connection string. The following example sets
284+
``connectTimeoutMS`` to 10000 milliseconds.
285+
286+
.. code-block:: csharp
287+
:copyable: true
288+
289+
using MongoDB.Driver;
290+
291+
// Connection URI
292+
const string connectionUri = "mongodb://<username>:<password>@<hostname>:<port>/?connectTimeoutMS=10000";
293+
294+
// Create a new client and connect to the server
295+
var client = new MongoClient(connectionUri);
296+
297+
You can also set configuration settings by creating a ``MongoClientSettings``
298+
object and passing that to the ``MongoClient`` constructor. For more information
299+
about using ``MongoClientSettings`` as well as some examples, see
300+
:ref:`Using MongoClientSettings <csharp-mongo-client-settings>`.
301+
302+
Check the Number of Connections
303+
-------------------------------
304+
305+
The number of connections to the server may exceed ``MaxConnectionPoolSize``. For more
306+
information about checking the number of connections, see
307+
:ref:`Check the Number of Connections <csharp-troubleshooting-connection-number-connections>`
308+
in the Error Sending Message section.

source/faq.txt

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ This page contains frequently asked questions and their corresponding answers.
1818
see the :ref:`csharp-issues-help` page for next steps and more
1919
resources.
2020

21+
Why Am I Getting Errors While Connecting to MongoDB?
22+
----------------------------------------------------
23+
24+
If you have trouble connecting to a MongoDB deployment, see
25+
the :ref:`Connection Troubleshooting Guide <csharp-connection-troubleshooting>`
26+
for possible solutions.
27+
2128
.. _csharp-faq-connection-pool:
2229

2330
How Does Connection Pooling Work in the {+driver-short+}?
@@ -165,43 +172,6 @@ The error message consists of multiple parts:
165172
can use tools like ``openssl s_client`` to debug TLS/SSL-related
166173
certificate problems.
167174

168-
Why is the Wait Queue for Acquiring a Connection to the Server Full?
169-
--------------------------------------------------------------------
170-
171-
This exception usually indicates a threading or concurrency problem in
172-
your application. The driver checks out a connection from the selected
173-
server’s connection pool for every read or write operation. If
174-
the connection pool is already at ``maxPoolSize`` - 100 by default - then the
175-
requesting thread blocks in a wait queue. The wait queue's default size
176-
is 5 times ``maxPoolSize``, or 500. If the wait queue is also full, the driver
177-
throws a ``MongoWaitQueueFullException``. The exception looks
178-
similar to the following:
179-
180-
.. code-block:: none
181-
182-
MongoDB.Driver.MongoWaitQueueFullException: The wait queue for
183-
acquiring a connection to server myServer is full.
184-
185-
To resolve this issue, try the following steps:
186-
187-
1. Tune your indexes. By improving the performance of your queries,
188-
you can reduce the time that operations take and reduce the number of
189-
concurrent connections needed for your workload.
190-
#. If you have long-running analytical queries, you may wish to isolate
191-
them to dedicated analytics nodes using :manual:`read preference tags
192-
</core/read-preference/>` or a hidden secondary.
193-
#. Increase ``maxPoolSize`` to allow more simultaneous operations to a
194-
given cluster node. If your MongoDB cluster does not have sufficient
195-
resources to handle the additional connections and simultaneous
196-
workload, performance can decrease due to resource contention
197-
on the cluster nodes. Adjust this setting only with careful
198-
consideration and testing.
199-
#. Increase ``waitQueueMultiple`` to allow more threads/tasks to block
200-
waiting for a connection. This is rarely the appropriate solution and
201-
can severely affect your application performance. Before
202-
considering changes to this setting, address the concurrency problems
203-
in your application.
204-
205175
.. _csharp-faq-unsupported-expressions:
206176

207177
Why are Certain LINQ or Builder Expressions Unsupported?

source/fundamentals/connection/connect.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Connection Guide
1515
This guide shows you how to connect to a MongoDB instance or replica set
1616
deployment using the {+driver-short+}.
1717

18+
.. _csharp_connection_uri:
19+
1820
Connection URI
1921
--------------
2022

0 commit comments

Comments
 (0)