Mongodb or mongodb+srv

Hello,

There are two prefix can be used when connect to a mongodb, mongodb or mongodb+srv, when I check the source code at mongo-go-driver/connstring.go at master · mongodb/mongo-go-driver · GitHub, I found the code below:

	if p.Scheme == SchemeMongoDBSRV {
		parsedHosts, err = p.dnsResolver.ParseHosts(hosts, true)
		if err != nil {
			return err
		}
		connectionArgsFromTXT, err = p.dnsResolver.GetConnectionArgsFromTXT(hosts)
		if err != nil {
			return err
		}

		// SSL is enabled by default for SRV, but can be manually disabled with "ssl=false".
		p.SSL = true
		p.SSLSet = true
	}

here SchemeMongoDBSRV is “mongodb+srv”.

based on the code, my understanding is if I deploy the mongodb on other servers than the local machine, i.e. on another docker or physical machine, I should use “mongodb+srv” as prefix when connecting to the db.
am I right?

James

Hello, I am using mongo go driver to replacing the globalsign mgo. I use the same connection string in mgo as in mongo-go-driver. During running the test on local machine and gitlab CI, the connection are ok, all the test passed. But when I deploy my service on pre-production, I got error “unable to authenticate using mechanism “SCRAM-SHA-1”: (AuthenticationFailed) Authentication failed”. what I should check in my code and on the pre-production db?
thanks,

James

SRV is a way to specify a single hostname that resolves to multiple host names. When using SRV, the driver conducts an SRV lookup to get the actual names of all of the hosts. Also, when using SRV, the driver does lookups for TXT records, which can contain specific URI options to configure the driver. Using the “mongodb+srv” prefix tells the driver that you’ve set up all of the SRV records correctly and the URI you’re giving needs to be resolved before using. If you haven’t set all of this up, you shouldn’t be using that prefix.

For the authentication error, I don’t remember anything about mgo’s URI parsing off the top of my head. Can you answer the following:

  1. Have you enabled auth on your database?
  2. If so, what auth mechanism (e.g. SCRAM-SHA-1/256, X509, etc)?
  3. What does your URI look like? For security, you can redact any fields like usernames/passwords and just replace them with placeholder.

– Divjot

1 Like

Hello Divjot,

Thanks for the answer. It looks not only an issue in code, I have to rely on the configuration manager help to check the settings.

Best,

James

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.