Overview
本节介绍Rust驾驶员中可用的MongoDB连接和身份验证选项。您可以使用连接 URI(也称为 连接字符串)或在创建ClientOptions 时在Client 实例上设置字段来配置连接。
使用连接 URI
如果将连接 URI 传递给 Client::with_uri_str 方法,则可以将连接选项作为 <name>=<value> 对包含在字符串中。以下示例显示了一个连接 URI,其中包含值为 60000 的 connectTimeoutMS 选项和值为 true 的 tls 选项:
use mongodb::Client; let uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"; let client = Client::with_uri_str(uri).await?;
使用 ClientOptions
您可以在 ClientOptions实例上设立连接选项,而不是将它们包含在连接 URI 中。使用 ClientOptions实例配置连接时,您可以更轻松地在运行时更改设置并在编译期间捕获错误。以下示例展示了如何使用 ClientOptions设立连接选项:
use std::time::Duration; use mongodb::{Client, options::ClientOptions}; let uri = "mongodb://<hostname>:<port>/"; let mut client_options = ClientOptions::parse(uri).await?; client_options.connect_timeout = Some(Duration::from_secs(60000)); //Set additional options on client_options here let client = Client::with_options(client_options)?;
连接选项
以下部分介绍了Rust驾驶员中可用的连接选项。要查看连接选项的完整列表,请访问服务器手册指南中有关连接字符串的连接字符串选项部分。
网络压缩
选项名称 | 接受的值 | 默认值 | 说明 |
|---|---|---|---|
compressors | 以逗号分隔的字符串列表 | 无 | Specifies compressors that the Client instance uses in
the specified order.To learn more about network compression, see the
Network Compression guide. |
zlibCompressionLevel | 介于 |
| Specifies the level field of the zlib compression if you
use that compressor. A higher level value results in more
compression, which is slower.Setting a value of 0 specifies no compression, and
setting a value of 9 specifies maximum compression.To learn more about network compression, see the
Network Compression guide. |
超时
选项名称 | 接受的值 | 默认值 | 说明 |
|---|---|---|---|
connectTimeoutMS | Non-negative integer |
| 指定尝试连接到服务器时传递给每个底层 TCP 流的连接超时时间(以毫秒为单位)。 |
服务器选择
选项名称 | 接受的值 | 默认值 | 说明 |
|---|---|---|---|
heartbeatFrequencyMS | 大于或等于 500 的整数。 |
| 指定每个监控线程在执行服务器检查之间等待的时间(以毫秒为单位)。 |
maxStalenessSeconds |
|
| Specifies the maximum lag, in seconds, behind the primary
node that a secondary node can be considered for the given
operation. The value of this option must be at least 90, or the
operation raises an error. A value of -1 means there is
no maximum lag. |
serverSelectionTimeoutMS | Non-negative integer |
| 指定 |
身份验证
选项名称 | 接受的值 | 默认值 | 说明 |
|---|---|---|---|
authMechanism | 字符串 | 无 | 指定要使用的身份验证机制。 如果不指定此选项,驱动程序将使用默认的身份验证机制。 要了解有关 Rust 驱动程序中身份验证的更多信息,请参阅身份验证机制指南。 |
authMechanismProperties | 字符串 | 无 | 为 |
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. |
TLS | 布尔 |
| Specifies the TLS configuration for the Client instance
to use in its connections with the server.By default, TLS is off. |
tlsAllowInvalidCertificates | 布尔 |
| Specifies whether the Client instance returns an error if the
server presents an invalid certificate.We recommend that you set this option to true only in
testing environments to avoid creating vulnerabilities in your
application. |
tlsCAFile | 字符串 | 查看说明 | Specifies the path to the certificate authority (CA) file that
the Client instance 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 the Client
instance presents to the server to verify its identify.If you do not set this option, the Client instance does not
attempt to verify its identity to the server. |
tlsCertificateKeyFilePassword | 字符串 | 无 | 如果密钥已加密,请指定用于解密证书文件中的私钥的密码。 |
tlsInsecure | 布尔 |
| Specifies whether the Client instance returns an error if the
server presents an invalid certificate.We recommend that you set this option to true only in
testing environments to avoid creating vulnerabilities in your
application. |
读取和写入操作
选项名称 | 接受的值 | 默认值 | 说明 |
|---|---|---|---|
directConnection | 布尔 |
| 指定 |
journal | 布尔 |
| 请求确认操作已传播到磁盘上日志。 |
localThresholdMS | Non-negative integer |
| Specifies the amount of 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 that there is no latency window,
so only the server with the lowest average round-trip time
is eligible. |
readConcernLevel | 字符串 | 无 | Specifies the default read concern for operations performed
on the Client instance.To learn more, see Read Concern in the Server manual. |
readPreference | 字符串 |
| Specifies how the driver routes a read operation to members
of a replica set. To learn more, see Read Preference in the Server manual. |
readPreferenceTags | 以逗号分隔的键值对列表 | 无 | Specifies which replica set members are considered for
operations. Each instance of this key is a separate tag set. The driver checks each tag set until it finds one or more
servers with each tag in the set. |
replicaSet | 字符串 | 无 | 指定 |
retryReads | 布尔 |
| 指定在操作失败时客户端是否重试读取操作。 |
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 | 无超时 | Specifies a time limit, in milliseconds, for the write
concern. If an operation has not propagated to the requested level
within the time limit, the driver raises an error. |
连接池
选项名称 | 接受的值 | 默认值 | 说明 |
|---|---|---|---|
maxIdleTimeMS | Non-negative integer |
| Specifies the amount of 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. |
maxPoolSize | Non-negative integer |
| Specifies the maximum number of connections that the
Client instance 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 |
| 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. |
常规
选项名称 | 接受的值 | 默认值 | 说明 |
|---|---|---|---|
appName | 字符串 | 无 | Specifies the application name that the Client instance
sends to the server as part of the handshake.Specifying an appName can help you use the server logs
to determine which Client instance is connected to the
server. |
API 文档
ClientOptions有关Rust驾驶员的 的更多信息,请参阅 ClientOptions 的API文档。