|
| 1 | +.. _pymongo-connection-options: |
| 2 | + |
| 3 | +================== |
| 4 | +Connection Options |
| 5 | +================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: connection string, URI, server, Atlas, settings, configure |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +This section describes the MongoDB connection and authentication options |
| 24 | +available in {+driver-short+}. You can configure your connection using either |
| 25 | +the connection URI or arguments to the ``MongoClient`` constructor. |
| 26 | + |
| 27 | +.. _pymongo-connection-uri: |
| 28 | + |
| 29 | +Using the Connection URI |
| 30 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 31 | + |
| 32 | +If you pass a connection URI to the ``MongoClient`` constructor, you can include |
| 33 | +connection options in the string as ``<name>=<value>`` pairs. In the following example, |
| 34 | +the connection URI contains the ``connect_timeout`` option with a value of ``60000`` |
| 35 | +and the ``tls`` option with a value of ``true``: |
| 36 | + |
| 37 | +.. literalinclude:: /includes/connect/local_connection_config.py |
| 38 | + :language: python |
| 39 | + :dedent: |
| 40 | + :start-after: # start local connection config |
| 41 | + :end-before: # end local connection config |
| 42 | + |
| 43 | +.. _pymongo-mongo-client-settings: |
| 44 | + |
| 45 | +Using a ``MongoClient`` |
| 46 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 47 | + |
| 48 | +You can pass connection options as arguments to the ``MongoClient`` constructor |
| 49 | +instead of including them in your connection URI. |
| 50 | +Configuring the connection this way makes it easier to |
| 51 | +change settings at runtime and helps you catch errors during compilation. |
| 52 | +The following example shows how to use the ``MongoClient`` constructor to set |
| 53 | +connection options: |
| 54 | + |
| 55 | +.. literalinclude:: /includes/connect/mongoclient_settings_config.py |
| 56 | + :language: python |
| 57 | + :dedent: |
| 58 | + :start-after: # start mongo client settings config |
| 59 | + :end-before: # end mongo client settings config |
| 60 | + |
| 61 | +Connection Options |
| 62 | +------------------ |
| 63 | + |
| 64 | +The following sections describe the connection options available in {+driver-short+}. |
| 65 | +If a ``MongoClient`` parameter maps to more than one |
| 66 | +option in the connection string, the **Connection URI Example** shows all |
| 67 | +relevant options. |
| 68 | + |
| 69 | +.. todo: .. note:: |
| 70 | + If you're using a query parameter for a time duration, the value must be in |
| 71 | + milliseconds. For example, to specify 60 seconds, use the value ``60000``. If you're |
| 72 | + using a ``MongoClientSettings`` object for a time duration, use the appropriate |
| 73 | + ``TimeSpan`` value. |
| 74 | + |
| 75 | +Network Compression |
| 76 | +~~~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +.. list-table:: |
| 79 | + :header-rows: 1 |
| 80 | + :widths: 30 70 |
| 81 | + |
| 82 | + * - Connection Option |
| 83 | + - Description |
| 84 | + |
| 85 | + * - **compressors** |
| 86 | + - | The preferred compression types, in order, for wire-protocol messages sent to |
| 87 | + | or received from the server. The driver uses the first of these compression types |
| 88 | + | that the server supports. |
| 89 | + | |
| 90 | + | **Data Type**: {+string-data-type+} |
| 91 | + | **Default**: ``None`` |
| 92 | + | **MongoClient Example**: ``compressors = "snappy,zstd,zlib"`` |
| 93 | + | **Connection URI Example**: ``compressors=snappy,zstd,zlib`` |
| 94 | + |
| 95 | + * - **zlibCompressionLevel** |
| 96 | + - | The compression level for zlib to use. This option accepts |
| 97 | + | an integer value between ``-1`` and ``9``: |
| 98 | + | |
| 99 | + | - **-1:** (Default). zlib uses its default compression level (usually ``6``). |
| 100 | + | - **0:** No compression. |
| 101 | + | - **1:** Fastest speed but lowest compression. |
| 102 | + | - **9:** Best compression but slowest speed. |
| 103 | + | |
| 104 | + | **Data Type**: {+int-data-type+} |
| 105 | + | **Default**: ``-1`` |
| 106 | + | **MongoClient Example**: ``zlibCompressionLevel = 3`` |
| 107 | + | **Connection URI Example**: ``zlibCompressionLevel=3`` |
| 108 | + |
| 109 | +Server Selection |
| 110 | +~~~~~~~~~~~~~~~~ |
| 111 | + |
| 112 | +.. list-table:: |
| 113 | + :header-rows: 1 |
| 114 | + :widths: 30 70 |
| 115 | + |
| 116 | + * - Connection Option |
| 117 | + - Description |
| 118 | + |
| 119 | + * - **server_selector** |
| 120 | + - | A user-defined Python function called by {+driver-short+} to choose the server |
| 121 | + | to run an operation against. For more information, see |
| 122 | + | :ref:`<pymongo-server-selection>`. |
| 123 | + | |
| 124 | + | **Data Type**: ``callable`` |
| 125 | + | **Default**: ``None`` |
| 126 | + | **MongoClient Example**: ``server_selector = your_function`` |
| 127 | + | **Connection URI Example**: N/A |
| 128 | + |
| 129 | +.. _secondary-reads: |
| 130 | + |
| 131 | +Secondary Reads |
| 132 | +--------------- |
| 133 | + |
| 134 | +By default, an instance of ``MongoClient`` sends queries to |
| 135 | +the primary member of the replica set. To use secondary members for queries instead, change |
| 136 | +the read preference as shown in the following example: |
| 137 | + |
| 138 | +.. code-block:: python |
| 139 | + |
| 140 | + >>> client = MongoClient( |
| 141 | + ... 'localhost:27017', |
| 142 | + ... replicaSet='foo', |
| 143 | + ... readPreference='secondaryPreferred') |
| 144 | + >>> client.read_preference |
| 145 | + SecondaryPreferred(tag_sets=None) |
| 146 | + |
| 147 | +Now the ``MongoClient`` sends all queries to the secondary members of the replica set. If there are |
| 148 | +no secondary members, the client uses the primary member as a fallback. If you have |
| 149 | +queries you would prefer to never send to the primary, you can specify that |
| 150 | +using the ``secondary`` read preference. |
| 151 | + |
| 152 | +.. _health-monitoring: |
| 153 | + |
| 154 | +Health Monitoring |
| 155 | +----------------- |
| 156 | + |
| 157 | +When you initialize a ``MongoClient``, it launches background threads to |
| 158 | +monitor the replica set for the following changes: |
| 159 | + |
| 160 | +- Health: Detect when a member goes down or comes up, or if a different member |
| 161 | + becomes primary. |
| 162 | +- Configuration: Detect when members are added or removed, and detect changes |
| 163 | + in members' tags. |
| 164 | +- Latency: Track a moving average of each member's ping time. |
| 165 | + |
| 166 | +Replica-set monitoring ensures that queries are continually routed to the proper |
| 167 | +members as the state of the replica set changes. |
| 168 | + |
| 169 | +.. _mongos-load-balancing: |
| 170 | + |
| 171 | +``mongos`` Load Balancing |
| 172 | +------------------------- |
| 173 | + |
| 174 | +You can configure an instance of ``~pymongo.mongo_client.MongoClient`` |
| 175 | +with a list of ``mongos`` server addresses: |
| 176 | + |
| 177 | +.. code-block:: python |
| 178 | + |
| 179 | + >> client = MongoClient('mongodb://host1,host2,host3') |
| 180 | + |
| 181 | +Each member of the list must be a single ``mongos`` server. Multi-homed and round |
| 182 | +robin DNS addresses are **not** supported. The client continuously |
| 183 | +monitors the availability of all ``mongos`` servers. It also monitors its |
| 184 | +network latency to each server. |
| 185 | + |
| 186 | +PyMongo distributes operations evenly among the set of ``mongos`` servers within its |
| 187 | +``localThresholdMS`` (similar to how it distributes reads to secondaries |
| 188 | +in a replica set). By default, the threshold is 15 ms. |
| 189 | + |
| 190 | +The server with the lowest latency, and all servers with latencies no more than |
| 191 | +``localThresholdMS`` beyond the server with the lowest latency, receive |
| 192 | +operations equally. For example, consider the following three ``mongos`` servers: |
| 193 | + |
| 194 | +- host1: 20 ms |
| 195 | +- host2: 35 ms |
| 196 | +- host3: 40 ms |
| 197 | + |
| 198 | +By default, the ``localThresholdMS`` is 15 ms, so PyMongo uses "host1" and "host2" |
| 199 | +evenly. It uses "host1" because its network latency to the driver is shortest. It |
| 200 | +uses "host2" because its latency is within 15 ms of the server with the lowest latency. |
| 201 | +PyMongo doesn't use "host3" because its latency is 20 ms beyond the server with the |
| 202 | +lowest latency. |
| 203 | + |
| 204 | +To ensure that all servers are within the threshold, set ``localThresholdMS`` to 30 |
| 205 | +ms as shown in the following example: |
| 206 | + |
| 207 | +.. code-block:: python |
| 208 | + |
| 209 | + >> client = MongoClient('mongodb://host1,host2,host3/?localThresholdMS=30') |
| 210 | + |
| 211 | +.. warning:: |
| 212 | + |
| 213 | + Do **not** connect PyMongo to a pool of ``mongos`` instances through a |
| 214 | + load balancer. A single socket connection must always route to the same |
| 215 | + ``mongos`` instance for proper cursor support. |
0 commit comments