|
| 1 | +.. _rust-connect-to-mongodb: |
| 2 | + |
| 3 | +================ |
| 4 | +Connection Guide |
| 5 | +================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +This guide shows you how to connect to a MongoDB instance or replica set |
| 14 | +deployment using the {+driver-short+}. |
| 15 | + |
| 16 | +.. _rust-connection-uri: |
| 17 | + |
| 18 | +-------------- |
| 19 | +Connection URI |
| 20 | +-------------- |
| 21 | + |
| 22 | +A **connection URI**, also known as a connection string, tells the |
| 23 | +driver how to connect to MongoDB and how to behave while connected. |
| 24 | + |
| 25 | +Parts of a Connection URI |
| 26 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 27 | + |
| 28 | +The following example explains each part of a sample connection URI: |
| 29 | + |
| 30 | +.. figure:: /includes/figures/connection_uri_parts.png |
| 31 | + :alt: Parts of a connection URI |
| 32 | + |
| 33 | +In this example, we use ``mongodb`` for the protocol, which specifies the |
| 34 | +:manual:`Standard Connection String Format |
| 35 | +</reference/connection-string/#std-label-connections-standard-connection-string-format>`. |
| 36 | +You can also use the :manual:`DNS Seed List Connection Format |
| 37 | +</reference/connection-string/#dns-seed-list-connection-format>` if you |
| 38 | +want more flexibility in your deployment and the ability to change the |
| 39 | +servers in rotation without reconfiguring clients. |
| 40 | + |
| 41 | +If you are using password-based authentication, the part of the |
| 42 | +connection string after the protocol contains your username and |
| 43 | +password. Replace the placeholder for ``user`` with your username and |
| 44 | +``pass`` with your password. If you are using an authentication |
| 45 | +mechanism that does not require a username and password, omit |
| 46 | +this part of the connection URI. |
| 47 | + |
| 48 | +The part of the connection string after the credentials specifies the |
| 49 | +hostname or IP address and port of your MongoDB instance. In the |
| 50 | +preceding example, we use ``sample.host`` as the hostname and ``27017`` |
| 51 | +as the port. Replace these values to point to your MongoDB instance. |
| 52 | + |
| 53 | +The last part of the connection string specifies connection and authentication |
| 54 | +options. In the example, we set two connection options: |
| 55 | +``maxPoolSize=20`` and ``w=majority``. |
| 56 | + |
| 57 | +.. TODO To learn more about connection |
| 58 | +.. options, see the :ref:`rust-connection-options` reference. |
| 59 | + |
| 60 | +MongoDB Client |
| 61 | +~~~~~~~~~~~~~~ |
| 62 | + |
| 63 | +To connect to MongoDB, you need to create a ``Client`` instance. A |
| 64 | +client manages your connections and runs database commands. |
| 65 | + |
| 66 | +.. tip:: Reuse Your Client |
| 67 | + |
| 68 | + We recommend that you reuse your client across sessions and operations. |
| 69 | + You can use the same ``Client`` instance to perform multiple tasks, instead of |
| 70 | + creating a new one each time. The ``Client`` type is safe for |
| 71 | + concurrent use by multiple threads or async tasks. |
| 72 | + |
| 73 | + .. TODO To learn more about how connection pools work in the driver, see |
| 74 | + .. the :ref:`FAQ page <rust-faq-connection-pool>`. |
| 75 | + |
| 76 | +You can create a client that uses your connection string and other |
| 77 | +client options by passing a ``ClientOptions`` object to the ``with_options()`` |
| 78 | +method. |
| 79 | + |
| 80 | +To specify your connection URI, pass it to the ``parse()`` |
| 81 | +method of ``ClientOptions``. To set any other |
| 82 | +options, set the relevant field of the ``ClientOptions`` struct. |
| 83 | + |
| 84 | +.. TODO To learn more about connection options, see the |
| 85 | +.. :ref:`Connection Options <rust-connection-options>` reference. |
| 86 | + |
| 87 | +To learn more about creating a client, see the API documentation for `Client |
| 88 | +<{+api+}/struct.Client.html>`__ and `with_options() <{+api+}/struct.Client.html#method.with_options>`__. |
| 89 | + |
| 90 | +You can set the {+stable-api+} version as an option to avoid |
| 91 | +breaking changes when you upgrade to a new server version. |
| 92 | + |
| 93 | +.. TODO To learn more about the {+stable-api+} feature, see the |
| 94 | +.. :ref:`{+stable-api+} page <rust-stable-api>`. |
| 95 | + |
| 96 | +.. _rust-atlas-connection-example: |
| 97 | + |
| 98 | +Connection Example |
| 99 | +~~~~~~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +The following code shows how you can create a client that uses an Atlas |
| 102 | +connection string and the {+stable-api+} version, connect to MongoDB, and |
| 103 | +verify that the connection is successful. Select from the |
| 104 | +:guilabel:`Sync` or :guilabel:`Async` tabs below for corresponding |
| 105 | +connection code samples. |
| 106 | + |
| 107 | +.. TODO To learn more about asynchronous and synchronous runtimes, see the |
| 108 | +.. :ref:`Runtimes <TODO link>` guide. |
| 109 | + |
| 110 | +.. tabs:: |
| 111 | + |
| 112 | + .. tab:: Async |
| 113 | + :tabid: rust-async |
| 114 | + |
| 115 | + .. literalinclude:: /includes/fundamentals/code-snippets/connection-async.rs |
| 116 | + :language: rust |
| 117 | + |
| 118 | + .. tab:: Sync |
| 119 | + :tabid: rust-sync |
| 120 | + |
| 121 | + .. literalinclude:: /includes/fundamentals/code-snippets/connection-sync.rs |
| 122 | + :language: rust |
| 123 | + |
| 124 | +.. .. tip:: |
| 125 | +.. |
| 126 | +.. TODO Follow the :ref:`Quick Start guide <rust-connect-to-your-cluster>` |
| 127 | +.. to retrieve your Atlas connection string. |
| 128 | + |
| 129 | +.. note:: |
| 130 | + |
| 131 | + To learn about connecting to Atlas Serverless, see the |
| 132 | + :ref:`Serverless Instance Limitations page |
| 133 | + <atlas-serverless-drivers>` to identify the minimum driver version |
| 134 | + you need. |
| 135 | + |
| 136 | +-------------------------------- |
| 137 | +Other Ways to Connect to MongoDB |
| 138 | +-------------------------------- |
| 139 | + |
| 140 | +If you need to connect to a single MongoDB server instance or replica set |
| 141 | +that is not hosted on Atlas, see the following sections to find out how to |
| 142 | +connect. |
| 143 | + |
| 144 | +Connect to a MongoDB Server on Your Local Machine |
| 145 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 146 | + |
| 147 | +.. include:: /includes/localhost-connection.rst |
| 148 | + |
| 149 | +Connect to a Replica Set |
| 150 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 151 | + |
| 152 | +A MongoDB replica set deployment is a group of connected instances, or nodes, |
| 153 | +where the nodes store the same set of data. This configuration of instances |
| 154 | +provides data redundancy and high data availability. |
| 155 | + |
| 156 | +To connect to a replica set deployment, specify the hostname and port numbers |
| 157 | +of each instance, separated by commas, and the replica set name as the value |
| 158 | +of the ``replicaSet`` parameter in the connection string. |
| 159 | + |
| 160 | +In the following example, the hostnames are ``host1``, ``host2``, and |
| 161 | +``host3``, and the port numbers are all ``27017``. The replica set name |
| 162 | +is ``myRS``. The following code shows the connection URI for the replica |
| 163 | +set with these specifications: |
| 164 | + |
| 165 | +.. code-block:: none |
| 166 | + |
| 167 | + mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRS |
| 168 | + |
| 169 | +When connecting to a replica set, the driver takes the following actions by default: |
| 170 | + |
| 171 | +- Discovers all replica set members when given the address of any one member. |
| 172 | +- Dispatches operations to the appropriate member, such as instructions |
| 173 | + to write against the **primary** node. To learn more about the replica |
| 174 | + set primary, see :manual:`Replica Set Primary </core/replica-set-primary/>` in the Server manual. |
| 175 | + |
| 176 | +.. tip:: |
| 177 | + |
| 178 | + You only need to specify one host to connect to a replica set. |
| 179 | + However, to ensure connectivity when the specified host |
| 180 | + is unavailable, you should provide the full list of hosts. |
| 181 | + |
| 182 | +Direct Connection |
| 183 | +````````````````` |
| 184 | + |
| 185 | +To force operations on the host designated in the connection URI, |
| 186 | +specify the ``directConnection`` option. Direct connections display the |
| 187 | +following behavior: |
| 188 | + |
| 189 | +- They don't support SRV strings. |
| 190 | +- They fail on writes when the specified host is not the primary. |
| 191 | +- They require you to :manual:`specify a secondary read preference |
| 192 | + </core/read-preference/#mongodb-readmode-secondary>` when the |
| 193 | + specified host isn't the primary member. To learn more about these |
| 194 | + replica set members, see :manual:`Replica Set Secondary Members |
| 195 | + </core/replica-set-secondary/>` in the Server manual. |
0 commit comments