Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is mean by +srv in mongoDb connection string

I am new to MongoDB and just encountered two types of connection string.

  1. mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

  2. mongodb+srv://[username:password@]host[/[database][?options]]

I know about the 1st one. But unfamiliar with the (+srv) in the 2nd.

   let connectionUrl;
      if (username && password)
        connectionUrl = `mongodb://${username}:${password}@${host}:${
          port || 27017
        }/${databaseName}`;
      else
        connectionUrl = `mongodb://${host}:${
          port || 27017
        }/${databaseName}`;
      console.log(connectionUrl, "connectionUrlconnectionUrl");
      let connection = await mongoose.createConnection(connectionUrl, {
        useNewUrlParser: true,
      });
      return connection;

Now the problem user can enter username, password, hostname, etc...

But is there any way to know when to add (+srv) because I was trying with localhost and with MongoDB atlas. Atlas works fine with +srv but in the case of localhost, it's throwing an error.

like image 960
Anuresh Verma Avatar asked Feb 16 '21 13:02

Anuresh Verma


People also ask

What is SRV in MongoDB connection string?

The SRV record points to the server or servers that will comprise the members of the replica set. The TXT record defines the options for the replica set, specifically the database that will be used for authorization and the name of the replica set.

What is SRV in MongoDB SRV?

SRV records, or service records, define the location (host and port) of specific services on a server. Some services, like SIP and XMPP/Jabber, require SRV records. For example, cluster hostnames in MongoDB do not resolve using standard dig requests to the hostname in the connection string.

What is SRV string?

He actually played as light as 12s and as heavy as 17s at one point, but for most of his career, Stevie Ray Vaughan's main guitars, his Number One and Lenny that are super legendary, were strung up with 13s—which is probably what you've heard of when you heard about Stevie Ray Vaughan's guitar string gauges in the past ...

What is connection string in MongoDB?

The Connection String describes the hosts to be used and options. The format of the Connection String is: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]] mongodb:// is a required prefix to identify that this is a string in the standard connection format.


1 Answers

in MongoDB 3.6 is introduced the concept of a seed list that is specified using DNS records, specifically SRV and TXT records. You will recall from using replica sets with MongoDB that the client must specify at least one replica set member (and may specify several of them) when connecting. This allows a client to connect to a replica set even if one of the nodes that the client specifies is unavailable

You can see an example of this URL on a 2.2.12 or later connection string

enter image description here

Note that without the SRV record configuration we must list several nodes (in the case of Atlas we always include all the cluster members, though this is not required). We also have to specify the ssl and replicaSet options

With the 3.4 or earlier driver, we have to specify all the options on the command line using the MongoDB URI syntax.

The use of SRV records eliminates the requirement for every client to pass in a complete set of state information for the cluster. Instead, a single SRV record identifies all the nodes associated with the cluster (and their port numbers) and an associated TXT record defines the options for the URI.

enter image description here

check the Reference

like image 183
Mohammad Yaser Ahmadi Avatar answered Oct 19 '22 08:10

Mohammad Yaser Ahmadi