@@ -32,169 +32,13 @@ Connection Guide
32
32
Overview
33
33
--------
34
34
35
- In this guide, you can learn how to connect to a MongoDB instance or
36
- replica set deployment by using the {+driver-short+}.
35
+ Learn how to set up a connection and specify connection behavior from your
36
+ application to a MongoDB deployment using the {+driver-short+} in the following
37
+ sections:
37
38
38
- You can use the {+driver-short+} to connect to deployments hosted in the
39
- following environments:
39
+ - :ref:`golang-mongoclient`
40
+ - :ref:`golang-connection-options`
41
+ - :ref:`golang-connection-troubleshooting`
40
42
41
- .. include:: /includes/fact-environments.rst
42
-
43
- .. _golang-connection-uri:
44
-
45
- --------------
46
- Connection URI
47
- --------------
48
-
49
- A **connection URI**, also known as a connection string, tells the
50
- driver how to connect to MongoDB and how to behave while connected.
51
-
52
- Parts of a Connection URI
53
- ~~~~~~~~~~~~~~~~~~~~~~~~~
54
-
55
- The following image explains each part of a sample connection URI:
56
-
57
- .. figure:: /includes/figures/connection_uri_parts.png
58
- :alt: Each part of the connection string
59
-
60
- In this example, we use ``mongodb`` for the protocol, which specifies the
61
- :manual:`Standard Connection String Format
62
- </reference/connection-string/#std-label-connections-standard-connection-string-format>`.
63
- You can also use the :manual:`SRV Connection Format
64
- </reference/connection-string/#srv-connection-format>` if you
65
- want more flexibility of deployment and the ability to change the
66
- servers in rotation without reconfiguring clients.
67
-
68
- The next part of the connection string contains your database username and, if
69
- you are using password-based authentication, your password. Replace the value of
70
- ``user`` with your database username and ``pass`` with your password. If you are using an
71
- authentication mechanism that does not require a username and password, omit
72
- this part of the connection URI.
73
-
74
- The next part of the connection string specifies the hostname or IP address and
75
- port of your MongoDB instance. In the preceding example, we use ``sample.host``
76
- as the hostname and ``27017`` as the port. Replace these values to point to
77
- your MongoDB instance.
78
-
79
- The last part of the connection string specifies connection and authentication
80
- options. In the example, we set two connection options:
81
- ``maxPoolSize=20`` and ``w=majority``. To learn more about connection
82
- options, see the :ref:`golang-connection-options` guide.
83
-
84
- Connection Example
85
- ~~~~~~~~~~~~~~~~~~
86
-
87
- To connect to MongoDB, you must create a client. A client manages your
88
- connections and runs database commands.
89
-
90
- .. tip:: Reuse Your Client
91
-
92
- We recommend that you reuse your client across sessions and operations.
93
- You can use the same ``Client`` instance to perform multiple tasks, instead of
94
- creating a new one each time. The ``Client`` type is safe for
95
- concurrent use by multiple `goroutines
96
- <https://www.golang-book.com/books/intro/10>`__.
97
-
98
- .. TODO, Add back this line when Connection Pools page is made: To learn more about
99
- how connection pools work in the driver, see the :ref:`Connection Pools guide <golang-connection-pools>`.
100
-
101
- You can create a client that uses your connection string and other
102
- client options by passing a ``ClientOptions`` object to the ``Connect()``
103
- method.
104
-
105
- To specify your connection URI, pass it to the ``ApplyURI()``
106
- method, which returns a new ``ClientOptions`` instance. To set any other
107
- options, call the relevant helper method from the ``options`` package.
108
-
109
- To learn more about connection options, see the
110
- :ref:`Connection Options section <golang-connection-options>`. To learn
111
- more about creating a client, see the API documentation for `Client
112
- <{+api+}/mongo#Client>`__ and `Connect() <{+api+}/mongo#Connect>`__.
113
-
114
- You can set the {+stable-api+} version as an option to avoid
115
- breaking changes when you upgrade to a new server version. To
116
- learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page
117
- <golang-stable-api>`.
118
-
119
- The following code shows how you can create a client that uses an Atlas
120
- connection string and the {+stable-api+} version, connect to MongoDB, and
121
- verify that the connection is successful:
122
-
123
- .. _go-connection-example-code:
124
-
125
- .. literalinclude:: /includes/fundamentals/code-snippets/srv.go
126
- :language: go
127
-
128
- .. tip::
129
-
130
- Follow the :ref:`Quick Start guide <golang-connect-to-your-cluster>`
131
- to retrieve your Atlas connection string.
132
-
133
- .. note::
134
-
135
- To learn about connecting to Atlas Serverless, see the
136
- :ref:`Serverless Instance Limitations page
137
- <atlas-serverless-drivers>` to identify the minimum driver version
138
- required.
139
-
140
- --------------------------------
141
- Other Ways to Connect to MongoDB
142
- --------------------------------
143
-
144
- If you are connecting to a single MongoDB server instance or replica set
145
- that is not hosted on Atlas, see the following sections to find out how to
146
- connect.
147
-
148
- Connect to a MongoDB Server on Your Local Machine
149
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150
-
151
- .. include:: /includes/localhost-connection.rst
152
-
153
- To test whether you can connect to your server, replace the connection
154
- string with your localhost connection string in the preceding code example.
155
-
156
- Connect to a Replica Set
157
- ~~~~~~~~~~~~~~~~~~~~~~~~
158
-
159
- A MongoDB replica set deployment is a group of connected instances that
160
- store the same set of data. This configuration provides data
161
- redundancy and high data availability.
162
-
163
- To connect to a replica set deployment, specify the hostname and port numbers
164
- of each instance, separated by commas, and the replica set name as the value
165
- of the ``replicaSet`` parameter in the connection string. In the following
166
- example, the hostnames are ``host1``, ``host2``, and ``host3``, and the
167
- port numbers are all ``27017``. The replica set name is ``myRS``.
168
-
169
- .. code-block:: none
170
-
171
- mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS
172
-
173
- When connecting to a replica set, the driver takes the following actions by default:
174
-
175
- - Discovers all replica set members when given the address of any one member.
176
- - Dispatches operations to the appropriate member, such as instructions
177
- to write against the **primary**.
178
-
179
- .. tip::
180
-
181
- You can specify just one host to connect to a replica set.
182
- However, to ensure connectivity when the specified host
183
- is unavailable, provide the full list of hosts.
184
-
185
- Direct Connection
186
- `````````````````
187
-
188
- To force operations on the host designated in the connection URI,
189
- specify the ``directConnection`` option. Direct connections exhibit the
190
- following behavior:
191
-
192
- - They don't support SRV strings.
193
- - They fail on writes when the specified host is not the **primary**.
194
- - They require you to specify a :manual:`secondary read preference
195
- </core/read-preference/#mongodb-readmode-secondary>` when the
196
- specified host isn't the **primary** node.
197
-
198
- .. note:: Replica Set in Docker
199
-
200
- .. sharedinclude:: dbx/docker-replica-set.rst
43
+ For more information about authenticating with a MongoDB instance, see the
44
+ :ref:`golang-authentication` section.
0 commit comments