Skip to content

Commit 01fa14d

Browse files
schmallisokay-kim
authored andcommitted
DOCS-11202: mongo connections using srv format will prompt for username
1 parent 2b94710 commit 01fa14d

File tree

3 files changed

+103
-39
lines changed

3 files changed

+103
-39
lines changed

source/reference/connection-string.txt

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ The components of this string are:
9292

9393
* - ``?options``
9494

95-
- Connection specific options. See
95+
- A query string that specifies connection specific options. See
9696
:ref:`connections-connection-options` for a full description of
9797
these options.
9898

@@ -133,81 +133,93 @@ The components of this string are:
133133

134134
.. index:: connections; dns-seedlist
135135
.. _connections-dns-seedlist:
136-
137-
136+
137+
138138
DNS Seedlist Connection Format
139139
------------------------------
140140

141141
.. versionadded:: 3.6
142142

143143
In addition to the standard connection format, MongoDB supports a
144-
DNS-constructed seedlist. Using DNS to construct the available servers
145-
list allows more flexibility of deployment and the ability to change the
146-
servers in rotation without reconfiguring clients.
144+
:abbr:`DNS (Domain Name Service)`-constructed seed list. Using DNS to
145+
construct the available servers list allows more flexibility of
146+
deployment and the ability to change the servers in rotation without
147+
reconfiguring clients.
147148

148149
In order to leverage the DNS seedlist, use a connection string prefix of
149-
``mongodb+srv:`` in place of the ``mongodb:`` string above.
150+
``mongodb+srv:`` rather than the standard ``mongodb:``. The ``+srv``
151+
indicates to the client that the hostname that follows corresponds to a
152+
DNS SRV record. The driver or :binary:`~bin.mongo` shell will then
153+
query the DNS for the record to determine which hosts are running the
154+
:binary:`~bin.mongod` instances.
150155

151-
The ``+srv`` indicates to the mongo client that the hostname that
152-
follows corresponds to a DNS SRV record. The client driver will then
153-
query the DNS for the record to determine the hosts that are running the
154-
mongod instances.
156+
.. note::
155157

156-
For example, to connect to a DNS-listed hostname:
158+
Use of the ``+srv`` connection string modifier
159+
automatically sets the ``ssl`` option to ``true`` for the connection.
160+
You can override this behavior by explicitly setting the ``ssl``
161+
option to ``false`` with ``ssl=false`` in the query string.
162+
163+
The following example shows a typical connection string for a DNS
164+
seedlist connection string:
157165

158166
.. code-block:: none
159167

160-
mongodb+srv://server.example.com/
168+
mongodb+srv://server.example.com/
161169

162-
A typical DNS configuration for the connection string above might look
163-
something like this:
170+
The corresponding DNS configuration might resemble:
164171

165172
.. code-block:: none
166173

167174
Record TTL Class Priority Weight Port Target
168175
_mongodb._tcp.server.example.com. 86400 IN SRV 0 5 27317 mongodb1.example.com.
169176
_mongodb._tcp.server.example.com. 86400 IN SRV 0 5 27017 mongodb2.example.com.
170177

171-
.. note::
172-
The hostnames returned in SRV records must share the same parent
173-
domain (in this example, ``example.com``) as the given hostname.
178+
.. important::
179+
180+
The hostnames returned in SRV records must share the same parent
181+
domain (in this example, ``example.com``) as the given hostname. If
182+
the parent domains and hostname do not match, you will not be able to
183+
connect.
174184

175-
The DNS seedlist connection string can also provide options as a query
176-
string, with a trailing "/?" as in the standard connection string above.
177-
However, the ``+srv`` appended to the standard connection string signals
178-
the driver to query the DNS for options as a configured TXT record.
179-
180-
Only two options are available for configuration via a TXT record:
181-
``replicaSet`` and ``authSource``, and only one TXT record is allowed
182-
per server. If multiple TXT records appear in the DNS and/or if the TXT
185+
Like the standard connection string, the DNS seedlist connection string
186+
supports specifying options as a query string. With a DNS seedlist
187+
connection string, you can *also* specify the following options via a
188+
TXT record:
189+
190+
- ``replicaSet``,
191+
- ``authSource``.
192+
193+
You may only specify one TXT record per :binary:`~bin.mongod` instance.
194+
If multiple TXT records appear in the DNS and/or if the TXT
183195
record contains an option other than ``replicaSet`` or ``authSource``,
184-
an error will be thrown by the driver.
196+
the client will return an error.
185197

186-
An example of a properly configured TXT record:
198+
The TXT record for the ``server.example.com`` DNS entry would resemble:
187199

188200
.. code-block:: none
189201

190202
Record TTL Class Text
191203
server.example.com. 86400 IN TXT "replicaSet=mySet&authSource=authDB"
192204

193-
In this case, taking into account both the DNS SRV records and the
194-
options retrieved from the TXT records, the parsed string will look like:
205+
Taken together, the DNS SRV records and the options specified in the TXT
206+
record resolve to the following standard format connection string:
195207

196208
.. code-block:: none
197209

198210
mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
199211

200-
Options set in a TXT record can be overridden by passing in a query
201-
string with the URI. In the example below, the query string has provided
212+
You can override the options specified in a TXT record by passing the option
213+
in the query string. In the following example, the query string has provided
202214
an override for the ``authSource`` option configured in the TXT record
203215
of the DNS entry above.
204216

205217
.. code-block:: none
206218

207219
mongodb+srv://server.example.com/?connectTimeoutMS=300000&authSource=aDifferentAuthDB
208220

209-
The rest of the option string will remain, and we can expect that the
210-
resulting URI would look like this (after parse).
221+
Given the override for the ``authSource``, the equivalent connection
222+
string in the standard format would be:
211223

212224
.. code-block:: none
213225

@@ -217,10 +229,15 @@ resulting URI would look like this (after parse).
217229
The ``mongodb+srv`` option will fail if there is no available DNS
218230
with records that correspond to the hostname identified in the
219231
connection string. In addition, use of the ``+srv`` connection string
220-
modifier sets the ``ssl`` option to ``true`` automatically for the
221-
connection. This can be overridden by explicitly setting the ``ssl``
222-
option to ``false`` with ``ssl=false`` in the query string.
232+
modifier automatically sets the ``ssl`` option to ``true`` for the
233+
connection. You can override this behavior by explicitly setting the
234+
``ssl`` option to ``false`` with ``ssl=false`` in the query string.
223235

236+
.. see::
237+
238+
:ref:`example-connect-mongo-using-srv` provides an example of
239+
connecting the :binary:`~bin.mongo` shell to a replica set using
240+
the DNS Seedlist Connection Format.
224241

225242
.. index:: connections; options
226243
.. _connections-connection-options:
@@ -849,5 +866,3 @@ The following connects to a sharded cluster with three :binary:`~bin.mongos` ins
849866
.. code-block:: none
850867

851868
mongodb://router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
852-
853-

source/reference/program/mongo.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ Typically users invoke the shell with the :binary:`~bin.mongo` command at
377377
the system prompt. Consider the following examples for other
378378
scenarios.
379379

380+
Connect to a :binary:`~bin.mongod` Instance with Access Control
381+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
382+
380383
To connect to a database on a remote host using authentication and a
381384
non-standard port, use the following form:
382385

@@ -394,6 +397,44 @@ Replace ``<user>``, ``<pass>``, and ``<host>`` with the appropriate
394397
values for your situation and substitute or omit the :option:`--port <mongo --port>`
395398
as needed.
396399

400+
.. _example-connect-mongo-using-srv:
401+
402+
Connect to a Replica Set Using the DNS Seedlist Connection Format
403+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
404+
405+
.. versionadded:: 3.6
406+
407+
To connect to a replica set described using the
408+
:ref:`connections-dns-seedlist`, use the :option:`~mongo.--host` option
409+
to specify the connection string to the :binary:`~bin.mongo` shell. In
410+
the following example, the DNS configuration resembles:
411+
412+
.. code-block:: none
413+
414+
Record TTL Class Priority Weight Port Target
415+
_mongodb._tcp.server.example.com. 86400 IN SRV 0 5 27317 mongodb1.example.com.
416+
_mongodb._tcp.server.example.com. 86400 IN SRV 0 5 27017 mongodb2.example.com.
417+
418+
The TXT record for the DNS entry includes the ``replicaSet`` and ``authSource`` options:
419+
420+
.. code-block:: none
421+
422+
Record TTL Class Text
423+
server.example.com. 86400 IN TXT "replicaSet=rs0&authSource=admin"
424+
425+
The following command then connects the :binary:`~bin.mongo` shell to
426+
the replica set:
427+
428+
.. code-block:: none
429+
430+
mongo --host "mongodb+srv://server.example.com/?username=allison"
431+
432+
The :binary:`~bin.mongo` shell will automatically prompt you to provide
433+
the password for the user specified in the ``username`` option.
434+
435+
Execute JavaScript Against the :binary:`~bin.mongo` Shell
436+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
437+
397438
To execute a JavaScript file without evaluating the :file:`~/.mongorc.js`
398439
file before starting a shell session, use the following form:
399440

@@ -408,6 +449,9 @@ rather than provided on the command-line, use the following form:
408449

409450
mongo script-file.js -u <user> -p
410451

452+
Use :option:`--eval <mongo --eval>` to Print Query Results as JSON
453+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
454+
411455
To print return a query as :term:`JSON`, from the system prompt using
412456
the :option:`--eval <mongo --eval>` option, use the following form:
413457

source/release-notes/4.0.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,11 @@ Miscellaneous
932932
- Resolves ``localhost`` IP address as configured instead of assuming
933933
``127.0.0.1``.
934934

935+
- When using the :ref:`connections-dns-seedlist` to connect to the
936+
:binary:`~bin.mongo` shell with authentication, the
937+
:binary:`~bin.mongo` shell will now prompt the user to
938+
provide their password when starting up.
939+
935940
Changes Affecting Compatibility
936941
-------------------------------
937942

0 commit comments

Comments
 (0)