forked from mongodb/mongo-ruby-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby-driver-create-client.txt
More file actions
339 lines (255 loc) · 10.3 KB
/
Copy pathruby-driver-create-client.txt
File metadata and controls
339 lines (255 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
=================
Creating a Client
=================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Using ``Mongo::Client``
-----------------------
To start a Ruby driver connection, create a ``Mongo::Client`` object.
Provide a list of hosts and options or a connection URI to the
``Mongo::Client`` constructor. The client's oselected database
defaults to ``admin``.
To create a client to a standalone server, provide one host in the
seed list. Optionally, you can force the cluster topology to be
standalone without going through the auto-discovery steps.
.. code-block:: ruby
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'mydb')
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'mydb', :connect => :direct)
Mongo::Client.new('mongodb://127.0.0.1:27017/mydb')
.. _ruby-driver-connect-replica-set:
To connect to a :manual:`replica set</replication/>`,
pass one or more hosts and the replica set name.
The driver's auto-discovery feature finds all members of the replica
set if they are not all provided.
.. code-block:: ruby
Mongo::Client.new([ '127.0.0.1:27017', '127.0.0.1:27018' ], :database => 'mydb', replica_set: 'myapp')
Mongo::Client.new('mongodb://127.0.0.1:27017,127.0.0.1:27018/mydb?replicaSet=myapp')
.. _ruby-driver-connect-sharded-cluster:
To create a client to a :manual:`sharded cluster</sharding/>`,
pass one or more :manual:`mongos</reference/program/mongos/>`
hosts. The auto-discovery feature can determine that the
servers are ``mongos`` instances, but if you
would like to bypass the auto-discovery, pass the
``sharded`` option to the client.
.. code-block:: ruby
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'mydb')
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'mydb', :connect => :sharded)
Mongo::Client.new('mongodb://127.0.0.1:27017/mydb?connect=sharded')
.. _ruby-driver-client-options:
Client Options
--------------
A number of different options can be passed to a ``Mongo::Client`` to configure driver
behavior, either by providing them in the options hash to the constructor or by
providing them in the URI.
Since the URI options are required in camel case, which is not the Ruby standard, the
following table shows the option in the URI and its corresponding option if passed
to the constructor in Ruby.
.. note::
The options passed directly should be symbols.
The options are explained in detail in the :manual:`Connection URI reference
</reference/connection-string/>`.
.. note::
Options that are set in **milliseconds** in the URI are
represented as a ``float`` in Ruby and the units are **seconds**.
URI Options Conversions
-----------------------
.. list-table::
:header-rows: 1
:widths: 40 105
* - URI Option
- Ruby Option
* - replicaSet=String
- ``:replica_set => String``
* - connect=String
- ``:connect => Symbol``
* - ssl=Boolean
- ``:ssl => true|false``
* - connectTimeoutMS=Integer
- ``:connect_timeout => Float``
* - socketTimeoutMS=Integer
- ``:socket_timeout => Float``
* - serverSelectionTimeoutMS=Integer
- ``:server_selection_timeout => Float``
* - localThresholdMS=Integer
- ``:local_threshold => Float``
* - maxPoolSize=Integer
- ``:max_pool_size => Integer``
* - minPoolSize=Integer
- ``:min_pool_size => Integer``
* - waitQueueTimeoutMS=Integer
- ``:wait_queue_timeout => Float``
* - w=Integer|String
- ``{ :write => { :w => Integer|String }}``
* - wtimeoutMS=Integer
- ``{ :write => { :wtimeout => Float }}``
* - journal=Boolean
- ``{ :write => { :j => true|false }}``
* - fsync=Boolean
- ``{ :write => { :fsync => true|false }}``
* - readPreference=String
- ``{ :read => { :mode => Symbol }}``
* - readPreferenceTags=Strings
- ``{ :read => { :tag_sets => Array<String> }}``
* - authSource=String
- ``:auth_source => String``
* - authMechanism=String
- ``:auth_mech => Symbol``
* - authMechanismProperties=Strings
- ``{ :auth_mech_properties => { :service_realm => String, :canonicalize_host_name => true|false, :service_name => String } }``
Ruby Options
------------
.. list-table::
:header-rows: 1
:widths: 25 40 10 15
* - Option
- Description
- Type
- Default
* - ``:replica_set``
- When connecting to a replica set, this is the name of the set to
filter servers by.
- ``String``
- none
* - ``:ssl``
- Tell the client to connect to the servers via SSL.
- ``Boolean``
- false
* - ``:2out``
- The number of seconds to wait to establish a socket connection
before raising an exception.
- ``Float``
- 10 seconds
* - ``:socket_timeout``
- The number of seconds to wait for an operation to execute on a
socket before raising an exception.
- ``Float``
- 5 seconds
* - ``:max_pool_size``
- The maximum size of the connection pool for each server.
- ``Integer``
- 5
* - ``:min_pool_size``
- The minimum number of connections in the connection pool for each
server.
- ``Integer``
- 1
* - ``:wait_queue_timeout``
- The number of seconds to wait for a connection in the connection
pool to become available.
- ``Float``
- 1
* - ``:write``
- Specifies write concern options as a ``Hash``.
Keys in the hash can be ``:w``, ``:wtimeout``, ``:j``, ``:fsync``.
.. code-block:: ruby
{ :write => { :w => 2 } }
- ``Hash``
- ``{ :w => 1 }``
* - ``:read``
- Specifies the read preference mode and tag sets for selecting servers as a ``Hash``.
Keys in the hash are ``:mode`` and ``:tag_sets``.
.. code-block:: ruby
{ :read =>
{ :mode => :secondary,
:tag_sets => [ "berlin" ]
}
}
- ``Hash``
- ``{ :mode => :primary }``
* - ``:auth_source``
- Specifies the authentication source.
- ``String``
- For MongoDB 2.6 and later: **admin** if credentials are
supplied, otherwise the current database
* - ``:auth_mech``
- Specifies the authenticaion mechanism to use. Can be one of:
``:mongodb_cr``, ``:mongodb_x509``, ``:plain``, ``:scram``.
- ``Symbol``
- MongoDB 3.0 and later: ``:scram`` if user credentials
are supplied but an ``:auth_mech`` is not. 2.6 and earlier:
``:mongodb_cr``
* - ``:auth_mech_properties``
- Provides additional authentication mechanism properties.
- ``Hash``
- none
* - ``:user``
- The name of the user to authenticate with.
- ``String``
- none
* - ``:password``
- The password of the user to authenticate with.
- ``String``
- none
* - ``:connect``
- Overrides the auto-discovery feature of the driver and forces the cluster
topology to a specific type. Choices: ``:direct``,
``:replica_set`` or ``:sharded``.
- ``Symbol``
- none
* - ``:heartbeat_frequency``
- The number of seconds for the server monitors to refresh
server states asynchronously.
- ``Float``
- 10
* - ``:database``
- The name of the database to connect to.
- ``String``
- admin
* - ``:server_selection_timeout``
- The number of seconds to wait for an appropriate server to
be selected for an operation to be executed before raising an exception.
- ``Float``
- 30
* - ``:local_threshold``
- Specifies the maximum latency in seconds between the nearest
server and the servers that can be available for selection to operate on.
- ``Float``
- 0.015
Details on Timeout Options
--------------------------
``connect_timeout``
On initialization of a connection to a server, this setting is the
number of seconds to wait to connect before raising an exception.
This timeout is also used when monitor threads ping their servers.
The default is 10 seconds. See the `socket timeout for monitoring
specification <https://github.com/mongodb/specifications/blob/
master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#socket-timeout-for-monitoring
-is-connecttimeoutms>`_ for further explanation.
``socket_timeout``
The number of seconds to wait for an operation to
execute on a socket before raising an exception. It should take into
account network latency and operation duration. Defaults to 5 seconds.
See the `socket timeout for monitoring specification <https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#socket-timeout-for-monitoring-is-connecttimeoutms>`_
documentation for further information.
``server_selection_timeout``
The number of seconds to wait for the driver to find an appropriate server to
which an operation can be sent before raising an exception. Defaults to 30.
It should take the speed of :manual:`elections</core/replica-set-elections/>`
during a failover into account. See the
`serverSelectionTimeoutMS specification
<https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#serverselectiontimeoutms>`_
for further information.
``local_threshold``
The maximum latency in seconds between the nearest server and the servers that can be considered available to send an
operation to. Defaults to 0.015.
.. note::
This is not the latency window between the driver and a server, but
rather the latency between the nearest server and other servers. See
`the localThresholdMS specification
<https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#localthresholdms>`_.
``wait_queue_timeout``
The number of seconds to wait for a connection in the connection pool to
become available. You should consider increasing this
number if you are seeing many ``Timeout`` errors while using many threads
or when operations are long-running. Defaults to 1 second.
``max_pool_size``
Maximum size of the connection pool for each server. Defaults to 5 connections.
``min_pool_size``
Minimum number of connections in the connection pool for each server.
Increase this number to create connections when the pool is
initialized and to reduce the overhead of creating new connections
later on. Defaults to 1.