@@ -9,5 +9,180 @@ Connect to a Deployment
9
9
.. contents:: On this page
10
10
:local:
11
11
:backlinks: none
12
- :depth: 1
12
+ :depth: 2
13
13
:class: singlecol
14
+
15
+
16
+ .. important::
17
+
18
+ Ensure that you have a running MongoDB deployment before attempting
19
+ to connect with ``mongosh``. The connection method you use
20
+ depends on your deployment type.
21
+
22
+ Local MongoDB Instance on Default Port
23
+ --------------------------------------
24
+
25
+ Run ``mongosh`` without any command-line options
26
+ to connect to a MongoDB instance running on your **localhost** with
27
+ **default port** 27017:
28
+
29
+ .. code-block:: sh
30
+
31
+ mongosh
32
+
33
+ This is equivalent to the following command:
34
+
35
+ .. code-block:: sh
36
+
37
+ mongosh "localhost:27017"
38
+
39
+ Local MongoDB Instance on a Non-Default Port
40
+ --------------------------------------------
41
+
42
+ To specify a port to connect to on localhost, use the
43
+ :manual:`connection string </reference/connection-string>` format.
44
+
45
+ .. example::
46
+
47
+ To connect to a MongoDB instance running on localhost with a
48
+ non-default port 28015:
49
+
50
+ .. code-block:: sh
51
+
52
+ mongosh "localhost:28015"
53
+
54
+ MongoDB Instance on a Remote Host
55
+ ---------------------------------
56
+
57
+ To specify a remote host and port, use a
58
+ :manual:`connection string </reference/connection-string>`.
59
+
60
+ .. example::
61
+
62
+ To connect to a MongoDB
63
+ instance running on a remote host machine on port 28015:
64
+
65
+ .. code-block:: sh
66
+
67
+ mongosh "mongodb://mongodb0.example.com:28015"
68
+
69
+ .. note:: Connecting to |service|
70
+
71
+ If your remote host is a |service-fullname| cluster, you can copy
72
+ your connection string from the |service| UI. To learn more, see
73
+ :atlas:`Connect to a Cluster <connect-to-cluster/#use-the-connect-dialog-to-connect-to-your-cluster>`.
74
+
75
+ Connection Options
76
+ ------------------
77
+
78
+ Connect with Authentication
79
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
80
+
81
+ To connect to a MongoDB instance requires authentication, use the
82
+ ``--username`` and ``--authenticationDatabase`` command-line options.
83
+ ``mongosh`` prompts you for a password, which it masks as you type.
84
+
85
+ .. example::
86
+
87
+ To connect to a remote MongoDB instance and authenticate against
88
+ the ``admin`` database as user ``alice``:
89
+
90
+ .. code-block:: sh
91
+
92
+ mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin
93
+
94
+ .. note::
95
+
96
+ To provide a password with the command instead of using the masked
97
+ prompt, you can use the ``--password`` option.
98
+
99
+ .. seealso::
100
+
101
+ - :manual:`Enable Access Control </tutorial/enable-authentication/>`
102
+ to enforce authentication.
103
+ - Add :manual:`Database Users </core/security-users/>`
104
+ to provide authenticated access to a MongoDB instance.
105
+
106
+ Connect to a Replica Set
107
+ ~~~~~~~~~~~~~~~~~~~~~~~~
108
+
109
+ To connect to a replica set:
110
+
111
+ - If you are using the :manual:`DNS Seedlist Connection Format
112
+ </reference/connection-string/#dns-seedlist-connection-format>`, you
113
+ can include the ``+srv`` modifier in your connection string.
114
+
115
+ .. example::
116
+
117
+ .. code-block:: sh
118
+
119
+ mongosh "mongodb+srv://server.example.com/"
120
+
121
+ .. note::
122
+ Using the ``+srv`` connection string modifier automatically sets
123
+ the
124
+ :manual:`tls option </reference/connection-string/#urioption.tls>`
125
+ to ``true`` for the connection. You can
126
+ override this behavior by explicitly setting ``tls`` to ``false``.
127
+
128
+ - You can specify the replica set name and members in the
129
+ :manual:`connection string </reference/connection-string>`.
130
+
131
+ .. example::
132
+
133
+ To connect to a three-member replica set named ``replA``:
134
+
135
+ .. code-block:: sh
136
+
137
+ mongosh "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
138
+
139
+ Connect using TLS
140
+ ~~~~~~~~~~~~~~~~~
141
+
142
+ For ``tls`` connections:
143
+
144
+ - If you are using the :manual:`DNS Seedlist Connection Format
145
+ </reference/connection-string/#dns-seedlist-connection-format>`, the
146
+ ``+srv`` connection string modifier automatically sets
147
+ the ``tls`` option to ``true`` for the connection:
148
+
149
+ .. example::
150
+
151
+ To connect to a DNS seedlist-defined replica set with ``tls``
152
+ enabled:
153
+
154
+ .. code-block:: sh
155
+
156
+ mongosh "mongodb+srv://server.example.com/"
157
+
158
+ - You can use the
159
+ :manual:`tls option </reference/connection-string/#urioption.tls>` to
160
+ set ``tls=true`` in the
161
+ :manual:`connection string </reference/connection-string>`:
162
+
163
+ .. example::
164
+
165
+ To enable ``tls`` in the connection string:
166
+
167
+ .. code-block:: sh
168
+
169
+ mongosh "mongodb://mongodb0.example.com:28015/?tls=true"
170
+
171
+ - You can specify the ``--tls`` command-line option.
172
+
173
+ .. example::
174
+
175
+ To connect to a remote host with ``tls`` enabled:
176
+
177
+ .. code-block:: sh
178
+
179
+ mongosh "mongodb://mongodb0.example.com:28015" --tls
180
+
181
+ Disconnect from a Deployment
182
+ ----------------------------
183
+
184
+ To disconnect from a deployment and exit ``mongosh``, you can:
185
+
186
+ - Type ``.exit``, or
187
+
188
+ - Press ``Ctrl`` + ``C`` twice.
0 commit comments