Hướng dẫn dùng mongodb+srv protocol trong PHP
Docs Home → MongoDB Manual This document describes the URI formats for defining connections between applications and MongoDB instances in the official MongoDB Drivers. For a list of drivers and links to driver documentation, see Drivers. You can specify the MongoDB connection string using either:
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. The standard URI connection scheme has the form:
For more examples, see Examples. The standard URI connection string includes the following components:
See also
The host (and optional port number) where the
If the port number is not specified, the default port
Optional. The authentication database to use if the connection string includes If both
Optional. A query string that specifies connection specific options as If the connection string does not specify a database/ you must specify a slash ( In addition to the standard connection format, 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. In order to leverage the DNS seed list, use a
connection string prefix of NoteUse of the The following example shows a typical connection string for a DNS seed list connection string:
The corresponding DNS configuration might resemble:
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 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. ImportantThe hostnames returned in SRV records must share the same parent domain (in this example, 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:
You may only specify one TXT record per The TXT record for the
Taken together, the DNS SRV records and the options specified in the TXT record resolve to the following standard format connection string:
You can override the options specified in a TXT record by passing the option in the query string. In the following example, the query string has provided an override for the
Given the override for the
NoteThe TipSee:This section lists all connection options. Connection options are pairs in the following form:
Separate options with the ampersand (i.e.
Note
Semi-colon separator for connection string argumentsTo provide backwards compatibility, drivers currently accept semi-colons (i.e. The following connection string to a replica set named
The following connection string to a replica set includes
Alternatively, you can also use the equivalent
If the New in version 4.2. ssl
A boolean to enable or disables TLS/SSL for the connection:
NoteThe |