Overview
このページでは、 PHPライブラリで使用できるMongoDBの接続オプションと認証オプションについて説明します。
接続オプションを設定する
接続 URI でオプションを指定するか、それを MongoDB\Clientコンストラクターに渡すことで接続を構成できます。
接続 URI の使用
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);
MongoDB $ Client オブジェクトの使用
接続 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の値が優先されます。
接続 URI オプション
次のセクションでは、 MongoDB. への接続に設定できるオプションについて説明します。 各接続オプションは、 MongoDB Serverマニュアル内の対応するエントリにリンクします。
重要
パーセント エンコーディング
接続オプションの値に特殊文字が含まれている場合は、接続 URI に含める前に、値を パーセント エンコードする 必要があります。RFC 3986 で指定されている URI構文に従ってこれらの値をエンコードするには、rawurlencode() メソッドを使用します。
接続オプションを$uriOptionsパラメーターに含める場合、パーセント エンコードしないでください。
詳細については、以下のリソースを参照してください。
レプリカセット オプション
接続オプション | 説明 |
|---|---|
Data Type: boolMongoDB\Client Example: $uriOptions = ['directConnection' => true];Connection URI Example: directConnection=true | |
Data Type: stringMongoDB\Client Example: $uriOptions = ['replicaSet' => 'replicaSetName'];Connection URI Example: replicaSet=replicaSetName |
接続オプション
TLS のオプション
PHPライブラリで利用できる TLS オプションの詳細については、 TLSページをご覧ください。
タイムアウト オプション
接続オプション | 説明 |
|---|---|
Data Type: intMongoDB\Client Example: $uriOptions = ['connectTimeoutMS' => 2000];Connection URI Example: connectTimeoutMS=2000 | |
Data Type: intMongoDB\Client Example: $uriOptions = ['socketTimeoutMS' => 20000];Connection URI Example: socketTimeoutMS=20000 |
圧縮オプション
接続オプション | 説明 |
|---|---|
Data Type: stringMongoDB\Client Example: $uriOptions = ['compressors' => 'snappy,zstd,zlib'];Connection URI Example: compressors=snappy,zstd,zlib | |
Data Type: intMongoDB\Client Example: $uriOptions = ['zlibCompressionLevel' => 3];Connection URI Example: zlibCompressionLevel=3 |
書込み保証(write concern)オプション
接続オプション | 説明 |
|---|---|
Data Type: stringMongoDB\Client Example: $uriOptions = ['w' => 'majority'];Connection URI Example: w=majority | |
Data Type: intMongoDB\Client Example: $uriOptions = ['wTimeoutMS' => 10000];Connection URI Example: wTimeoutMS=10000 | |
Data Type: boolMongoDB\Client Example: $uriOptions = ['journal' => true];Connection URI Example: journal=true |
readConcern オプション
接続オプション | 説明 |
|---|---|
Data Type: stringMongoDB\Client Example: $uriOptions = ['readConcernLevel' => 'majority'];Connection URI Example: readConcernLevel=majority |
設定オプションを読む
接続オプション | 説明 | ||||||
|---|---|---|---|---|---|---|---|
Data Type: MongoDB\Driver\ReadPreference MongoDB\Client Example: $uriOptions = ['readPreference' => 'secondaryPreferred'];Connection URI Example: readPreference=secondaryPreferred | |||||||
Data Type: intMongoDB\Client Example: $uriOptions = ['maxStalenessSeconds' => 30];Connection URI Example: maxStalenessSeconds=30 | |||||||
Data Type: arrayMongoDB\Client Example: 接続 URI の例: |
認証オプション
PHPライブラリで利用可能な認証オプションの詳細については、「認証メカニズム 」を参照してください。
サーバーの選択と検出オプション
接続オプション | 説明 |
|---|---|
Data Type: intMongoDB\Client Example: $uriOptions = ['localThresholdMS' => 20];Connection URI Example: localThresholdMS=20 | |
Data Type: intMongoDB\Client Example: $uriOptions = ['serverSelectionTimeoutMS' => 5000];Connection URI Example: serverSelectionTimeoutMS=5000 | |
Data Type: boolMongoDB\Client Example: $uriOptions = ['serverSelectionTryOnce' => false];Connection URI Example: serverSelectionTryOnce=false | |
Data Type: intMongoDB\Client Example: $uriOptions = ['heartbeatFrequencyMS' => 30000];Connection URI Example: heartbeatFrequencyMS=30000 | |
Data Type: intMongoDB\Client Example: $uriOptions = ['socketCheckIntervalMS' => 4000];Connection URI Example: socketCheckIntervalMS=4000 |
その他の設定
接続オプション | 説明 |
|---|---|
Data Type: stringMongoDB\Client Example: $uriOptions = ['appName' => 'myApp'];Connection URI Example: appName=myApp | |
Data Type: boolMongoDB\Client Example: $uriOptions = ['retryReads' => false];Connection URI Example: retryReads=false | |
Data Type: boolMongoDB\Client Example: $uriOptions = ['retryWrites' => false];Connection URI Example: retryWrites=false | |
Data Type: boolMongoDB\Client Example: $uriOptions = ['loadBalanced' => true];Connection URI Example: loadBalanced=true | |
Data Type: intMongoDB\Client Example: $uriOptions = ['srvMaxHosts' => 5];Connection URI Example: srvMaxHosts=5 |
API ドキュメント
MongoDB\Clientクラスの詳細については、次のPHPライブラリAPIドキュメントを参照してください。
MongoDB\Driver\ReadPreferenceクラスの詳細については、次のPHP拡張APIドキュメントを参照してください。