指定连接选项
本页内容
概述
本页面描述了PHP库中可用的MongoDB连接和认证选项。
设置连接选项
您可以通过指定连接URI中的选项或将它们传递给MongoDB\Client
构造函数来配置您的连接。
使用连接URI
如果您将连接URI传递给 MongoDB\Client
构造函数,您可以在URI中以 <name>=<value>
对的形式包含连接选项。在下面的示例中,连接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对象
您可以将连接选项传递给MongoDB.Client
构造函数,而不是将它们包含在连接URI中。
以下示例展示了如何使用$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服务器手册中的相应条目。
重要
百分号编码
如果连接选项的值包含特殊字符,则必须百分号编码 将值在包含到连接 URI 之前进行百分号编码。您可以使用 rawurlencode()
方法根据 RFC 3986 规范中指定的 URI 语法对这些值进行编码。
在将连接选项包含在 $uriOptions
参数中时,不要对连接选项进行百分号编码。
要了解更多信息,请参阅以下资源
rawurlencode 在 PHP 手册中
副本集选项
连接选项 | 描述 |
---|---|
数据类型: bool MongoDB\Client 示例: $uriOptions = ['directConnection' => true]; 连接 URI 示例: directConnection=true | |
数据类型: string MongoDB.Client 示例: $uriOptions = ['replicaSet' => 'replicaSetName']; 连接 URI 示例: replicaSet=replicaSetName |
连接选项
TLS选项
要了解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\Client 示例: $uriOptions = ['readPreference' => 'secondaryPreferred']; 连接URI示例: readPreference=secondaryPreferred | |||||||
数据类型: int MongoDB\Client 示例: $uriOptions = ['maxStalenessSeconds' => 30]; 连接URI示例: maxStalenessSeconds=30 | |||||||
数据类型: array MongoDB\Client 示例:
连接URI示例: |
认证选项
有关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 |
其他配置
连接选项 | 描述 |
---|---|
数据类型: 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 |
API 文档
有关 MongoDB\Client
类的更多信息,请参阅以下 PHP 库 API 文档
有关 MongoDB\Driver\ReadPreference
类的更多信息,请参阅以下 PHP 扩展 API 文档