Overview
このセクションでは、 Rustドライバーで使用できるMongoDB の接続オプションと認証オプションについて説明します。接続を構成するには、接続 URI ( 接続文字列とも呼ばれます)を使用するか、ClientOptions の作成時にClient インスタンスにフィールドを設定します。
接続 URI の使用
接続 URI を Client::with_uri_str メソッドに渡す場合は、接続オプションを <name>=<value> ペアとして string に含めることができます。次の例では、値が 60000 である connectTimeoutMS オプションと、値が true である tls オプションを含む接続 URI を示しています。
use mongodb::Client; let uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"; let client = Client::with_uri_str(uri).await?;
ClientOptions の使用
接続 URI に接続オプションを含める代わりに、ClientOptionsインスタンスで接続オプションを設定できます。 ClientOptionsインスタンスを使用して接続を構成すると、実行時に設定を変更し、コンパイル中にエラーを検出しやすくなります。次の例は、ClientOptions を使用して接続オプションを設定する方法を示しています。
use std::time::Duration; use mongodb::{Client, options::ClientOptions}; let uri = "mongodb://<hostname>:<port>/"; let mut client_options = ClientOptions::parse(uri).await?; client_options.connect_timeout = Some(Duration::from_secs(60000)); //Set additional options on client_options here let client = Client::with_options(client_options)?;
接続オプション
次のセクションでは、 Rustドライバーで使用できる接続オプションについて説明します。接続オプションの完全なリストを表示するには、サーバー マニュアルガイドの「 接続文字列 オプション」セクションにアクセスしてください。
ネットワーク圧縮
オプション名 | 許容値 | デフォルト値 | 説明 |
|---|---|---|---|
compressors | 文字列のコンマ区切りリスト | なし | Specifies compressors that the Client instance uses in
the specified order.To learn more about network compression, see the
Network Compression guide. |
zlibCompressionLevel |
|
| Specifies the level field of the zlib compression if you
use that compressor. A higher level value results in more
compression, which is slower.Setting a value of 0 specifies no compression, and
setting a value of 9 specifies maximum compression.To learn more about network compression, see the
Network Compression guide. |
タイムアウト
オプション名 | 許容値 | デフォルト値 | 説明 |
|---|---|---|---|
connectTimeoutMS | Non-negative integer |
| サーバーに接続するときに、基礎の各 TCP ストリームに渡される接続タイムアウトをミリ秒単位で指定します。 |
サーバーの選択
オプション名 | 許容値 | デフォルト値 | 説明 |
|---|---|---|---|
heartbeatFrequencyMS | 500 以上の整数 |
| 各監視スレッドがサーバー チェックの実行間で待機する時間をミリ秒単位で指定します。 |
maxStalenessSeconds |
|
| Specifies the maximum lag, in seconds, behind the primary
node that a secondary node can be considered for the given
operation. The value of this option must be at least 90, or the
operation raises an error. A value of -1 means there is
no maximum lag. |
serverSelectionTimeoutMS | Non-negative integer |
|
|
認証
オプション名 | 許容値 | デフォルト値 | 説明 |
|---|---|---|---|
authMechanism | 文字列 | なし | 使用する認証メカニズムを指定します。 このオプションを指定しない場合、ドライバーはデフォルトの認証メカニズムを使用します。 Rust ドライバーでの認証の詳細については、「認証メカニズム 」に関するガイドを参照してください。 |
authMechanismProperties | 文字列 | なし |
|
authSource | 文字列 | 説明を参照してください | Specifies the database used to authenticate. This option defaults to admin for SCRAM-based
authentication mechanisms, $external for the
MONGODB-X509 mechanism, and the database name or
$external for the PLAIN mechanism. |
tls | ブール値 |
| Specifies the TLS configuration for the Client instance
to use in its connections with the server.By default, TLS is off. |
tlsAllowInvalidCertificates | ブール値 |
| Specifies whether the Client instance returns an error if the
server presents an invalid certificate.We recommend that you set this option to true only in
testing environments to avoid creating vulnerabilities in your
application. |
tlsCAFile | 文字列 | 説明を参照してください | Specifies the path to the certificate authority (CA) file that
the Client instance uses for TLS.If you do not specify this option, the driver uses the Mozilla
root certificates from the webpki-roots crate. |
tlsCertificateKeyFile | 文字列 | なし | Specifies the path to the certificate file that the Client
instance presents to the server to verify its identify.If you do not set this option, the Client instance does not
attempt to verify its identity to the server. |
tlsCertificateKeyFilePassword | 文字列 | なし | キーが暗号化されている場合は、 証明ファイル内の秘密キーを復号化するためのパスワードを指定します。 |
tlsInsecure | ブール値 |
| Specifies whether the Client instance returns an error if the
server presents an invalid certificate.We recommend that you set this option to true only in
testing environments to avoid creating vulnerabilities in your
application. |
読み取り操作と書込み操作
オプション名 | 許容値 | デフォルト値 | 説明 |
|---|---|---|---|
directConnection | ブール値 |
|
|
journal | ブール値 |
| 操作がディスク上のジャーナルに伝達されたことの確認を要求します。 |
localThresholdMS | Non-negative integer |
| Specifies the amount of time in milliseconds that the average
round-trip time between the driver and server can last
compared to the shortest round-trip time of all the suitable
servers. A value of 0 indicates that there is no latency window,
so only the server with the lowest average round-trip time
is eligible. |
readConcernLevel | 文字列 | なし | Specifies the default read concern for operations performed
on the Client instance.To learn more, see Read Concern in the Server manual. |
readPreference | 文字列 |
| Specifies how the driver routes a read operation to members
of a replica set. To learn more, see Read Preference in the Server manual. |
readPreferenceTags | キーと値のペアをコンマで区切ったリスト | なし | Specifies which replica set members are considered for
operations. Each instance of this key is a separate tag set. The driver checks each tag set until it finds one or more
servers with each tag in the set. |
replicaSet | 文字列 | なし |
|
retryReads | ブール値 |
| 操作が失敗した場合にクライアントが読み取り操作を再試行するかどうかを指定します。 |
w | 負でない整数または文字列 | なし | Requests acknowledgment that the operation has propagated to a
specific number or variety of servers. To learn more, see Write Concern in the Server manual. |
wTimeoutMS | Non-negative integer | タイムアウトなし | Specifies a time limit, in milliseconds, for the write
concern. If an operation has not propagated to the requested level
within the time limit, the driver raises an error. |
接続プール
オプション名 | 許容値 | デフォルト値 | 説明 |
|---|---|---|---|
maxIdleTimeMS | Non-negative integer |
| Specifies the amount of time in milliseconds that a
connection can remain idle in a connection pool before the
server closes it. A value of 0 indicates that the client does not close
idle connections. |
maxPoolSize | Non-negative integer |
| Specifies the maximum number of connections that the
Client instance can create in a connection pool for a
given server.If you attempt an operation while the value of
maxPoolSize connections are checked out, the operation
waits until an in-progress operation finishes and the
connection returns to the pool. |
minPoolSize | Non-negative integer |
| Specifies the minimum number of connections available in a
server's connection pool at a given time. If fewer than minPoolSize connections are in the pool,
the server adds connections in the background up to the
value of minPoolSize. |
接続プールの詳細については、「 接続プールガイド 」を参照してください。
一般
オプション名 | 許容値 | デフォルト値 | 説明 |
|---|---|---|---|
appName | 文字列 | なし | Specifies the application name that the Client instance
sends to the server as part of the handshake.Specifying an appName can help you use the server logs
to determine which Client instance is connected to the
server. |
API ドキュメント
ClientOptionsRustドライバーの の詳細については、 ClientOptions のAPIドキュメントを参照してください。