Docs Menu

接続オプション

このガイドでは、接続、認証、ドライバーのオプションと、Lambda アプリケーションのデータベース接続構成でそれらを指定する方法を学習できます。 接続オプションは、データベース接続を管理する MongoDB PHP ライブラリに渡されます。

MongoDB PHP ライブラリの詳細については、 MongoDB PHP ライブラリのドキュメント を参照してください。

このガイドでは、次のトピックについて説明します。

次のセクションでは、構成ファイルに一般的な接続および認証オプションを追加する方法を学びます。

次のいずれかの方法を使用して、Lambda ウェブ アプリケーションのconfig/database.php構成ファイルで接続オプションまたは認証オプションを指定できます。

  • 設定と値を配列項目としてoptions配列項目に追加します。

  • dsn 配列項目で指定された接続stringに、設定と値をクエリstringパラメーターとして追加します。

options配列でオプションを指定するには、次の例に示すように、名前と値を配列アイテムとして追加します。

'connections' => [
'mongodb' => [
'dsn' => 'mongodb+srv://mongodb0.example.com/',
'driver' => 'mongodb',
'database' => 'sample_mflix',
'options' => [
'appName' => 'myLaravelApp',
'compressors' => 'zlib',
'zlibCompressionLevel' => 7,
],
],
],

接続stringのパラメーターとしてオプションを指定するには、次のクエリstring構文形式を使用します。

  • ホスト情報を パラメーターから区切るために、疑問符文字?を追加します。

  • オプションを追加するには、 <option>=<value>としてフォーマットします。

  • 各オプションと値のペアの間にアンパサンド文字&を挿入して、それらを区切ります。

次の設定例は、接続stringパラメーターの構文を示しています。

'dsn' => 'mongodb+srv://mongodb0.example.com/?appName=myLaravelApp&compressors=zlib',

次の表では、接続オプションと認証オプションの一覧とそのデフォルト値について説明しています。

オプション名
許容値
デフォルト値
説明

appName

文字列

なし

Specifies the application name that the MongoDB PHP Library sends the MongoDB deployment as part of the handshake.
Specifying appName can help you identify activities related to that application in the server logs.

authMechanism

文字列

なし

使用する認証メカニズムを指定します。 このオプションを指定しない場合、ドライバーはデフォルトの認証メカニズムを使用します。 詳細については、サーバー マニュアルの 「認証」 を参照してください。

authMechanismProperties

文字列

なし

authMechanismオプションで設定された認証メカニズムに関連するプロパティをさらに指定します。

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.

compressors

文字列のコンマ区切りリスト

なし

MongoDB PHP ライブラリが使用するデータコンプレッサーを指定して、指定された順序で MongoDB とアプリケーション間で渡されるネットワーク データの量を削減します。

connectTimeoutMS

Non-negative integer

10000 ( 10秒)

サーバーに接続するときに、基礎の各 TCP ストリームに渡される接続タイムアウトをミリ秒単位で指定します。

directConnection

ブール値

false

クラスター内のすべてのサーバーを検出して接続するのではなく、単一のホストに直接接続するかどうかを指定します。 この設定の詳細については、接続ガイドの「直接接続」を参照してください。

heartbeatFrequencyMS

500以上の整数

10000 ( 10秒)

各監視スレッドがサーバー チェックの実行間で待機する時間をミリ秒単位で指定します。

journal

ブール値

false

操作がディスク上のジャーナルに伝達されたことの確認を要求します。

localThresholdMS

Non-negative integer

15

Specifies the 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 no latency window, so only the server with the lowest average round-trip time is eligible.

maxIdleTimeMS

Non-negative integer

0

Specifies the 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.

maxStalenessSeconds

-1 、または90以上の任意の整数

-1

Specifies the maximum lag, in seconds, behind the primary node that a secondary node can be considered for the given operation.
This option's value must be at least 90, or the operation raises an error. A value of -1 means there is no maximum lag.

maxPoolSize

Non-negative integer

10

Specifies the maximum number of connections that the MongoDB PHP Library 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

0

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.

readConcernLevel

文字列

なし

MongoDB PHP ライブラリによって実行される操作のデフォルトの読み取り保証 (read concern) を指定します。 詳細については、サーバー マニュアルの「読み取り保証」を参照してください。

readPreference

文字列

primary

MongoDB PHP ライブラリが読み取り操作をレプリカセット メンバーにルーティングする方法を指定します。 詳細については、サーバー マニュアルの「読み込み設定」を参照してください。

readPreferenceTags

キーと値のペアをコンマで区切ったリスト

なし

どのレプリカセット ノードを操作の対象にするかを指定します。 このキーの各インスタンスは、個別のタグ セットです。 ドライバーは、各タグを持つ 1 つ以上のサーバーを見つけるまで、各タグセットをチェックします。

replicaSet

文字列

なし

MongoDB PHP ライブラリが接続するレプリカセットの名前を指定します。

retryReads

ブール値

true

操作が失敗した場合に MongoDB PHP ライブラリが読み取り操作を再試行するかどうかを指定します。

serverSelectionTimeoutMS

Non-negative integer

30000 ( 30秒)

MongoDB PHP ライブラリがタイムアウトする前に、操作のサーバーを選択するまでの待機時間をミリ秒単位で指定します。

tls

ブール値

false

Specifies the TLS configuration for the MongoDB PHP Library to use in its connections with the server.
By default, TLS is off.

tlsAllowInvalidCertificates

ブール値

false

Specifies whether the MongoDB PHP Library returns an error if the server presents an invalid certificate.
We recommend setting this option to true only in testing environments to avoid creating security vulnerabilities in your application.

tlsCAFile

文字列

説明を参照してください

Specifies the path to the certificate authority (CA) file that the MongoDB PHP Library 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 MongoDB PHP Library presents to the server to verify its identity.
If you do not set this option, the MongoDB PHP Library does not attempt to verify its identity with the server.

tlsInsecure

ブール値

false

Specifies whether the MongoDB PHP Library returns an error if the server presents an invalid certificate.
We recommend setting this option to true only in testing environments to avoid creating security vulnerabilities in your application.

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

タイムアウトなし

書込み保証 (write concern) の時間制限をミリ秒単位で指定します。 操作が時間制限内に要求されたレベルに伝播しなかった場合、MongoDB PHP ライブラリはエラーを発生させます。

zlibCompressionLevel

-1 以上9 以下の整数

-1

Specifies the level field of the zlib compression if you use that compressor.
Setting a value of -1 selects the default compression level (6).
Setting a value of 0 specifies no compression, and setting a value of 9 specifies maximum compression.

接続オプションの完全なリストを表示するには、サーバー マニュアルの『 接続文字列 』ガイドの「接続stringオプション 」セクションを参照してください。 ページの右側にあるSelect your languageドロップダウン メニューからPHPを選択します。

ドライバー オプションは、Lambel アプリケーションと MongoDB 間の接続とすべての操作を管理する MongoDB PHP ライブラリの動作を変更します。

Lambda Web アプリケーションのconfig/database.php構成ファイルでドライバー オプションを指定できます。 ドライバー オプションを追加するには、次の例に示すように、設定と値を配列項目としてdriver_options配列項目に追加します。

'connections' => [
'mongodb' => [
'dsn' => 'mongodb+srv://mongodb0.example.com/',
'driver' => 'mongodb',
'database' => 'sample_mflix',
'driver_options' => [
'serverApi' => 1,
'allow_invalid_hostname' => false,
],
],
]

ドライバー オプションのリストについては、MongoDB PHP ライブラリのドキュメントの「 $DriverOptions: 配列セクション 」を参照してください。