@@ -21,12 +21,11 @@ Prerequisites
2121.. include:: /includes/fact-connect-prereqs.rst
2222
2323
24- Local MongoDB Instance on Default Port
25- --------------------------------------
24+ Connect to a Local Deployment on the Default Port
25+ -------------------------------------------------
2626
27- Run ``mongosh`` without any command-line options
28- to connect to a MongoDB instance running on your **localhost** with
29- **default port** 27017:
27+ To connect to a MongoDB deployment running on **localhost** with
28+ **default port** 27017, run ``mongosh`` without any options:
3029
3130.. code-block:: sh
3231
@@ -38,204 +37,207 @@ This is equivalent to the following command:
3837
3938 mongosh "mongodb://localhost:27017"
4039
41- Local MongoDB Instance on a Non-Default Port
42- --------------------------------------------
40+ Connect to a Local Deployment on a Non-Default Port
41+ ---------------------------------------------------
4342
44- To specify a port to connect to on localhost, you can use:
43+ To specify a port to connect to on localhost, you can use either :
4544
46- - A :manual:`connection string </reference/connection-string>`.
45+ - A :manual:`connection string </reference/connection-string>` with the
46+ chosen port
4747
48- .. example::
49-
50- To connect to a MongoDB instance running on localhost with a
51- non-default port 28015:
52-
53- .. code-block:: sh
54-
55- mongosh "mongodb://localhost:28015"
48+ - The ``--port`` command-line option
5649
57- - The command-line option ``--port``.
50+ For example, the following commands connect to a deployment running on
51+ localhost port 28015:
5852
59- .. example::
60-
61- To connect to a MongoDB instance running on localhost with a
62- non-default port 28015:
53+ .. code-block:: sh
6354
64- .. code-block:: sh
55+ mongosh "mongodb://localhost:28015"
6556
66- mongosh --port 28015
57+ .. code-block:: sh
6758
68- MongoDB Instance on a Remote Host
69- ---------------------------------
59+ mongosh --port 28015
7060
71- To specify a remote host and port, you can use:
61+ Connect to a Deployment on a Remote Host
62+ ----------------------------------------
7263
73- - A :manual:`connection string </reference/connection-string>`.
64+ To specify a remote host and port, you can use either:
7465
75- .. example::
66+ - A :manual:`connection string </reference/connection-string>` with the
67+ chosen host and port.
7668
77- To connect to a MongoDB
78- instance running on a remote host on port 28015:
69+ - The ``--host`` and ``--port`` command-line options. If you omit the
70+ ``--port`` option, ``mongosh`` uses the default port 27017.
7971
80- .. code-block:: sh
72+ For example, the following commands connect to a MongoDB deployment
73+ running on host ``mongodb0.example.com`` and port 28015:
8174
82- mongosh "mongodb://mongodb0.example.com:28015"
75+ .. code-block:: sh
8376
84- .. note:: Connecting to |service|
85-
86- If your remote host is a |service-fullname| cluster, you can copy
87- your connection string from the |service| UI. To learn more, see
88- :atlas:`Connect to a Cluster </connect-to-cluster/#use-the-connect-dialog-to-connect-to-your-cluster>`.
77+ mongosh "mongodb://mongodb0.example.com:28015"
8978
90- - The command-line options ``--host`` and ``--port``. If you do not
91- include the ``--port`` option, ``mongosh`` uses the **default port**
92- 27017.
79+ .. code-block:: sh
9380
94- .. example::
81+ mongosh --host mongodb0.example.com --port 28015
9582
96- To connect to a MongoDB
97- instance running on a remote host on port 28015:
83+ .. note:: Connect to |service-fullname|
9884
99- .. code-block:: sh
85+ If your remote host is an |service| cluster, you can copy your
86+ connection string from the |service| UI. To learn more, see
87+ :atlas:`Connect to a Cluster
88+ </connect-to-cluster/#use-the-connect-dialog-to-connect-to-your-cluster>`
89+ in the Atlas documentation.
10090
101- mongosh --host mongodb0.example.com --port 28015
91+ Specify Connection Options
92+ --------------------------
10293
103- Connection Options
104- ------------------
94+ Specify different connection options to connect to different types of
95+ deployments.
10596
106- Connect with Authentication
97+ Connect With Authentication
10798~~~~~~~~~~~~~~~~~~~~~~~~~~~
10899
109- To connect to a MongoDB instance requires authentication, use the
110- ``--username`` and ``--authenticationDatabase`` command-line options.
111- ``mongosh`` prompts you for a password, which it masks as you type.
112-
113- .. example::
114-
115- To connect to a remote MongoDB instance and authenticate against
116- the ``admin`` database as user ``alice``:
117-
118- .. code-block:: sh
100+ To connect to a MongoDB deployment that requires authentication, use the
101+ :option:`--username <--username>` and :option:`--authenticationDatabase
102+ <--authenticationDatabase>` options. ``mongosh`` prompts you for a
103+ password, which it hides as you type.
119104
120- mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin
105+ For example, to authenticate as user ``alice`` on the ``admin``
106+ database, run the following command:
121107
122- .. note::
108+ .. code-block:: sh
123109
124- To provide a password with the command instead of using the masked
125- prompt, you can use the ``--password`` option.
110+ mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin
111+
112+ To provide a password as part of the connection command instead of using
113+ the prompt, use the :option:`--password <--password>` option. Use this
114+ option for programmatic usage of ``mongosh``, like a :driver:`driver
115+ </>`.
126116
127117.. seealso::
128118
129- - :manual:`Enable Access Control </tutorial/enable-authentication/>`
130- to enforce authentication.
131- - Add :manual:`Database Users </core/security-users/>`
132- to provide authenticated access to a MongoDB instance.
119+ - To enforce authentication on a deployment, see
120+ :manual:`Enable Access Control </tutorial/enable-authentication/>`.
121+
122+ - To provision access to a MongoDB deployment, see :manual:`Database
123+ Users </core/security-users/>`.
133124
134125Connect to a Replica Set
135126~~~~~~~~~~~~~~~~~~~~~~~~
136127
137- To connect to a replica set:
128+ To connect to a replica set, you can either:
129+
130+ - Use the :manual:`DNS Seedlist Connection Format
131+ </reference/connection-string/#dns-seedlist-connection-format>`.
138132
139- - If you are using the :manual:`DNS Seedlist Connection Format
140- </reference/connection-string/#dns-seedlist-connection-format>`, you
141- can include the ``+srv`` modifier in your connection string.
133+ - Explicitly specify the replica set name and members in the connection
134+ string.
142135
143- .. example::
136+ Option 1: DNS Seedlist Format
137+ `````````````````````````````
144138
145- .. code-block:: sh
139+ To use the DNS seedlist connection format, include the ``+srv`` modifier
140+ in your connection string.
146141
147- mongosh "mongodb+srv://server.example.com/"
142+ For example, to connect to a replica set on ``server.example.com``, run
143+ the following command:
148144
149- .. note::
150- Using the ``+srv`` connection string modifier automatically sets
151- the
152- :manual:`tls option </reference/connection-string/#urioption.tls>`
153- to ``true`` for the connection. You can
154- override this behavior by explicitly setting ``tls`` to ``false``.
145+ .. code-block:: sh
146+
147+ mongosh "mongodb+srv://server.example.com/"
155148
156- - You can specify the replica set name and members in the
157- :manual:`connection string </reference/connection-string>`.
149+ .. note:: +srv TLS Behavior
158150
159- .. example::
151+ When you use the ``+srv`` connection string modifier, MongoDB
152+ automatically sets the :option:`--tls <--tls>` connection option to
153+ ``true``. To override this behavior, set ``--tls`` to ``false``.
160154
161- To connect to a three-member replica set named ``replA``:
155+ Option 2: Specify Members in Connection String
156+ ``````````````````````````````````````````````
162157
163- .. code-block:: sh
158+ You can specify individual replica set members in the
159+ :manual:`connection string </reference/connection-string>`.
164160
165- mongosh "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
161+ For example, to connect to a three-member replica set named ``replA``,
162+ run the following command:
166163
167- .. note::
164+ .. code-block:: sh
165+
166+ mongosh "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
167+
168+ .. note:: directConnection Parameter Added Automatically
168169
169- ``mongosh`` adds the ``directConnection=true`` query parameter to the
170- connection string automatically unless at least one of the following is
171- true:
170+ When you specify individual replica set members in the connection
171+ string, ``mongosh`` automatically adds the ``directConnection=true``
172+ parameter, unless at least one of the following is true:
172173
173174 - The ``replicaSet`` query parameter is present in the connection string.
174- - The connection string uses the ``mongodb+srv://`` scheme.
175+ - The connection string uses the ``mongodb+srv://`` connection string
176+ format.
175177 - The connection string contains a seed list with multiple hosts.
178+ - The connection string already contains a ``directConnection``
179+ parameter.
176180
177- Connect using TLS
181+ When ``directConnection=true``, all operations are run on the host
182+ specified in the connection URI.
183+
184+ Connect Using TLS
178185~~~~~~~~~~~~~~~~~
179186
180- For ``tls`` connections :
187+ To connect to a deployment using TLS, you can either :
181188
182- - If you are using the :manual:`DNS Seedlist Connection Format
183- </reference/connection-string/#dns-seedlist-connection-format>`, the
184- ``+srv`` connection string modifier automatically sets
185- the ``tls`` option to ``true`` for the connection:
189+ - Use the :manual:`DNS Seedlist Connection Format
190+ </reference/connection-string/#dns-seedlist-connection-format>`. The
191+ ``+srv`` connection string modifier automatically sets the ``tls``
192+ option to ``true`` for the connection.
186193
187- .. example::
194+ For example, to connect to a DNS seedlist-defined replica set with
195+ ``tls`` enabled, run the following command:
188196
189- To connect to a DNS seedlist-defined replica set with ``tls``
190- enabled:
197+ .. code-block:: sh
191198
192- .. code-block:: sh
199+ mongosh "mongodb+srv://server.example.com/"
193200
194- mongosh "mongodb+srv://server.example.com/"
201+ - Set the :option:`--tls <--tls>` option to ``true`` in the connection
202+ string.
195203
196- - You can use the
197- :manual:`tls option </reference/connection-string/#urioption.tls>` to
198- set ``tls=true`` in the
199- :manual:`connection string </reference/connection-string>`:
204+ For example, to enable ``tls`` with a connection string option, run
205+ the following command:
200206
201- .. example::
207+ .. code-block:: sh
202208
203- To enable ``tls`` in the connection string:
209+ mongosh "mongodb://mongodb0.example.com:28015/?tls=true"
204210
205- .. code-block:: sh
211+ - Specify the ``--tls`` command-line option.
206212
207- mongosh "mongodb://mongodb0.example.com:28015/?tls=true"
213+ For example, to connect to a remote host with ``tls`` enabled, run the
214+ following command:
208215
209- - You can specify the ``--tls`` command-line option.
210-
211- .. example::
212-
213- To connect to a remote host with ``tls`` enabled:
214-
215- .. code-block:: sh
216+ .. code-block:: sh
216217
217- mongosh "mongodb://mongodb0.example.com:28015" --tls
218+ mongosh "mongodb://mongodb0.example.com:28015" --tls
218219
219220Connect to a Specific Database
220221~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221222
222223To connect to a specific database, specify a database in your
223- :manual:`connection string URI path </reference/connection-string/>` If
224- you do not specify a database in your URI path, you connect to a database named ``test``.
224+ :manual:`connection string URI path </reference/connection-string/>`. If
225+ you do not specify a database in your URI path, you connect to the
226+ ``test`` database.
225227
226- .. example::
228+ For example, to connect to a database called ``qa`` on localhost, run the
229+ following command:
227230
228- The following connection string URI connects to database ``db1``.
229-
230- .. code-block:: sh
231+ .. code-block:: sh
231232
232- mongosh "mongodb://localhost:27017/db1 "
233+ mongosh "mongodb://localhost:27017/qa "
233234
234235Connect to a Different Deployment
235236---------------------------------
236237
237- You can use the :method:`Mongo()` or the :manual:`connect()
238- </reference/method/connect/>` methods to connect to a different MongoDB
238+ If you are already connected to a deployment in the |mdb-shell|, you can
239+ use the :method:`Mongo()` or :manual:`connect()
240+ </reference/method/connect/>` method to connect to a different
239241deployment from within the |mdb-shell|.
240242
241243To learn how to connect to a different deployment using these methods,
@@ -244,17 +246,17 @@ see :ref:`mdb-shell-open-new-connections-in-shell`.
244246Verify Current Connection
245247-------------------------
246248
247- Use the :method:`db.getMongo()` method to verify your current database
248- connection .
249+ To verify your current database connection, use the
250+ :method:`db.getMongo()` method .
249251
250- The method returns the
251- :manual:`connection string URI </reference/connection-string/>` for
252- your current connection.
252+ The method returns the :manual:`connection string URI
253+ </reference/connection-string/>` for your current connection.
253254
254255Disconnect from a Deployment
255256----------------------------
256257
257- To disconnect from a deployment and exit ``mongosh``, you can:
258+ To disconnect from a deployment and exit ``mongosh``, perform one of the
259+ following actions:
258260
259261- Type ``.exit``, ``exit``, or ``exit()``.
260262
@@ -267,17 +269,17 @@ To disconnect from a deployment and exit ``mongosh``, you can:
267269Limitations
268270-----------
269271
270- - :manual:`Kerberos authentication </core/kerberos/>` does not permit
272+ - :manual:`Kerberos authentication </core/kerberos/>` does not allow
271273 ``authMechanismProperties=CANONICALIZE_HOST_NAME:true|false`` in the
272- connection string. Use one of:
273- ``authMechanismProperties=CANONICALIZE_HOST_NAME:forward|forwardAndReverse|none``
274- instead.
274+ connection string. Instead, use either:
275+
276+ - ``authMechanismProperties=CANONICALIZE_HOST_NAME:forward``
277+ - ``authMechanismProperties=CANONICALIZE_HOST_NAME:forwardAndReverse``
278+ - ``authMechanismProperties=CANONICALIZE_HOST_NAME:none``
275279
276280- ``mongosh`` currently only supports the ``zlib``
277281 :manual:`compressor </core/wiredtiger/#compression>`. The following
278282 compressors are not supported:
279283
280284 - ``zstd``
281285 - ``snappy``
282-
283-
0 commit comments