Docs 菜单
Docs 主页
/ / /
Java Reactive Streams 驱动程序
/

指定连接 URI 选项

在此页面上

  • Overview
  • 使用连接 URI
  • 连接选项
  • 身份验证
  • 连接配置
  • 连接池配置
  • CompressorConfiguration
  • 常规配置
  • 代理配置
  • 读取偏好配置
  • 副本集配置
  • 服务器配置
  • SRV 配置
  • 写关注配置

本节介绍MongoDB连接和身份验证选项,您可以在Java Reactive Streams驾驶员中作为连接 URI 的参数传递。

如果将连接 URI 传递给 MongoClient 构造函数,则可以将连接选项作为 <name>=<value> 对包含在string中。 在以下示例中,连接 URIconnectTimeoutMS 包含值为 的60000 选项和值为tlstrue 选项:

public class MongoConnection {
public static void main(String[] args) {
String uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true";
try (MongoClient client = MongoClients.create(uri)) {
// Use client here
}
}
}

以下部分描述了Java Reactive Streams驾驶员中可用的连接选项。 您可以将这些选项作为连接 URI 的参数传递,以指定客户端的行为。

连接选项
说明
authMechanism
Sets the mechanism the Java Reactive Streams driver uses to authenticate the
application. Valid options are defined in the Class ConnectionString API documentation.

Data Type: String
Default: "SCRAM-SHA-256" when connecting to MongoDB v4.0 or later.
"SCRAM-SHA-1" when connecting to MongoDB v3.0 through v3.13.
Connection URI Example: authMechanism=MONGODB-X509
authMechanismProperties
Sets options specific to the authentication mechanism.

Data Type: String
Default: ""
Connection URI Example: authMechanismProperties=AWS_SESSION_TOKEN:12435
authSource
Sets the source of authentication credentials. The source is typically a
database. This option is only respected when authMechanism is set
to MONGO-CR.

Data Type: String
Default: The database in the connection URI, or "admin" if none is provided
Connection URI Example: authSource=admin
用户名
Sets the username for authentication. When this option is included in a connection
URI, you must percent-escape it.

Data Type: String
Default: ""
Connection URI Example: username=my+user
密码
Sets the password for authentication. When this option is included in a connection
URI, you must percent-escape it.

Data Type: String
Default: ""
Connection URI Example: password=strong+password
连接选项
说明
connectTimeoutMS
Sets the number of milliseconds the connection can take before timing out. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver raises a timeout exception.

Data Type: Integer
Default: null
Connection URI Example: connectTimeoutMS=6000
maxIdleTimeMS
Sets the maximum idle time in milliseconds of a pooled connection. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver
closes the connection.

Data Type: Integer
Default: null
Connection URI Example: maxIdleTimeMS=6000
maxLifeTimeMS
Sets the maximum lifetime in milliseconds of a pooled connection. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver
closes the connection.

Data Type: Integer
Default: null
Connection URI Example: maxLifeTimeMS=6000
socketTimeoutMS
Sets the number of milliseconds a recieve on a socket can take before timing out. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver raises a timeout exception.

Data Type: Integer
Default: null
Connection URI Example: socketTimeoutMS=6000
ssl
Sets whether to connect using TLS/SSL.

Data Type: boolean
Default: null
Connection URI Example: ssl=true
sslInvalidHostNameAllowed
Allows invalid host names for TLS/SSL connections.

Data Type: boolean
Default: null
Connection URI Example: sslInvalidHostNameAllowed=true
TLS
Sets whether to connect using TLS. Supersedes the ssl option.

Data Type: boolean
Default: null
Connection URI Example: tls=true
tlsAllowInvalidHostnames
Allows invalid host names for TLS connections. Supersedes the
sslInvalidHostNameAllowed option.

Data Type: boolean
Default: null
Connection URI Example: tlsAllowInvalidHostnames=true
tlsInsecure
Enables insecure TLS connections when connecting using TLS.

Data Type: boolean
Default: null
Connection URI Example: tlsInsecure=true
连接选项
说明
maxConnecting
Sets the maximum number of connections a pool can establish concurrently.

Data Type: Integer
Default: null
Connection URI Example: maxConnecting=5
maxPoolSize
Sets the maximum number of connections in the connection pool.

Data Type: Integer
Default: null
Connection URI Example: maxPoolSize=5
minPoolSize
Sets the minimum number of connections in the connection pool.

Data Type: Integer
Default: null
Connection URI Example: minPoolSize=3
waitQueueTimeoutMS
Sets the maximum amount of time to wait in milliseconds before either
an in-use connection becomes available or a connection is created and
starts becoming established.

Data Type: Integer
Default: null
Connection URI Example: waitQueueTimeoutMS=6000
连接选项
说明
compressors
Sets preferred compression types, in order, for wire-protocol messages sent to
or received from the server. Compression types must be passed in as a
comma-separated list. The driver uses the first of these compression types
that the server supports.

Data Type: String
Default: null
Connection URI Example: compressors=snappy,zstd,zlib
zlibCompressionLevel
Sets the compression level for zlib to use. This option accepts
an integer value between -1 and 9:

- -1: (Default). zlib uses its default compression level (usually 6).
- 0: No compression.
- 1: Fastest speed but lowest compression.
- 9: Best compression but slowest speed.

Data Type: Integer
Default: -1
Connection URI Example: zLibCompressionLevel=3
连接选项
说明
directConnection
Sets whether the driver connects directly to the host.

Data Type: boolean
Default: null
Connection URI Example: directConnection=true
loadBalanced
Sets whether or not the driver is connecting to MongoDB using a load
balancer. If true, the driver assumes that it is connecting through a
load balancer.

Data Type: boolean
Default: null
Connection URI Example: directConnection=true
retryWrites
Sets whether or not the driver retries supported write operations if
they fail due to a network error.

Data Type: boolean
Default: True
Connection URI Example: retryWrites=true
retryReads
Sets whether or not the driver retries supported read operations if
they fail due to a network error.

Data Type: boolean
Default: True
Connection URI Example: retryReads=true
uuidRepresentation
Sets the mechanism the Java Reactive Streams driver uses to encode instances of
UUID. See the Uuid Representation API documentation for a list of all
valid options.

Data Type: String
Default: unspecified
Connection URI Example: uuidRepresentation=unspecified
连接选项
说明
proxyHost
Establishes the SOCKS5 proxy host to establish a connection through.
The value of the proxyHost can be a valid IPv4 address, IPv6
address, or a domain name.
Required if either proxyPassword, proxyUsername or proxyPort are specified.

Data Type: String
Default: null
Connection URI Example: proxyHost=example.com
代理端口
Sets the port number for the SOCKS5 proxy server. Must be a non-negative integer.

Data Type: Integer
Default: null
Connection URI Example: proxyPort=27
proxyUsername
Sets the username for authenticating with the proxy server. Required if proxyPassword is specified.

Data Type: String
Default: null
Connection URI Example: proxyUsername=example
proxyPassword
Sets the password for authenticating with the proxy server. Required if
proxyPassword is specified.

Data Type: String
Default: null
Connection URI Example: proxyPassword=password
连接选项
说明
readPreference
Sets the read preference for this connection. Enumerated values are:
primary, primaryPreferred, secondary, secondaryPreferred, and nearest.

Data Type: Enum
Default: primary
Connection URI Example: readPreference.primary
readPreferenceTags
Specifies a tag set as a comma-separated list of
colon-separated key-value pairs, e.g. dc:ny,rack:1.
Spaces are stripped from beginning and end of all keys and values. To
specify a list of tag sets, use multiple readPreferenceTags, separated
by semicolons. To learn more about readPreferenceTags, see the Class ConnectionString API documentation.

Data Type: String
Default: null
Connection URI Example: readPreferenceTags=dc:ny
maxStalenessSeconds
Sets the maximum staleness in seconds. To learn more about staleness,
see the Class ConnectionString API documentation.

Data Type: Integer
Default: null
Connection URI Example: maxStalenessSeconds=60
连接选项
说明
replicaSet
Specifies the name of the replica set to connect to.

Data Type: String
Default: null
Connection URI Example: replicaSet=replicaSet
连接选项
说明
appName
Sets the logical name of the application.
The application name may be used by the client to identify the
application to the server,
for use in server logs, slow query logs, and profile collection.

Data Type: String
Default: null
Connection URI Example: appName=name
heartbeatFrequencyMS
The frequency in milliseconds of the driver determining the current state of each server in the cluster.

Data Type: Integer
Default: null
Connection URI Example: heartbeatFrequencyMS=6000
localThresholdMS
Sets a ping time threshold for choosing among multiple MongoDB servers
to send a request. When this value is set, the driver will only send
that request to a server whose
ping time is less than or equal to the server with the fastest ping time plus the local threshold.

Data Type: Integer
Default: null
Connection URI Example: localThresholdMS=6000
serverMonitoringMode
Sets the server monitoring mode, which defines the monitoring protocol to use. The enumerated values are: stream, poll, and auto.

Data Type: Enum
Default: auto
Connection URI Example: serverMonitoringMode.auto
serverSelectionTimeoutMS
Sets how long the driver will wait for server selection to succeed before throwing an exception.

Data Type: Integer
Default: null
Connection URI Example: serverSelectionTimeoutMS=6000
连接选项
说明
srvServiceName
Sets the SRV service name. See
API documentation to learn more.

Data Type: String
Default: "mongodb"
Connection URI Example: srvServiceName=mongodb
SrvMaxHosts
Sets the maximum number of hosts from the SRV record to connect to.

Data Type: Integer
Default: null
Connection URI Example: srvMaxHosts=5
连接选项
说明
safe
Sets whether the driver ensures that all writes are acknowledged by the
MongoDB server, or else throws an exception. If false, the driver
does not ensure that all writes are acknowledged by the MongoDB server.

Data Type: boolean
Default: null
Connection URI Example: safe=true
journal
Sets whether the driver waits for the server to group commit to the
journal file on disk.

Data Type: boolean
Default: null
Connection URI Example: journal=true
w
If set, the driver adds { w : wValue } to all write commands. If set,
implies that safe=True.

Data Type: Integer or String
Default: null
Connection URI Example: w=60
wtimeoutMS
If set, The driver adds { wtimeout : ms } to all write commands. If
set, implies safe=true. This option is used in combination with w.

Data Type: Integer
Default: null
Connection URI Example: wtimeoutMS=6000

有关本节中连接选项的更多信息,请参阅数据库和集合指南。

后退

选择连接目标