Skip to content

Commit 3eff93c

Browse files
author
Bob Grabar
committed
DOCS-501 new connections document
1 parent 7fee73d commit 3eff93c

File tree

1 file changed

+388
-0
lines changed

1 file changed

+388
-0
lines changed

draft/administration/connections.txt

Lines changed: 388 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,388 @@
1+
.. index:: connections
2+
3+
===========
4+
Connections
5+
===========
6+
7+
.. default-domain:: mongodb
8+
9+
This document describes the URI format for defining connections between
10+
applications and MongoDB instances in the official MongoDB :doc:`drivers
11+
</applications/drivers>`.
12+
13+
When you connect to a MongoDB database server, the MongoDB driver creates a
14+
connection object that is instantiated once and then reused for every
15+
operation during the lifetime of the connection. The connection object
16+
uses the options you specify when creating the connection.
17+
The driver's reuse of the connection object saves on the costs of setup and
18+
teardown on connections.
19+
20+
Beginning with the Fall 2012 driver release, MongoDB uses two new connection classes for
21+
the creation of connection objects:
22+
23+
- ``MongoClient``: the standard connection class
24+
25+
- ``MongoReplicaSetClient``: a connection class that some drivers use
26+
separately for :term:`replica sets <replica set>`. Not all drivers use
27+
a separate class for connections to replica sets. See the
28+
:doc:`drivers </applications/drivers>` documentation for details.
29+
30+
In MongoDB versions prior to the Fall 2012 driver release, the connection classes vary from driver
31+
to driver.
32+
33+
You connect to a MongoDB database server by starting a :program:`mongod`
34+
instance through a MongoDB :doc:`driver </applications/drivers>` or
35+
through the :program:`mongo` shell.
36+
37+
Upon connection to the server, :program:`mongod` displays logging
38+
information and then displays that it is waiting for a client
39+
connections, as shown in the following example.
40+
41+
.. code-block:: none
42+
43+
Wed Nov 21 12:56:58 [websvr] admin web console waiting for connections on port 28017
44+
Wed Nov 21 12:56:58 [initandlisten] waiting for connections on port 27017
45+
46+
By default, MongoDB waits for connections on port 27017. The
47+
:program:`mongod` program runs in either the foreground or background.
48+
49+
.. note:: You *cannot* create a connection through HTTP on port 27017.
50+
Specifically, you *cannot* connect to MongoDB by going to
51+
``http://localhost:27017`` in your web browser.
52+
53+
.. index:: connections; connection string format
54+
.. _connections-standard-connection-string-format:
55+
56+
Standard Connection String Format
57+
---------------------------------
58+
59+
This section describes the standard format of the MongoDB connection URI
60+
used to connect to a MongoDB database server. The format is the same for
61+
all official MongoDB drivers. For a list of drivers and links to driver
62+
documentation, see :doc:`/applications/drivers`.
63+
64+
The following is the standard URI connection scheme:
65+
66+
.. code-block:: none
67+
68+
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
69+
70+
.. example:: In the following example, a connection issued from a Java
71+
driver logs into two databases in the ``test`` replica set:
72+
73+
.. code-block:: none
74+
75+
mongodb://db1.example.com,db2.example.com:2500/?replicaSet=test
76+
77+
- ``mongodb:// `` is a required prefix to identify that this is a string
78+
in the standard connection format.
79+
80+
- ``username:password@`` are optional. If they are given,
81+
:program:`mongod` attempts to log in to a specific database after
82+
connecting to a database server.
83+
84+
- ``host1`` is the only required part of the URI. It identifies a server
85+
address to connect to. It identifies either a hostname, IP address, or
86+
UNIX domain socket.
87+
88+
- ``:port1`` is optional and defaults to ``:27017`` if not provided.
89+
90+
- ``hostX`` is optional. You can specify as many hosts as necessary. You
91+
would specify multiple hosts, for example, for connections to replica
92+
sets.
93+
94+
- ``:portX`` is optional and defaults to ``:27017`` if not provided.
95+
96+
- ``/database`` is the name of the database to log in to. This is
97+
relevant only if the ``username:password@`` syntax is used. If
98+
``/database`` is not specified, the ``admin`` database is used by
99+
default.
100+
101+
- ``?options`` are connection options, as described in
102+
:ref:`connections-connection-options`.
103+
104+
If the ``database`` string is absent you must still have a ``/``
105+
between the last ``hostN`` and the ``?`` introducing the options.
106+
107+
.. index:: connections; options
108+
.. _connections-connection-options:
109+
110+
Connection Options
111+
------------------
112+
113+
The connection options used in the
114+
:ref:`connections-standard-connection-string-format` are listed in this
115+
section. The options are not case-sensitive.
116+
117+
Connection options are name=value pairs. The pairs are separated by "&".
118+
For backwards compatibility, ``;`` is accepted as a separator. But ``;``
119+
is deprecated.
120+
121+
.. example:: In the following example, a connection issued from a Java
122+
driver uses the ``replicaSet`` and ``connectTimeoutMS`` options.
123+
124+
.. code-block:: none
125+
126+
mongodb://db1.example.com,db2.example.com:2500/?replicaSet=test&connectTimeoutMS=300000
127+
128+
Replica Set Options
129+
~~~~~~~~~~~~~~~~~~~
130+
131+
.. data:: replicaSet=<name>
132+
133+
If the :program:`mongod` is a member of a :term:`replica set`, the
134+
name of the replica set.
135+
136+
Connection Options
137+
~~~~~~~~~~~~~~~~~~
138+
139+
.. data:: ssl=<true,false,prefer>
140+
141+
``true``: Initiate the connection with SSL.
142+
143+
``false``: Initiate the connection without SSL.
144+
145+
``prefer``: Initiate the connection with SSL, but if that fails initiate without SSL.
146+
147+
The default value is ``false``.
148+
149+
.. data:: connectTimeoutMS=<milliseconds>
150+
151+
The time in milliseconds to attempt a connection before timing out.
152+
The default is never to timeout, though different drivers might vary.
153+
See the :doc:`/applications/drivers` documentation.
154+
155+
.. data:: socketTimeoutMS=<milliseconds>
156+
157+
The time in milliseconds to attempt a send or receive on a socket
158+
before the attempt times out. The default is never to timeout, though
159+
different drivers might vary. See the :doc:`/applications/drivers`
160+
documentation.
161+
162+
Connection Pool Options
163+
~~~~~~~~~~~~~~~~~~~~~~~
164+
165+
The server uses one thread per TCP connection, therefore it is highly
166+
recommended that your application use some sort of connection pooling.
167+
Most drivers handle this for you behind the scenes. Some drivers do not
168+
support connection pools. See the :doc:`/applications/drivers`
169+
documentation.
170+
171+
.. data:: maxPoolSize=<number>
172+
173+
The maximum number of connections in the connection pool. The default
174+
value is ``100``.
175+
176+
.. data:: minPoolSize=<number>
177+
178+
The minimum number of connections in the connection pool The default
179+
value is ``0``.
180+
181+
.. data:: maxIdleTimeMS=<milliseconds>
182+
183+
The maximum number of milliseconds that a connection can remain idle
184+
in the pool before being removed and closed.
185+
186+
.. data:: waitQueueMultiple=<milliseconds>
187+
188+
A multiplier number that is multiplied with the
189+
``maxPoolSize`` setting to give the maximum number of threads allowed
190+
to wait for a connection to become available from the pool. For
191+
default values, see the :doc:`/applications/drivers` documentation.
192+
193+
.. data:: waitQueueTimeoutMS=<milliseconds>
194+
195+
The maximum time in milliseconds that a thread is allowed to wait for
196+
a connection to become available. For
197+
default values, see the :doc:`/applications/drivers` documentation.
198+
199+
Write Concern Options
200+
~~~~~~~~~~~~~~~~~~~~~
201+
202+
.. data:: fireAndForget=<true|false>
203+
204+
``false``: Sends a :dbcommand:`getLastError` command after
205+
every update to ensure that the update succeeded.
206+
207+
``true``: Does not send a :dbcommand:`getLastError` command after
208+
every update.
209+
210+
The default value depends on the connection class. Prior to the Fall
211+
2012 driver release, the default value is ``true``, which disables
212+
write concern. For the ``MongoClient`` and ``MongoReplicaSetClient``
213+
classes new in the Fall 2012 driver release the default is ``false``.
214+
215+
For the new ``MongoClient`` and ``MongoReplicaSetClient`` classes, if
216+
you enable ``fireAndForget`` but also enable write concern through
217+
the ``w`` option, MongoDB issues an exception about the conflict.
218+
219+
.. data:: w=<number or string>
220+
221+
Used by the :dbcommand:`getLastError` command either
222+
to configure write concern on members of :term:`replica sets <replica
223+
set>` *or* to disable write concern entirely.
224+
225+
``0``: Disables write concern. If you disable write concern but
226+
enable the :dbcommand:`getLastError` command's ``journal`` option, the
227+
journal option prevails and write concern with journaling is enabled.
228+
229+
``1``: Enables write concern on a standalone :program:`mongod` or the
230+
:term:`primary` in a replica set. *This is the default setting.*
231+
232+
*A number greater than 1*: Confirms that write operations have
233+
replicated to the specified number of replica set members, including
234+
the primary. If you set ``w`` to a number that is greater than the
235+
number of set members that hold data, MongoDB waits for the
236+
non-existent members become available, which means MongoDB blocks
237+
indefinitely.
238+
239+
``majority``: Confirms that write operations have replicated to the
240+
majority of set members.
241+
242+
``-1``: Turns off reporting of network errors.
243+
244+
For the new ``MongoClient`` and ``MongoReplicaSetClient`` classes, if
245+
you enable write concern but also enable ``fireAndForget``, MongoDB
246+
issues an exception about the conflict.
247+
248+
.. data:: wtimeoutMS=<milliseconds>
249+
250+
The time in milliseconds to wait for replication to succeed, as
251+
specified in the ``w`` option, before timing out.
252+
253+
.. data:: journal=<true|false>
254+
255+
``true``: Sync to journal.
256+
257+
Confirm that the :program:`mongod` instance has written the data to
258+
the on-disk journal. This is equivalent to specifying the
259+
:dbcommand:`getLastError` command with the ``j`` option enabled.
260+
261+
``false``: Does not confirm that data is written to the on-disk
262+
journal.
263+
264+
The default value is ``false``.
265+
266+
If you enable the ``journal``option but have not enabled write concern through
267+
the ``w`` option, the journal option prevails and write concern with
268+
journaling is enabled.
269+
270+
Read Preference Options
271+
~~~~~~~~~~~~~~~~~~~~~~~
272+
273+
.. data:: slaveOk=<true|false>
274+
275+
Specifies whether a driver connected to a replica set sends reads
276+
to secondaries. The default setting is ``false``.
277+
278+
.. data:: readPreference=<string>
279+
280+
Specifies the :term:`replica set` read preference for this
281+
connection. This setting overrides any ``slaveOk`` value. The read
282+
preference values are the following:
283+
284+
- :readmode:`primary`
285+
- :readmode:`primaryPreferred`
286+
- :readmode:`secondary`
287+
- :readmode:`secondaryPreferred`
288+
- :readmode:`nearest`
289+
290+
For descriptions of each value, see
291+
:ref:`replica-set-read-preference-modes`.
292+
293+
The default value is :readmode:`primary`, which sends all read
294+
operations to the replica set's :term:`primary`.
295+
296+
.. data:: readPreferenceTags=<string>
297+
298+
Specifies a tag set as a comma-separated list of
299+
colon-separated key-value pairs. For example:
300+
301+
.. code-block:: none
302+
303+
dc:ny,rack:1
304+
305+
To specify a *list* of tag sets, use multiple ``readPreferenceTags``.
306+
The following specifies two tag sets and an empty tag set:
307+
308+
.. code-block:: none
309+
310+
readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:ny&readPreferenceTags=
311+
312+
Order matters when using multiple ``readPreferenceTags``.
313+
314+
Miscellaneous Configuration
315+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
316+
317+
.. data:: uuidRepresentation=<string>
318+
319+
``standard``: The standard binary representation.
320+
321+
``csharpLegacy``: The default representation for the C# driver.
322+
323+
``javaLegacy``: The default representation for the Java driver.
324+
325+
``pythonLegacy``: The default representation for the Python driver.
326+
327+
For the default, see the :doc:`drivers </applications/drivers>`
328+
documentation for your driver.
329+
330+
.. _connections-connection-examples:
331+
332+
Examples
333+
--------
334+
335+
Connect to a database server running locally on the default port:
336+
337+
.. code-block:: none
338+
339+
mongodb://localhost
340+
341+
Connect and log in to the ``admin`` database as user ``rachel`` with
342+
password ``moon``:
343+
344+
.. code-block:: none
345+
346+
mongodb://rachel:moon@localhost
347+
348+
Connect and log in to the ``baz`` database as user ``rachel`` with
349+
password ``moon``:
350+
351+
.. code-block:: none
352+
353+
mongodb://rachel:moon@localhost/baz
354+
355+
Connect to a UNIX domain socket:
356+
357+
.. code-block:: none
358+
359+
mongodb:///tmp/mongodb-27017.sock
360+
361+
Connect to a :term:`replica set` with two members, one on
362+
``example1.com`` and the other on ``example2.com``:
363+
364+
.. code-block:: none
365+
366+
mongodb://example1.com:27017,example2.com:27017
367+
368+
Connect to a replica set with three members running on ``localhost``, on
369+
ports 27017, 27018 and 27019:
370+
371+
.. code-block:: none
372+
373+
mongodb://localhost,localhost:27018,localhost:27019
374+
375+
Connect to a replica set with three members. Send all writes to the
376+
:term:`primary` and distribute reads to the :term:`secondaries <secondary>`:
377+
378+
.. code-block:: none
379+
380+
mongodb://example1.com,example2.com,example3.com/?slaveOk=true
381+
382+
Connect to a replica set with write concern configured to wait for
383+
replication to succeed on at least two members, with a two-second
384+
timeout.
385+
386+
.. code-block:: none
387+
388+
mongodb://example1.com,example2.com,example3.com/?w=2&wtimeoutMS=2000

0 commit comments

Comments
 (0)