개요
이 섹션에서는 Ruby 운전자 에서 사용할 수 있는 MongoDB 연결 및 인증 옵션에 대해 설명합니다. 연결 URI(연결 문자열 이라고도 함)를 사용하거나 Mongo::Client 생성자에 인수를 전달하여 연결을 구성할 수 있습니다.
연결 URI 사용
연결 URI를 Mongo::Client 생성자에 전달하는 경우 문자열에 연결 옵션을 <name>=<value> 쌍으로 포함할 수 있습니다. 다음 예시에서 연결 URI에는 값이 60000인 connectTimeoutMS 옵션과 값이 true인 tls 옵션이 포함되어 있습니다.
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true" client = Mongo::Client.new(uri)
Mongo::Client 사용
연결 옵션을 연결 URI에 포함하는 대신 Mongo::Client 생성자에 인수로 전달할 수 있습니다. 이러한 방식으로 연결을 구성하면 런타임에 설정을 변경하고 컴파일 중에 오류를 포착하기가 더 쉬워집니다. 다음 예시 Mongo::Client 생성자를 사용하여 연결 옵션을 설정하다 방법을 보여 줍니다.
uri = "mongodb://<hostname>:<port>" client = Mongo::Client.new(uri, connect_timeout: 60000, ssl: true)
연결 옵션
다음 섹션에서는 Ruby 운전자 에서 사용할 수 있는 연결 옵션에 대해 설명합니다.
네트워크 압축
연결 옵션 | 설명 |
|---|---|
:compressors | A list of potential compressors to use, in order of preference.
The driver chooses the first compressor that is also supported
by the server. Currently the driver only supports zstd, snappy, and zlib.Data Type: Array<String>Default: none Client Example: compressors: ['snappy', 'zstd', 'zlib']Connection URI Example: compressors=snappy,zstd,zlib |
:zlib_compression_level | The Zlib compression level to use, if using compression.
This option accepts an integer value between -1 and 9:- -1: zlib uses its default compression level (usually 6).- 0: No compression. - 1: Fastest speed but lowest compression. - 9: Best compression but slowest speed. For more information, see Ruby's ZLib module
documentation. Data Type: IntegerDefault: NoneClient Example: zlib_compression_level: 3Connection URI Example: zlibCompressionLevel=3 |
시간 초과
연결 옵션 | 설명 |
|---|---|
:connect_timeout | The number of seconds to wait to establish a socket
connection before raising an exception. This
timeout is also used for SRV DNS record resolution. nil and 0 mean no timeout. Client creation will
fail with an error if an invalid timeout value
is passed (such as a negative value or a non-numeric value).Data Type: FloatDefault: 10.0Client Example: connect_timeout: 10.0Connection URI Example: connectTimeoutMS=10000 |
:timeout_ms | The number of milliseconds to wait for an operation to execute
before raising an exception. 0 means no timeout. Client creation will fail
with an error if an invalid timeout value is passed
(such as a negative value or a non-numeric value).Data Type: IntegerDefault: none Client Example: timeout_ms: 5000Connection URI Example: timeoutMS=5000 |
서버 선택
연결 옵션 | 설명 |
|---|---|
:load_balancing | Whether to expect to connect to a load balancer. Data Type: BooleanDefault: falseClient Example: load_balanced: { true }Connection URI Example: N/A |
:server_selection_timeout | The maximum amount of time, in seconds, the driver waits
for server selection to succeed before throwing an exception. Data Type: IntegerDefault: 30Client Example: server_selection_timeout: 30Connection URI Example: serverSelectionTimeoutMS=30000 |
서버 선택에 대한 자세한 내용은 서버 선택 가이드 참조하세요.
인증
연결 옵션 | 설명 |
|---|---|
:auth_mech | The mechanism that the Ruby driver uses to authenticate the application. Data Type: SymbolDefault: :scram256nil if user credentials are not supplied.Client Example: auth_mech: :scram256Connection URI Example: authMechanism=SCRAM-SHA-256 |
:auth_mech_properties | Options specific to the authentication mechanism. This option
isn't needed for all authentication mechanisms. Data Type: HashDefault: When you use the GSSAPI authentication mechanism, the default properties are {service_name: "mongodb"}.Otherwise, the default is nil.Client Example: auth_mech_properties: {aws_session_token: '12345'}Connection URI Example: authMechanismProperties=AWS_SESSION_TOKEN:12345 |
:auth_source | The database to authenticate against. Data Type: StringDefault: admin, if credentials are suppliedClient Example: auth_source: adminConnection URI Example: authSource=admin |
: user | The username for authentication. When this option is included
in a connection URI, you must
percent-encode it. Data Type: StringDefault: none Client Example: user: my+userConnection URI Example: username=my+user |
:password | The password for authentication. When this option is included
in a connection URI, you must
percent-encode it. Data Type: StringDefault: none Client Example: password: strong+passwordConnection URI Example: password=strong+password |
읽기 및 쓰기 작업
연결 옵션 | 설명 |
|---|---|
:replica_set | Specifies the name of the replica set to connect to. Data Type: StringDefault: none Client Example: replica_set: 'myRS'Connection URI Example: replicaSet=myRS |
:direct_connection | Whether to connect directly to the specified host Data Type: BooleanDefault: falseClient Example: direct_connection: trueConnection URI Example: directConnection=true |
:read | The read preference options. For more information,
see Read Preference in the MongoDB Server manual. Data Type: HashDefault: { mode: :primary }Client Example: read: { mode: :primary }Connection URI Example: readPreference=primary |
:read_concern | Specifies the read concern options. For more information, see
Read Concern in the MongoDB Server manual. Data Type: HashDefault: none Client Example: read: { level: :majority }Connection URI Example: readConcern=majority |
:write_concern | Specifies the client's write concern. For more
information, see Write Concern in
the MongoDB Server manual. Data Type: HashDefault: write_concern: { w: 1 }Client Example: write_concern: { w: 2 }Connection URI Example: w=2 |
:local_threshold | The latency window, in seconds, for a replica-set member's
eligibility. If a member's round trip ping takes longer
than the fastest server's round-trip ping time
plus this value, the server isn't eligible for selection. Data Type: FloatDefault: 0.015Client Example: local_threshold: 0.020Connection URI Example: localThresholdMS=20 |
연결 풀
연결 옵션 | 설명 |
|---|---|
:max_pool_size | The maximum number of concurrent connections that the pool maintains.
If the number of in-use connections to a server reaches the specified
value, the next request to that server waits until a connection becomes
available. Setting this option to 0 creates an unlimited connection pool.Data Type: IntegerDefault: 100Client Example: max_pool_size: 100Connection URI Example: maxPoolSize=100 |
:max_connecting | The maximum number of connections that each pool can establish
concurrently. Data Type: IntegerDefault: 2Client Example: max_connecting: 2Connection URI Example: maxConnecting=2 |
:min_pool_size | The minimum number of concurrent connections that the pool maintains. Data Type: IntegerDefault: 0Client Example: min_pool_size: 1Connection URI Example: minPoolSize=1 |
:max_idle_time | The maximum number of seconds that a connection can remain idle in
the pool. Data Type: IntegerDefault: 0 (no limit)Client Example: max_idle_time: 10Connection URI Example: maxIdleTimeMS=10000 |
연결 풀에 대한 자세한 내용은 연결 풀로 연결 관리 가이드를 참조하세요.
API 문서
Ruby 운전자 의 Mongo::Client 옵션에 대한 자세한 내용은 Mongo::Client에 대한 API 설명서를 참조하세요.