连接选项
Overview
在本指南中,您可以了解连接、身份验证和驱动程序选项,以及如何在 Laravel 应用程序的数据库连接配置中指定这些选项。 连接选项将传递到 MongoDB PHP 库,该库管理数据库连接。
要了解有关 MongoDB PHP 库的更多信息,请参阅 MongoDB PHP 库文档。
本指南涵盖以下主题:
连接和身份验证选项
通过以下部分,了解如何将常用连接和身份验证选项添加到配置文件中:
添加连接和身份验证选项
您可以使用以下方法之一在 Laravel Web 应用程序的 config/database.php
配置文件中指定连接或身份验证选项:
将设置和值作为数组项添加到
options
数组项中。将设置和值作为查询string参数附加到
dsn
数组项中指定的连接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 | 字符串 | 无 | |
readPreference | 字符串 | primary | |
readPreferenceTags | 以逗号分隔的键值对列表 | 无 | 指定考虑对哪些副本集成员进行操作。 此键的每个实例都是一个单独的标签集。 驱动程序会检查每个标签集,直到找到带有每个标签的一个或多个服务器。 |
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 | 无超时 | 指定写关注的时间限制(以毫秒为单位)。 如果操作未在时限内传播到请求的级别,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. |
要查看连接选项的完整列表,请参阅 stringMongoDB Server手册中连接字符串指南的 连接 选项 部分。从页面右侧的 Select your language下拉菜单中选择PHP
。
驱动程序连接选项
驱动程序选项修改 MongoDB PHP 库的行为,该库管理 Laravel 应用程序和 MongoDB 之间的连接和所有操作。
您可以在 Laravel Web 应用程序的config/database.php
配置文件中指定驱动程序选项。 要添加驱动程序选项,请将设置和值作为数组项添加到driverOptions
数组项中,如下例所示:
'connections' => [ 'mongodb' => [ 'dsn' => 'mongodb+srv://mongodb0.example.com/', 'driver' => 'mongodb', 'database' => 'sample_mflix', 'driverOptions' => [ 'serverApi' => 1, 'allow_invalid_hostname' => false, ], ], ]
有关驱动程序选项列表,请参阅 MongoDB PHP 库文档的$driverOptions: array部分。