AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

接続オプションの指定

このページでは、 PHPライブラリで使用できるMongoDBの接続オプションと認証オプションについて説明します。

接続 URI でオプションを指定するか、それをMongoDB\Clientコンストラクターに渡すことで接続を構成できます。

MongoDB\Clientコンストラクターに接続 URI を渡す場合は、接続オプションを<name>=<value>ペアとして URI に含めることができます。 次の例では、接続 URI はtlsオプションをtrueに、 tlsCertificateKeyFileオプションを/path/to/file.pemに設定します。

// Replace the placeholders with your actual hostname, port, and path to the certificate key file
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/file.pem';
// Create a MongoDB client
$client = new MongoDB\Client($uri);

接続 URI に接続オプションを含める代わりに、 MongoDB\Clientコンストラクターに接続オプションを渡すことができます。

次の例は、 MongoDB\Clientコンストラクターの$uriOptionsパラメーターを使用して接続オプションを設定する方法を示しています。

// Replace the placeholders with your actual hostname and port
$uri = 'mongodb://<hostname>:<port>/';
// Set the connection options
// Replace the placeholder with the actual path to the certificate key file
$uriOptions = [
'tls' => true,
'tlsCertificateKeyFile' => '/path/to/file.pem',
];
// Create a MongoDB client with the URI and options
$client = new Client($uri, $uriOptions);

注意

$uriOptionsパラメータと接続 URI の両方にオプションを指定する場合は、 $uriOptionsの値が優先されます。

次のセクションでは、 MongoDB. への接続に設定できるオプションについて説明します。 各接続オプションは、 MongoDB Serverマニュアル内の対応するエントリにリンクします。

重要

パーセント エンコーディング

接続オプションの値に特殊文字が含まれている場合は、接続 URI に含める前に値をパーセント エンコードする必要があります。RFC 3986 で指定されている URI構文に従ってこれらの値をエンコードするには、rawurlencode() メソッドを使用します。

接続オプションを$uriOptionsパラメーターに含める場合、パーセント エンコードしないでください。

詳細については、以下のリソースを参照してください。

接続オプション
説明

データ型:bool
MongoDB$Client 例:$uriOptions = ['directConnection' => true];
接続 URI の例:directConnection=true

データ型:string
MongoDB$Client 例:$uriOptions = ['replicaSet' => 'replicaSetName'];
接続 URI の例:replicaSet=replicaSetName

PHPライブラリで利用できる TLS オプションの詳細については、 TLSページを参照してください。

接続オプション
説明

データ型:int
MongoDB$Client 例:$uriOptions = ['connectTimeoutMS' => 2000];
接続 URI の例:connectTimeoutMS=2000

データ型:int
MongoDB$Client 例:$uriOptions = ['socketTimeoutMS' => 20000];
接続 URI の例:socketTimeoutMS=20000

接続オプション
説明

データ型:string
MongoDB$Client 例:$uriOptions = ['compressors' => 'snappy,zstd,zlib'];
接続 URI の例:compressors=snappy,zstd,zlib

データ型:int
MongoDB$Client 例:$uriOptions = ['zlibCompressionLevel' => 3];
接続 URI の例:zlibCompressionLevel=3

接続オプション
説明

データ型:string
MongoDB$Client 例:$uriOptions = ['w' => 'majority'];
接続 URI の例:w=majority

データ型:int
MongoDB$Client 例:$uriOptions = ['wTimeoutMS' => 10000];
接続 URI の例:wTimeoutMS=10000

データ型:bool
MongoDB$Client 例:$uriOptions = ['journal' => true];
接続 URI の例:journal=true

接続オプション
説明

データ型:string
MongoDB$Client 例:$uriOptions = ['readConcernLevel' => 'majority'];
接続 URI の例:readConcernLevel=majority

接続オプション
説明

データ型: MongoDB$Driver\ReadPreference
MongoDB$Client の例:$uriOptions = ['readPreference' => 'secondaryPreferred'];
接続 URI の例:readPreference=secondaryPreferred

データ型:int
MongoDB$Client 例:$uriOptions = ['maxStalenessSeconds' => 30];
接続 URI の例:maxStalenessSeconds=30

データ型:array
MongoDB$Client 例:

$uriOptions = [
'readPreferenceTags' => [
['dc' => 'ny', 'rack' => 'r1'],
[],
],
];

接続 URI の例: readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=

PHPライブラリで利用可能な認証オプションの詳細については、「認証メカニズム 」を参照してください。

接続オプション
説明

データ型:int
MongoDB$Client 例:$uriOptions = ['localThresholdMS' => 20];
接続 URI の例:localThresholdMS=20

データ型:int
MongoDB$Client 例:$uriOptions = ['serverSelectionTimeoutMS' => 5000];
接続 URI の例:serverSelectionTimeoutMS=5000

データ型:bool
MongoDB$Client 例:$uriOptions = ['serverSelectionTryOnce' => false];
接続 URI の例:serverSelectionTryOnce=false

データ型:int
MongoDB$Client 例:$uriOptions = ['heartbeatFrequencyMS' => 30000];
接続 URI の例:heartbeatFrequencyMS=30000

データ型:int
MongoDB$Client 例:$uriOptions = ['socketCheckIntervalMS' => 4000];
接続 URI の例:socketCheckIntervalMS=4000

enableOverloadRetargeting

データ型:bool
MongoDB$Client 例:$uriOptions = ['enableOverloadRetargeting' => true];
接続 URI の例:enableOverloadRetargeting=true

maxAdaptiveRetries

データ型:int
MongoDB$Client 例:$uriOptions = ['maxAdaptiveRetries' => 3];
接続 URI の例:maxAdaptiveRetries=3

接続オプション
説明

データ型:string
MongoDB$Client 例:$uriOptions = ['appName' => 'myApp'];
接続 URI の例:appName=myApp

データ型:bool
MongoDB$Client 例:$uriOptions = ['retryReads' => false];
接続 URI の例:retryReads=false

データ型:bool
MongoDB$Client 例:$uriOptions = ['retryWrites' => false];
接続 URI の例:retryWrites=false

データ型:bool
MongoDB$Client 例:$uriOptions = ['loadBalanced' => true];
接続 URI の例:loadBalanced=true

データ型:int
MongoDB$Client 例:$uriOptions = ['srvMaxHosts' => 5];
接続 URI の例:srvMaxHosts=5

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

MongoDB\Driver\ReadPreferenceクラスの詳細については、次のPHP拡張APIドキュメントを参照してください。