Connection String URI Format连接字符串URI格式
On this page本页内容
This document describes the URI formats for defining connections between applications and MongoDB instances in the official MongoDB Drivers. 本文档描述了在官方MongoDB驱动程序中定义应用程序和MongoDB实例之间连接的URI格式。For a list of drivers and links to driver documentation, see Drivers.有关驱动程序列表和驱动程序文档链接,请参阅驱动程序。
Connection String Formats连接字符串格式
You can specify the MongoDB connection string by using either:您可以使用以下任一项指定MongoDB连接字符串:
Standard Connection String Format标准连接字符串格式
This section describes the standard format of the MongoDB connection URI used to connect to a MongoDB deployment: standalone, replica set, or a sharded cluster.本节描述了用于连接到MongoDB部署的MongoDB连接URI的标准格式:独立、副本集或分片集群。
The standard URI connection scheme has the form:标准URI连接方案的形式如下:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
Examples实例
-
For a standalone:对于单机版:mongodb://mongodb0.example.com:27017
-
For a standalone that enforces access control:对于强制访问控制的单机版:mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
NoteIf the username or password includes the following characters:如果用户名或密码包含以下字符:$ : / ? # [ ] @
those characters must be converted using percent encoding.这些字符必须使用百分比编码进行转换。
For a replica set, specify the hostname(s) of the 对于副本集,指定副本集配置中列出的mongod instance(s) as listed in the replica set configuration.mongod实例的主机名。
For a replica set, include the 对于复制副本集,请包含replicaSet option.replicaSet选项。
-
For a replica set:对于复制副本集:mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl
-
For a replica set that enforces access control, include user credentials:对于强制执行访问控制的复制副本集,请包括用户凭据:mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl
NoteIf the username or password includes the following characters:如果用户名或密码包含以下字符:$ : / ? # [ ] @
those characters must be converted using percent encoding.这些字符必须使用百分比编码进行转换。
For a connection string to a sharded cluster, specify 对于到分片集群的连接字符串,请在连接字符串中指定mongos hosts in the connection string.mongos-hosts。
-
For a sharded cluster:对于分片集群:mongodb://mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017
-
For a sharded cluster that enforces access control, include user credentials:对于强制访问控制的分片集群,请包括用户凭据:mongodb://myDBReader:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin
NoteIf the username or password includes the following characters:如果用户名或密码包含以下字符:$ : / ? # [ ] @
those characters must be converted using percent encoding.这些字符必须使用百分比编码进行转换。
To connect to your deployment in MongoDB Atlas, use the SRV connection string provided in the Atlas UI when you click the Connect button. 要连接到MongoDB Atlas中的部署,请在单击“连接”按钮时使用Atlas UI中提供的SRV连接字符串。To learn more, see SRV Connection Format.要了解更多信息,请参阅SRV连接格式。
Your Atlas connection string might resemble the following example:Atlas连接字符串可能类似于以下示例:
mongosh "mongodb+srv://cluster0.example.mongodb.net myFirstDatabase" --username JohnDoe
To register a new Atlas account, start at the MongoDB Atlas registration page. 要注册新的Atlas帐户,请从MongoDB Atlas注册页面开始。To learn more, see Get Started with Atlas.要了解更多信息,请参阅Atlas入门。
Components组件
The standard URI connection string includes the following components:标准URI连接字符串包括以下组件:
mongodb:// | |
username:password@ | authSource. authSource验证用户。authSource is unspecified, the client will attempt to authenticate the user to the defaultauthdb. authSource,则客户端将尝试向defaultauthdb验证用户。defaultauthdb is unspecified, to the admin database. defaultauthdb,则返回到admin数据库。Note $ : / ? # [ ] @ authSource. authSource。 |
host[:port] | mongod instance (or mongos instance for a sharded cluster) is running. mongod实例(或分片集群的mongos实例)运行的主机(和可选端口号)。
27017 is used. 27017。 |
/defaultauthdb | username:password@ authentication credentials but the authSource option is unspecified.username:password@身份验证凭据,但authSource选项未指定,则要使用的身份验证数据库。authSource and defaultauthdb are unspecified, the client will attempt to authenticate the specified user to the admin database. authSource和defaultauthdb,则客户端将尝试向admin数据库验证指定的用户。 |
?<options> | <name>=<value> pairs. <name>=<value>对的查询字符串。/) between the last host and the question mark (?) that begins the string of options. /,则必须在最后一个主机和选项字符串开头的问号(?)之间指定一个斜线(/)。 |
SRV Connection FormatSRV连接格式
In addition to the standard connection format, MongoDB supports a DNS-constructed seed list. 除了标准的连接格式外,MongoDB还支持DNS构建的种子列表。Using DNS to construct the available servers list allows more flexibility of deployment and the ability to change the servers in rotation without reconfiguring clients.使用DNS构建可用服务器列表可以实现更大的部署灵活性,并能够在不重新配置客户端的情况下轮流更改服务器。
In order to leverage the DNS seed list, use a connection string prefix of 为了利用DNS种子列表,请使用mongodb+srv rather than the standard mongodb. mongodb+srv的连接字符串前缀,而不是标准的mongodb。The +srv indicates to the client that the hostname that follows corresponds to a DNS SRV record. The driver or mongosh will then query the DNS for the record to determine which hosts are running the mongod or mongos instances.+srv向客户端指示后面的主机名对应于DNS srv记录。然后,驱动程序或mongosh将查询DNS中的记录,以确定哪些主机正在运行mongod或mongos实例。
Use of the 使用+srv connection string modifier automatically sets the tls (or the equivalent ssl) option to true for the connection. +srv连接字符串修饰符会自动将连接的tls(或等效的ssl)选项设置为true。You can override this behavior by explicitly setting the 您可以通过在查询字符串中显式地将tls (or the equivalent ssl) option to false with tls=false (or ssl=false) in the query string.tls(或等效的ssl)选项设置为false,并使用tls=false(或ssl=false)来覆盖此行为。
When using the 使用+srv format, you must specify the hostname, domain, and top-level domain (TLD) in the following format:+srv格式时,必须按以下格式指定hostname、domain和顶级域(TLD):
<hostname>.<domain>.<TLD>. This table shows how the placeholders correspond to example values:此表显示占位符如何与示例值相对应:
<hostname> | server |
<domain> | example |
<TLD> | com |
<hostname>.<domain>.<TLD> | server.example.com |
This example shows a DNS seed list connection string that correctly uses the 此示例显示正确使用<hostname>.<domain>.<TLD> format.<hostname>.<domain>.<TLD>格式的DNS种子列表连接字符串。
mongodb+srv://server.example.com/
The corresponding DNS configuration might resemble:相应的DNS配置可能类似于:
Record TTL Class Priority Weight Port Target
_mongodb._tcp.server.example.com. 86400 IN SRV 0 5 27317 mongodb1.example.com.
_mongodb._tcp.server.example.com. 86400 IN SRV 0 5 27017 mongodb2.example.com.
Individual SRV records must be in 单个SRV记录必须位于_mongodb._tcp.<hostname>.<domain>.<TLD> format._mongodb._tcp.<hostname>.<domain>.<TLD>格式。
When a client connects to a member of the seed list, the client retrieves a list of replica set members it can connect to. 当客户端连接到种子列表的一个成员时,客户端会检索它可以连接到的副本集成员的列表。Clients often use DNS aliases in their seed lists which means the host may return a server list that differs from the original seed list. 客户端通常在其种子列表中使用DNS别名,这意味着主机可能会返回与原始种子列表不同的服务器列表。If this happens, clients will use the hostnames provided by the replica set rather than the hostnames listed in the seed list to ensure that replica set members can be reached via the hostnames in the resulting replica set config.如果发生这种情况,客户端将使用副本集提供的主机名,而不是种子列表中列出的主机名来确保可以通过生成的副本集配置中的主机名访问副本集成员。
The hostnames returned in SRV records must share the same parent domain (in this example, SRV记录中返回的主机名必须与给定主机名共享相同的父域(在本例中为example.com) as the given hostname. example.com)。If the parent domains and hostname do not match, you will not be able to connect.如果父域和主机名不匹配,您将无法连接。
This example shows a DNS seed list connection string that is missing the 此示例显示缺少<hostname>. <hostname>的DNS种子列表连接字符串。This is incorrect and raises an error.这是不正确的,并引发错误。
# This connection string is missing the <hostname> and raises a connection error
mongodb+srv://example.com/
Like the standard connection string, the DNS seed list connection string supports specifying options as a query string. 与标准连接字符串一样,DNS种子列表连接字符串支持将选项指定为查询字符串。With a DNS seed list connection string, you can also specify the following options via a TXT record:使用DNS种子列表连接字符串,您还可以通过TXT记录指定以下选项:
replicaSetauthSource
You may only specify one TXT record per 每个mongod instance. mongod实例只能指定一个TXT记录。If multiple TXT records appear in the DNS and/or if the TXT record contains an option other than 如果DNS中出现多个TXT记录和/或TXT记录包含replicaSet or authSource, the client will return an error.replicaSet或authSource以外的选项,则客户端将返回错误。
The TXT record for the server.example.com DNS entry would resemble:server.example.comDNS条目的TXT记录将类似于:
Record TTL Class Text
server.example.com. 86400 IN TXT "replicaSet=mySet&authSource=authDB"
Taken together, the DNS SRV records and the options specified in the TXT record resolve to the following standard format connection string:总之,DNS SRV记录和TXT记录中指定的选项解析为以下标准格式的连接字符串:
mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
You can override the options specified in a TXT record by passing the option in the query string. 您可以通过在查询字符串中传递选项来覆盖TXT记录中指定的选项。In the following example, the query string has provided an override for the 在以下示例中,查询字符串为上面DNS条目的TXT记录中配置的authSource option configured in the TXT record of the DNS entry above.authSource选项提供了覆盖。
mongodb+srv://server.example.com/?connectTimeoutMS=300000&authSource=aDifferentAuthDB
Given the override for the 给定authSource, the equivalent connection string in the standard format would be:authSource的覆盖,标准格式的等效连接字符串为:
mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?connectTimeoutMS=300000&replicaSet=mySet&authSource=aDifferentAuthDB
The 如果没有与连接字符串中标识的主机名对应的记录的可用DNS,则mongodb+srv option fails if there is no available DNS with records that correspond to the hostname identified in the connection string. mongodb+srv选项将失败。If you use the 如果使用+srv connection string modifier, the tls (or the equivalent ssl) option is set to true for the connection. +srv连接字符串修饰符,则连接的tls(或等效的ssl)选项将设置为true。You can override this behavior by explicitly setting the 您可以通过在查询字符串中显式地将tls (or the equivalent ssl) option to false with tls=false (or ssl=false) in the query string.tls(或等效的ssl)选项设置为false,并使用tls=false(或ssl=false)来覆盖此行为。
For an example that connects 有关使用DNS种子列表连接格式将mongosh to a replica set using the DNS seed list connection format, see mongosh Connection Options.mongosh连接到副本集的示例,请参阅mongosh连接选项。
Connection String Options连接字符串选项
This section lists all connection options.本节列出了所有连接选项。
Connection options are pairs in the following form: 连接选项是以下形式的对:name=value.name=value。
The option使用驱动程序时,选项nameis case insensitive when using a driver.name不区分大小写。The option使用nameis case insensitive when usingmongosh.mongosh时,选项name不区分大小写。Thevalueis always case sensitive.value始终区分大小写。
Separate options with the ampersand (i.e. 用与号(即&) character name1=value1&name2=value2. &)字符分隔选项name1=value1&name2=value2。In the following example, a connection includes the 在以下示例中,连接包括replicaSet and connectTimeoutMS options:replicaSet和connectTimeoutMS选项:
mongodb://db1.example.net:27017,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000
Semi-colon separator for connection string arguments连接字符串参数的分号分隔符
To provide backwards compatibility, drivers currently accept semi-colons (i.e. 为了提供向后兼容性,驱动程序目前接受分号(即;)作为选项分隔符。;) as option separators.
Replica Set Option复制副本集选项
The following connection string to a replica set named 以下连接字符串指向名为myRepl with members running on the specified hosts:myRepl的复制副本集,该复制副本集的成员在指定的主机上运行:
mongodb://db0.example.com:27017,db1.example.com:27017,db2.example.com:27017/?replicaSet=myRepl
replicaSet
| mongod is a member of a replica set. Set the replicaSet connection option to ensure consistent behavior across drivers.mongod是副本集的成员,则指定副本集的名称。设置replicaSet连接选项以确保驱动程序之间的行为一致。host[:port] component of the uri. host[:port]组件提供副本集成员的种子列表。 |
Connection Options连接选项
TLS OptionsTLS选项
The following connection string to a replica set includes 以下到副本集的连接字符串包括tls=true option (available starting in MongoDB 4.2):tls=true选项(从MongoDB 4.2开始可用):
mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&tls=true
Alternatively, you can also use the equivalent 或者,您也可以使用等效的ssl=true option:ssl=true选项:
mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&ssl=true
tls
|
mongosh shell specifies additional tls/ssl options from the command-line, use the --tls command-line option instead. mongosh shell从命令行指定了额外的tls/ssl选项,请改用--tls命令行选项。 |
ssl
|
mongosh shell specifies additional tls/ssl options from the command-line, use the --ssl command-line option instead. mongosh shell从命令行指定了额外的tls/ssl选项,请改用--ssl命令行选项。 |
tlsCertificateKeyFile
| .pem file that contains either the client's TLS/SSL X.509 certificate or the client's TLS/SSL certificate and key..pem文件的位置,该文件包含客户端的TLS/SSL X.509证书或客户端的TLS/ASL证书和键。mongod / mongos instance. mongod/mongos实例。mongod / mongos30 days of the mongod/mongos host system time. mongod/mongos主机系统时间后30天内过期,则在连接时记录警告。mongo shell. mongo shell。 |
tlsCertificateKeyFilePassword
| tlsCertificateKeyFile.tlsCertificateKeyFile进行解密的密码。mongo shell. mongo shell。 |
tlsCAFile
| .pem file that contains the root certificate chain from the Certificate Authority. .pem文件的位置,该文件包含来自证书颁发机构的根证书链。mongod / mongos instance.mongod/mongos实例提供的证书。mongo shell. |
tlsAllowInvalidCertificates
| mongod / mongos instancemongod/mongos实例提供的证书验证true to connect to MongoDB instances even if the server's present invalid certificates.true可连接到MongoDB实例,即使服务器提供的证书无效。mongo shell. mongo shell。Warning |
tlsAllowInvalidHostnames
| mongod / mongos instance.mongod/mongos实例提供的证书的主机名验证。true to connect to MongoDB instances even if the hostname in the server certificates do not match the server's host.true可连接到MongoDB实例,即使服务器证书中的主机名与服务器的主机不匹配。mongo shell. Warning |
tlsInsecure
| true to disable certificate validations. true可禁用证书验证。mongo shell. mongo shell。Warning |
Timeout Options超时选项
connectTimeoutMS
| |
socketTimeoutMS
|
Compression Options压缩选项
compressors
| mongod / mongos instance.mongod/mongos实例之间通信的网络压缩。"zlib,snappy" and the mongod specifies "snappy,zlib", messages between the client and the mongod uses zlib. "zlib,snappy",而mongod指定"snappy,zlib",则客户端和mongod之间的消息将使用zlib。Important mongosh supports the uri connection string option compressors. mongosh支持uri连接字符串选项compressors。 | ||||||||
zlibCompressionLevel
| network compression.-1 to 9:-1到9的整数值:
|
Connection Pool Options连接池选项
Most drivers implement some kind of connection pool handling. 大多数驱动程序实现某种连接池处理。Some drivers do not support connection pools. See your driver documentation for more information on the connection pooling implementation. 某些驱动程序不支持连接池。有关连接池实现的更多信息,请参阅驱动程序文档。These options allow applications to configure the connection pool when connecting to the MongoDB deployment.这些选项允许应用程序在连接到MongoDB部署时配置连接池。
maxPoolSize
| 100.100。 |
minPoolSize
| 0. 0。Note minPoolSize option is not supported by all drivers. minPoolSize选项。 |
maxIdleTimeMS
| |
waitQueueMultiple
| maxPoolSize value to, to provide the maximum number of threads allowed to wait for a connection to become available from the pool. maxPoolSize值乘以的一个数字,以提供允许等待连接从池中变为可用的最大线程数。 |
waitQueueTimeoutMS
|
Write Concern Options写入关注选项
Write concern describes the level of acknowledgment requested from MongoDB. 写入关注描述了MongoDB请求的确认级别。The write concern option is supported by the:以下各项支持写入问题选项:
MongoDB driversMongoDB驱动程序mongoshmongofilesmongoimportmongorestore
You can specify the write concern both in the connection string and as a parameter to methods like 您可以在连接字符串中指定写关注点,也可以将其作为insert or update. insert或update等方法的参数。If the write concern is specified in both places, the method parameter overrides the connection-string setting.如果在两个位置都指定了写问题,则方法参数将覆盖连接字符串设置。
The following connection string to a replica set specifies 以下到副本集的连接字符串指定"majority" write concern and a 5 second timeout using the wtimeoutMS write concern parameter:"majority"写入关注和使用wtimeoutMS写入关注参数的5秒超时:
mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&w=majority&wtimeoutMS=5000
w
| w Option. w选项。w option requests acknowledgement that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.w选项请求确认写入操作已传播到指定数量的mongod实例或具有指定标记的mongod实例。number, the string majority, or a tag set.number、majority字符串或标记集。w Option. w选项。 |
wtimeoutMS
| wtimeout. wtimeout。wtimeoutMSwtimeoutMS is 0, write operations will never time out. wtimeoutMS为0时,写入操作将永远不会超时。wtimeout. wtimeout。 |
journal
| j Option option. j选项。journal option requests acknowledgement from MongoDB that the write operation has been written to the journal. journal选项请求MongoDB确认写入操作已写入日志。j Option.j选项。journal to true, and specify a w value less than 1, journal prevails. journal设置为true,并指定一个小于1的w值,则以journal为准。 |
For more information, see Write Concern.有关更多信息,请参阅写入关注。
readConcern Options
For the WiredTiger storage engine, MongoDB 3.2 introduces the readConcern option for replica sets and replica set shards.对于WiredTiger存储引擎,MongoDB 3.2为副本集和副本集分片引入了readConcern选项。
Read Concern读取关注 allows clients to choose a level of isolation for their reads from replica sets.允许客户端为其从副本集中的读取选择隔离级别。
The following connection string to a replica set specifies 以下到副本集的连接字符串指定readConcernLevel=majority:readConcernLevel=majority:
mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&readConcernLevel=majority
readConcernLevel
| mongosh. mongosh。 |
For more information, see Read Concern.有关更多信息,请参阅阅取关注。
Read Preference Options读取偏好选项
Read preferences describe the behavior of read operations with regards to replica sets. 读取首选项描述了与副本集有关的读取操作的行为。These parameters allow you to specify read preferences on a per-connection basis in the connection string.这些参数允许您在连接字符串中按每个连接指定读取首选项。
To specify the hedged reads option using the drivers, refer to the drivers' read preference API.要使用驱动程序指定对冲读取选项,请参阅驱动程序的读取偏好API。
For example:例如:
The following connection string to a replica set specifies以下到副本集的连接字符串指定secondaryread preference mode and amaxStalenessSecondsvalue of 120 seconds:secondary读取首选项模式和120秒的maxStalenessSeconds值:mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&readPreference=secondary&maxStalenessSeconds=120
The following connection string to a sharded cluster specifies以下到分片集群的连接字符串指定secondaryread preference mode and amaxStalenessSecondsvalue of 120 seconds:secondary读取首选模式和120秒的maxStalenessSeconds值:mongodb://mongos1.example.com,mongos2.example.com/?readPreference=secondary&maxStalenessSeconds=120
The following connection string to a sharded cluster specifies以下到分片集群的连接字符串指定了secondaryread preference mode as well as threereadPreferenceTags:secondary读取首选项模式以及三个readPreferenceTags:mongodb://mongos1.example.com,mongos2.example.com/?readPreference=secondary&readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=dc:ny&readPreferenceTags=
Order matters when using multiple 使用多个readPreferenceTags. readPreferenceTags时,订单很重要。The 将按顺序尝试readPreferenceTags are tried in order until a match is found. readPreferenceTags,直到找到匹配项。Once found, that specification is used to find all eligible matching members and any remaining 一旦找到,该规范将用于查找所有符合条件的匹配成员,并忽略任何剩余的readPreferenceTags are ignored. readPreferenceTag。For details, see Order of Tag Matching.有关详细信息,请参阅标记匹配顺序。
readPreference
| primary. primary。mongo shell. cursor.readPref() and Mongo.setReadPref() instead. cursor.readPref()和Mongo.setReadPref()。 |
maxStalenessSeconds
| maxStalenessSeconds.maxStalenessSeconds。maxStalenessSeconds value is 90 seconds. maxStalenessSeconds的最小值为90秒。maxStalenessSeconds value of -1 as "no max staleness", the same as if maxStalenessSeconds is omitted. maxStalenessSeconds值-1视为“无最大过时”,与省略maxStalenessSeconds时相同。Important maxStalenessSeconds, all of the MongoDB instances in your deployment must be using MongoDB 3.4 or later. maxStalenessSeconds,部署中的所有MongoDB实例都必须使用MongoDB 3.4或更高版本。mongod / mongos will raise an error. mongod/mongos将引发错误。 |
readPreferenceTags
|
readPreferenceTags. readPreferenceTags。readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=.readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=。readPreferenceTags. readPreferenceTags时,顺序很重要。readPreferenceTags are tried in order until a match is found. For details, see Order of Tag Matching.readPreferenceTags,直到找到匹配项。有关详细信息,请参阅标记匹配顺序。mongo shell. cursor.readPref() and Mongo.setReadPref() instead. cursor.readPref()和Mongo.setReadPref()。 |
For more information, see Read preferences.有关详细信息,请参阅读取首选项。
Authentication Options身份验证选项
The following connection string to a replica set specifies the 以下到副本集的连接字符串指定到authSource to the admin database. That is, the user credentials are authenticated against the admin database.admin数据库的authSource。也就是说,用户凭据是根据admin数据库进行身份验证的。
mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&authSource=admin
If the username or password includes the following characters:如果用户名或密码包含以下字符:
$ : / ? # [ ] @
those characters must be converted using percent encoding.这些字符必须使用百分比编码进行转换。
authSource
| authSource is unspecified, authSource defaults to the defaultauthdb specified in the connection string. authSource,则authSource默认为连接字符串中指定的defaultauthdb。defaultauthdb is unspecified, then authSource defaults to admin.defaultauthdb,则authSource默认为admin。PLAIN (LDAP), GSSAPI (Kerberos), and MONGODB-AWS (IAM) authentication mechanisms require that authSource be set to $external, as these mechanisms delegate credential storage to external services.PLAIN(LDAP)、GSSAPI(Kerberos)和MONGODB-AWS(IAM)身份验证机制要求将authSource设置为$external,因为这些机制将凭据存储委派给外部服务。authSource values if no username is provided, either in the connection string or via the --username parameter. --username参数提供用户名,MongoDB将忽略authSource值。 |
authMechanism
Default: SCRAM-SHA-256 | authMechanism but provide user credentials, MongoDB attempts to use SCRAM-SHA-256. If this fails, it falls back to SCRAM-SHA-1. authMechanism,但提供了用户凭据,MongoDB会尝试使用SCRAM-SHA-256。如果失败,则返回SCRAM-SHA-1。
mongod and mongos instances provide GSSAPI (Kerberos) and PLAIN (LDAP) mechanisms.mongod和mongos实例提供GSSAPI(Kerberos)和PLAIN(LDAP)机制。MONGODB-X509, you must have TLS/SSL Enabled.MONGODB-X509,必须启用TLS/SSL。MONGODB-AWS, you must be connecting to a MongoDB AtlasMONGODB-AWS,您必须连接到MONGODB AtlasMONGODB-AWS authentication mechanism requires that the authSource be set to $external.MONGODB-AWS身份验证机制要求authSource设置为$external。MONGODB-AWS, provide your AWS access key ID as the username and the secret access key as the password. MONGODB-AWS时,请提供您的AWS访问键ID作为用户名,并提供机密访问键作为密码。AWS_SESSION_TOKEN authMechanismProperties value. AWS_session_tokenauthMechanismProperties值。Note $ : / ? # [ ] @ mongosh will use these environment variable values to authenticate; you do not need to specify them in the connection string.mongosh将使用这些环境变量值进行身份验证;您不需要在连接字符串中指定它们。MONGODB-AWS authentication mechanism using both a connection string and the environment variables method.MONGODB-AWS身份验证机制使用连接字符串和环境变量方法的示例,请参阅连接到Atlas集群。 |
authMechanismProperties
| authMechanism as a comma-separated list of colon-separated key-value pairs.authMechanism的属性指定为逗号分隔的键值对列表。
|
gssapiServiceName
| gssapiServiceNamemongodb for all clients and MongoDB instances. saslServiceName setting on a MongoDB instance, you must set gssapiServiceName to match that setting.saslServiceName设置,则必须将gssapiServiceName设置为与该设置匹配。gssapiServiceNameauthMechanismProperties=SERVICE_NAME:mongodb. authMechanismProperties=SERVICE_NAME:mongodb的不推荐使用的别名。 |
Server Selection and Discovery Options服务器选择和发现选项
MongoDB provides the following options to configure how MongoDB drivers and MongoDB提供了以下选项来配置MongoDB驱动程序和mongos instances select a server to which to direct read or write operations.mongos实例如何选择要直接进行读写操作的服务器。
localThresholdMS
| localThresholdMS. localThresholdMS。localThreshold alias when specifying the latency window size to mongos. mongos指定延迟窗口大小时,请使用localThreshold别名。 |
serverSelectionTimeoutMS
| |
serverSelectionTryOnce
| true, instructs the driver to scan the MongoDB deployment exactly once after server selection fails and then either select a server or raise an error. true,则指示驱动程序在服务器选择失败后扫描MongoDB部署一次,然后选择服务器或引发错误。false, the driver blocks and searches for a server up to the serverSelectionTimeoutMS value. Default: true.false,则驱动程序会阻止并搜索服务器,直到serverSelectionTimeoutMS值。默认值:true。mongos do not support serverSelectionTryOnce.mongos不支持serverSelectionTryOnce。 |
heartbeatFrequencyMS
| heartbeatFrequencyMSDefault:
mongos |
Miscellaneous Configuration其他配置
appName
|
appName connection option is available for:
|
retryReads
|
mongosh |
retryWrites
|
retryWrites. retryWrites的值。 |
uuidRepresentation
|
Note uuidRepresentation option. uuidRepresentation选项。 |
Examples实例
The following provide example URI strings for common connection targets.下面提供了常见连接目标的URI字符串示例。
Database Server Running Locally本地运行的数据库服务器
The following connects to a database server running locally on the default port:以下连接到在默认端口上本地运行的数据库服务器:
mongodb://localhost
admin Database数据库
The following connects and logs in to the 以下用户使用密码admin database as user sysop with the password moon:moon以用户sysop的身份连接并登录到admin数据库:
mongodb://sysop:moon@localhost
records Database数据库
The following connects and logs in to the 以下用户使用密码records database as user sysop with the password moon:moon作为用户sysop连接并登录到records数据库:
mongodb://sysop:moon@localhost/records
UNIX Domain Socket域套接字
Use a URL encoded connection string when connecting to a UNIX domain socket.连接到UNIX域套接字时,请使用URL编码的连接字符串。
The following connects to a UNIX domain socket with file path 以下连接到文件路径为/tmp/mongodb-27017.sock:/tmp/mongodb-27017.sock的UNIX域套接字:
mongodb://%2Ftmp%2Fmongodb-27017.sock
Replica Set with Members on Different Machines不同计算机上具有成员的副本集
The following connects to a replica set with two members, one on 下面连接到一个具有两个成员的复制集,一个在db1.example.net and the other on db2.example.net:db1.example.net上,另一个在db2.example.net上:
For a replica set, specify the hostname(s) of the 对于副本集,指定副本集配置中列出的mongod instance(s) as listed in the replica set configuration.mongod实例的主机名。
mongodb://db1.example.net,db2.example.com/?replicaSet=test
Replica Set with Members on localhostlocalhost具有成员的副本集
localhostThe following connects to a replica set with three members running on 以下连接到一个复制副本集,其中有三个成员在本地主机上的端口localhost on ports 27017, 27018, and 27019:27017、27018和27019上运行:
For a replica set, specify the hostname(s) of the 对于副本集,指定副本集配置中列出的mongod instance(s) as listed in the replica set configuration.mongod实例的主机名。
mongodb://localhost,localhost:27018,localhost:27019/?replicaSet=test
Replica Set with Read Distribution具有读取分发的副本集
The following connects to a replica set with three members and distributes reads to the secondaries:以下连接到一个有三个成员的复制副本集,并将读取分发到secondary:
For a replica set, specify the hostname(s) of the 对于副本集,指定副本集配置中列出的mongod instance(s) as listed in the replica set configuration.mongod实例的主机名。
mongodb://example1.com,example2.com,example3.com/?replicaSet=test&readPreference=secondary
Replica Set with a High Level of Write Concern具有高级别写问题的复制副本集
The following connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout.以下连接到一个具有写问题的复制集,该复制集配置为等待复制在大多数带有数据的投票成员中成功,超时两秒。
For a replica set, specify the hostname(s) of the 对于副本集,指定副本集配置中列出的mongod instance(s) as listed in the replica set configuration.mongod实例的主机名。
mongodb://example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=2000
Sharded Cluster分片集群
The following connects to a sharded cluster with three 以下连接到具有三个mongos instances:mongos实例的分片集群:
mongodb://router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/
MongoDB Atlas ClusterMongoDB Atlas集群
New in version 4.4. 4.4版新增。
The following connects to a MongoDB Atlas以下连接到MongoDB Atlas cluster which has been configured to support authentication via AWS IAM credentials:
集群,该集群已配置为支持通过AWS IAM凭据
进行身份验证:
mongosh 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS'
Connecting to Atlas using AWS IAM credentials in this manner uses the 以这种方式使用AWS IAM凭据连接到Atlas使用MONGODB-AWS authentication mechanism and the $external authSource, as shown in this example.MONGODB-AWS身份验证机制和$externalauthSource,如本例所示。
If using an AWS session token如果也使用AWS会话令牌, as well, provide it with the
AWS_SESSION_TOKEN authMechanismProperties value, as follows:,请为其提供
AWS_SESSION_TOKEN authMechanismProperties值,如下所示:
mongosh 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>'
If the AWS access key ID, the secret access key, or the session token include the following characters:如果AWS访问键ID、秘密访问键或会话令牌包括以下字符:
$ : / ? # [ ] @
those characters must be converted using percent encoding.这些字符必须使用百分比编码进行转换。
You may also set these credentials on your platform using standard AWS IAM environment variables您也可以使用标准的AWS IAM环境变量.
在您的平台上设置这些凭据。
当您使用mongosh checks for the following environment variables when you use the MONGODB-AWS authentication mechanism:MONGODB-AWS身份验证机制时,mongosh会检查以下环境变量:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKEN
If set, these credentials do not need to be specified in the connection string.如果设置了,则无需在连接字符串中指定这些凭据。
The following example sets these environment variables in the 以下示例在bash shell:bash shell中设置这些环境变量:
export AWS_ACCESS_KEY_ID='<aws access key id>'
export AWS_SECRET_ACCESS_KEY='<aws secret access key>'
export AWS_SESSION_TOKEN='<aws session token>'
Syntax for setting environment variables in other shells will be different. 在其他shell中设置环境变量的语法会有所不同。Consult the documentation for your platform for more information.有关详细信息,请参阅平台的文档。
You can verify that these environment variables have been set with the following command:您可以验证是否已使用以下命令设置了这些环境变量:
env | grep AWS
Once set, the following example connects to a MongoDB Atlas cluster using these environment variables:一旦设置好,以下示例将使用这些环境变量连接到MongoDB Atlas集群:
mongosh 'mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS'