You can specify the MongoDB connection string by using one of the following formats:您可以使用以下格式之一指定MongoDB连接字符串:
- SRV Connection Format: A connection string with a hostname that corresponds to a DNS SRV record. Your driver or
mongoshqueries the record to determine which hosts are running themongodormongosinstances. - Standard Connection String Format: A connection string that specifies all hosts that are running the
mongodormongosinstances.
MongoDB Atlas clusters use SRV connection format unless you connect to an online archive.
To connect directly to a host and port specified in a connection string, set the directConnection option to true. For full details about directConnection and the other connection string options, see Connection String Options.
SRV Connection FormatSRV连接格式
MongoDB supports a DNS-constructed seed list. 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.
The SRV URI connection scheme has the following form:SRV URI连接方案具有以下形式:
mongodb+srv://[username:password@]host[/[defaultauthdb][?options]]
For more examples, see these pages:有关更多示例,请参阅以下页面:
MongoDB Atlas Deployment Connection String ExampleMongoDB Atlas部署连接字符串示例sSelf-Managed Deployment Connection String Examples自我管理部署连接字符串示例
Connection String Components连接字符串组件
A connection string includes the following components:连接字符串包括以下组件:
| A required prefix to identify that this is a string in the standard connection format ( |
| Optional. Authentication credentials. If specified, the client will attempt to authenticate the user to the
|
| The host (and optional port number) where the
If the port number is not specified, the default port If you use the SRV URI connection format, you can specify only one host and no port. Otherwise, the driver or |
| Optional. The authentication database to use if the connection string includes If both IMPORTANT: For Atlas deployments using the SRV Connection Format, |
| Optional. A query string that specifies connection specific options as If the connection string does not specify a database/ you must specify a slash ( |
To use the DNS seed list, use the same syntax as a standard connection string with a prefix of mongodb+srv rather than the standard mongodb. The +srv indicates to the client that the hostname that follows corresponds to a DNS SRV record. The driver or mongosh queries the DNS for the record to determine which hosts are running the mongod or mongos instances.
Note
When using the +srv format, you must specify the hostname, domain, and top-level domain (TLD) in the following format: <hostname>.<domain>.<TLD>. This table shows how the placeholders correspond to example values:此表显示了占位符如何与示例值对应:
| Placeholder | Example |
|---|---|
|
|
|
|
|
|
|
|
This example shows a DNS seed list connection string that correctly uses the <hostname>.<domain>.<TLD> format. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd:
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@server.example.com/
The corresponding DNS configuration resembles:
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 _mongodb._tcp.<hostname>.<domain>.<TLD> format.
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. If this happens, clients 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.
Important
The hostnames returned in SRV records must share the same parent domain (in this example, SRV记录中返回的主机名必须与给定主机名共享相同的父域(在本例中为example.com) as the given hostname. If the parent domains and hostname do not match, you can't connect.example.com)。如果父域和主机名不匹配,则无法连接。
Like the standard connection string, the DNS seed list connection string supports specifying options as a query string. With a DNS seed list connection string, you can also specify the following options via a TXT record:与标准连接字符串一样,DNS种子列表连接字符串支持将选项指定为查询字符串。使用DNS种子列表链接字符串,您还可以通过TXT记录指定以下选项:
replicaSetauthSource
You can only specify one TXT record per mongod instance. If multiple TXT records appear in the DNS or if the TXT record contains an option other than replicaSet or authSource, the client returns an error.
The TXT record for the server.example.com DNS entry resembles the following example:
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:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
To override the options specified in a TXT record, specify the option in the query string. For example, the following query string overrides the authSource option configured in the TXT record of the previous DNS entry.
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@server.example.com/?connectTimeoutMS=300000&authSource=aDifferentAuthDB
Given the override for the authSource, the equivalent connection string in the standard format would resemble the following example:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb1.example.com:27317,mongodb2.example.com:27017/?connectTimeoutMS=300000&replicaSet=mySet&authSource=aDifferentAuthDB
Note
The mongodb+srv option fails if there is no available DNS with records that correspond to the hostname identified in the connection string. If you use the +srv connection string modifier, the tls (or the equivalent ssl) option is set to true for the connection. 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.
For an example that connects mongosh to a replica set using the DNS seed list connection format, see mongosh Connection Options.
Standard Connection String Format标准连接字符串格式
This section describes the standard format of the MongoDB connection URI used to connect to a self-hosted MongoDB standalone deployment, replica set, or sharded cluster.本节介绍用于连接到自托管MongoDB独立部署、副本集或分片集群的MongoDB连接URI的标准格式。
The standard URI connection scheme has the form:标准URI连接方案的形式如下:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]Connection String Database Options连接字符串数据库选项
You can specify a default database in the [/defaultauthdb] field of the connection string. The client uses the specified [/defaultauthdb] database as the default database. If unspecified by the connection string, MongoDB uses the test database as the default.
You can specify the authentication database in your connection string using the authSource connection option. If specified, the client uses this database to verify your user identity and credentials. If authSource is unspecified, it defaults to the [/defaultauthdb] database. If both authSource and [/defaultauthdb] are unspecified, authSource defaults to the admin database.
The following example connection string sets the default database to 以下示例连接字符串将默认数据库设置为myDefaultDB and the authentication database to admin:myDefaultDB,将身份验证数据库设置为admin:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017/myDefaultDB?authSource=adminConnection String Components连接字符串组件
A connection string includes the following components:连接字符串包括以下组件:
| A required prefix to identify that this is a string in the standard connection format ( |
| Optional. Authentication credentials. If specified, the client will attempt to authenticate the user to the If the username or password includes the following characters, those characters must be converted using percent encoding:
See also |
| The host (and optional port number) where the
If you use the SRV URI connection format, you can specify only one host and no port. Otherwise, the driver or |
| Optional. The authentication database to use if the connection string includes If both IMPORTANT: For Atlas deployments using the SRV Connection Format, |
| Optional. A query string that specifies connection specific options as
|