Docs Menu
Docs Home
/ / /
PHP ライブラリ マニュアル
/

接続オプションの指定

項目一覧

  • Overview
  • 接続オプションを設定する
  • 接続 URI の使用
  • MongoDB $ Client オブジェクトの使用
  • 接続 URI オプション
  • レプリカセット オプション
  • 接続オプション
  • 書込み保証(write concern)オプション
  • 読み取り保証のオプション
  • 設定オプションを読む
  • 認証オプション
  • サーバーの選択と検出オプション
  • その他の設定
  • API ドキュメント

このページでは、 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 に含める前に、その値を します。rawurlencode() RFC で指定されている URI構文に従ってこれらの値をエンコードするには、 メソッドを使用します。3986

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

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

  • RFC 3986

  • ローリングコード PHPマニュアルの

接続オプション
説明
Data Type: bool
MongoDB\Client Example: $uriOptions = ['directConnection' => true];
Connection URI Example: directConnection=true
Data Type: string
MongoDB\Client Example: $uriOptions = ['replicaSet' => 'replicaSetName'];
Connection URI Example: replicaSet=replicaSetName

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

接続オプション
説明
Data Type: int
MongoDB\Client Example: $uriOptions = ['connectTimeoutMS' => 2000];
Connection URI Example: connectTimeoutMS=2000
Data Type: int
MongoDB\Client Example: $uriOptions = ['socketTimeoutMS' => 20000];
Connection URI Example: socketTimeoutMS=20000
接続オプション
説明
Data Type: string
MongoDB\Client Example: $uriOptions = ['compressors' => 'snappy,zstd,zlib'];
Connection URI Example: compressors=snappy,zstd,zlib
Data Type: int
MongoDB\Client Example: $uriOptions = ['zlibCompressionLevel' => 3];
Connection URI Example: zlibCompressionLevel=3
接続オプション
説明
Data Type: string
MongoDB\Client Example: $uriOptions = ['w' => 'majority'];
Connection URI Example: w=majority
Data Type: int
MongoDB\Client Example: $uriOptions = ['wTimeoutMS' => 10000];
Connection URI Example: wTimeoutMS=10000
Data Type: bool
MongoDB\Client Example: $uriOptions = ['journal' => true];
Connection URI Example: journal=true
接続オプション
説明
Data Type: string
MongoDB\Client Example: $uriOptions = ['readConcernLevel' => 'majority'];
Connection URI Example: readConcernLevel=majority
接続オプション
説明
MongoDB\Client Example: $uriOptions = ['readPreference' => 'secondaryPreferred'];
Connection URI Example: readPreference=secondaryPreferred
Data Type: int
MongoDB\Client Example: $uriOptions = ['maxStalenessSeconds' => 30];
Connection URI Example: maxStalenessSeconds=30
Data Type: array
MongoDB\Client Example:
$uriOptions = [
'readPreferenceTags' => [
['dc' => 'ny', 'rack' => 'r1'],
[],
],
];

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

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

接続オプション
説明
Data Type: int
MongoDB\Client Example: $uriOptions = ['localThresholdMS' => 20];
Connection URI Example: localThresholdMS=20
Data Type: int
MongoDB\Client Example: $uriOptions = ['serverSelectionTimeoutMS' => 5000];
Connection URI Example: serverSelectionTimeoutMS=5000
Data Type: bool
MongoDB\Client Example: $uriOptions = ['serverSelectionTryOnce' => false];
Connection URI Example: serverSelectionTryOnce=false
Data Type: int
MongoDB\Client Example: $uriOptions = ['heartbeatFrequencyMS' => 30000];
Connection URI Example: heartbeatFrequencyMS=30000
Data Type: int
MongoDB\Client Example: $uriOptions = ['socketCheckIntervalMS' => 4000];
Connection URI Example: socketCheckIntervalMS=4000
接続オプション
説明
Data Type: string
MongoDB\Client Example: $uriOptions = ['appName' => 'myApp'];
Connection URI Example: appName=myApp
Data Type: bool
MongoDB\Client Example: $uriOptions = ['retryReads' => false];
Connection URI Example: retryReads=false
Data Type: bool
MongoDB\Client Example: $uriOptions = ['retryWrites' => false];
Connection URI Example: retryWrites=false
Data Type: bool
MongoDB\Client Example: $uriOptions = ['loadBalanced' => true];
Connection URI Example: loadBalanced=true
Data Type: int
MongoDB\Client Example: $uriOptions = ['srvMaxHosts' => 5];
Connection URI Example: srvMaxHosts=5

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

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

戻る

MongoDB クライアントの作成