@@ -21,12 +21,11 @@ Prerequisites
21
21
.. include:: /includes/fact-connect-prereqs.rst
22
22
23
23
24
- Local MongoDB Instance on Default Port
25
- --------------------------------------
24
+ Connect to a Local Deployment on the Default Port
25
+ -------------------------------------------------
26
26
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:
30
29
31
30
.. code-block:: sh
32
31
@@ -38,204 +37,207 @@ This is equivalent to the following command:
38
37
39
38
mongosh "mongodb://localhost:27017"
40
39
41
- Local MongoDB Instance on a Non-Default Port
42
- --------------------------------------------
40
+ Connect to a Local Deployment on a Non-Default Port
41
+ ---------------------------------------------------
43
42
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 :
45
44
46
- - A :manual:`connection string </reference/connection-string>`.
45
+ - A :manual:`connection string </reference/connection-string>` with the
46
+ chosen port
47
47
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
56
49
57
- - The command-line option ``--port``.
50
+ For example, the following commands connect to a deployment running on
51
+ localhost port 28015:
58
52
59
- .. example::
60
-
61
- To connect to a MongoDB instance running on localhost with a
62
- non-default port 28015:
53
+ .. code-block:: sh
63
54
64
- .. code-block:: sh
55
+ mongosh "mongodb://localhost:28015"
65
56
66
- mongosh --port 28015
57
+ .. code-block:: sh
67
58
68
- MongoDB Instance on a Remote Host
69
- ---------------------------------
59
+ mongosh --port 28015
70
60
71
- To specify a remote host and port, you can use:
61
+ Connect to a Deployment on a Remote Host
62
+ ----------------------------------------
72
63
73
- - A :manual:`connection string </reference/connection-string>`.
64
+ To specify a remote host and port, you can use either:
74
65
75
- .. example::
66
+ - A :manual:`connection string </reference/connection-string>` with the
67
+ chosen host and port.
76
68
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.
79
71
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:
81
74
82
- mongosh "mongodb://mongodb0.example.com:28015"
75
+ .. code-block:: sh
83
76
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"
89
78
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
93
80
94
- .. example::
81
+ mongosh --host mongodb0.example.com --port 28015
95
82
96
- To connect to a MongoDB
97
- instance running on a remote host on port 28015:
83
+ .. note:: Connect to |service-fullname|
98
84
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.
100
90
101
- mongosh --host mongodb0.example.com --port 28015
91
+ Specify Connection Options
92
+ --------------------------
102
93
103
- Connection Options
104
- ------------------
94
+ Specify different connection options to connect to different types of
95
+ deployments.
105
96
106
- Connect with Authentication
97
+ Connect With Authentication
107
98
~~~~~~~~~~~~~~~~~~~~~~~~~~~
108
99
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.
119
104
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:
121
107
122
- .. note::
108
+ .. code-block:: sh
123
109
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
+ </>`.
126
116
127
117
.. seealso::
128
118
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/>`.
133
124
134
125
Connect to a Replica Set
135
126
~~~~~~~~~~~~~~~~~~~~~~~~
136
127
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>`.
138
132
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.
142
135
143
- .. example::
136
+ Option 1: DNS Seedlist Format
137
+ `````````````````````````````
144
138
145
- .. code-block:: sh
139
+ To use the DNS seedlist connection format, include the ``+srv`` modifier
140
+ in your connection string.
146
141
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:
148
144
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/"
155
148
156
- - You can specify the replica set name and members in the
157
- :manual:`connection string </reference/connection-string>`.
149
+ .. note:: +srv TLS Behavior
158
150
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``.
160
154
161
- To connect to a three-member replica set named ``replA``:
155
+ Option 2: Specify Members in Connection String
156
+ ``````````````````````````````````````````````
162
157
163
- .. code-block:: sh
158
+ You can specify individual replica set members in the
159
+ :manual:`connection string </reference/connection-string>`.
164
160
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:
166
163
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
168
169
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:
172
173
173
174
- 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.
175
177
- The connection string contains a seed list with multiple hosts.
178
+ - The connection string already contains a ``directConnection``
179
+ parameter.
176
180
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
178
185
~~~~~~~~~~~~~~~~~
179
186
180
- For ``tls`` connections :
187
+ To connect to a deployment using TLS, you can either :
181
188
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.
186
193
187
- .. example::
194
+ For example, to connect to a DNS seedlist-defined replica set with
195
+ ``tls`` enabled, run the following command:
188
196
189
- To connect to a DNS seedlist-defined replica set with ``tls``
190
- enabled:
197
+ .. code-block:: sh
191
198
192
- .. code-block:: sh
199
+ mongosh "mongodb+srv://server.example.com/"
193
200
194
- mongosh "mongodb+srv://server.example.com/"
201
+ - Set the :option:`--tls <--tls>` option to ``true`` in the connection
202
+ string.
195
203
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:
200
206
201
- .. example::
207
+ .. code-block:: sh
202
208
203
- To enable ``tls`` in the connection string:
209
+ mongosh "mongodb://mongodb0.example.com:28015/?tls=true"
204
210
205
- .. code-block:: sh
211
+ - Specify the ``--tls`` command-line option.
206
212
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:
208
215
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
216
217
217
- mongosh "mongodb://mongodb0.example.com:28015" --tls
218
+ mongosh "mongodb://mongodb0.example.com:28015" --tls
218
219
219
220
Connect to a Specific Database
220
221
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221
222
222
223
To 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.
225
227
226
- .. example::
228
+ For example, to connect to a database called ``qa`` on localhost, run the
229
+ following command:
227
230
228
- The following connection string URI connects to database ``db1``.
229
-
230
- .. code-block:: sh
231
+ .. code-block:: sh
231
232
232
- mongosh "mongodb://localhost:27017/db1 "
233
+ mongosh "mongodb://localhost:27017/qa "
233
234
234
235
Connect to a Different Deployment
235
236
---------------------------------
236
237
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
239
241
deployment from within the |mdb-shell|.
240
242
241
243
To learn how to connect to a different deployment using these methods,
@@ -244,17 +246,17 @@ see :ref:`mdb-shell-open-new-connections-in-shell`.
244
246
Verify Current Connection
245
247
-------------------------
246
248
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 .
249
251
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.
253
254
254
255
Disconnect from a Deployment
255
256
----------------------------
256
257
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:
258
260
259
261
- Type ``.exit``, ``exit``, or ``exit()``.
260
262
@@ -267,17 +269,17 @@ To disconnect from a deployment and exit ``mongosh``, you can:
267
269
Limitations
268
270
-----------
269
271
270
- - :manual:`Kerberos authentication </core/kerberos/>` does not permit
272
+ - :manual:`Kerberos authentication </core/kerberos/>` does not allow
271
273
``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``
275
279
276
280
- ``mongosh`` currently only supports the ``zlib``
277
281
:manual:`compressor </core/wiredtiger/#compression>`. The following
278
282
compressors are not supported:
279
283
280
284
- ``zstd``
281
285
- ``snappy``
282
-
283
-
0 commit comments