Skip to content

Instantly share code, notes, and snippets.

@nedimf
Last active November 7, 2020 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nedimf/c180c3228f07e25b8ad8315a4cb0d1b0 to your computer and use it in GitHub Desktop.
Save nedimf/c180c3228f07e25b8ad8315a4cb0d1b0 to your computer and use it in GitHub Desktop.
"cannot unmarshal DNS message" for SRV lookup MongoDB

Issue

 "cannot unmarshal DNS message" for SRV lookup

this gists won't go over DNS setup part for +srv to work

When you google this error, you will get something few results, one of which is this. If you are not using Docker that one will actually not help you, but will definitly give you perspective on a problem.

What does it mean?

This error is pretty straight forward and a slippery to fix. Let's see whats going on. So offical documentation

The hostnames returned in SRV records must share the same parent domain (in this example, example.com) as the given hostname. If the parent domains and hostname do not match, you will not be able to connect. This doc part is actually explaing how to setup domain. In my +srv was not a priority so I skipped it.

Just make it work, already

To go around this error, you have to use old MongoDB formating string mongodb://. Only problem is how to get valid string generated for your cluster. Fix for this in MongoDB Atlas:

  • Connect
  • Connect your application
  • Choose driver 'Node 2.2.12 or earlier'

Copy generated code to your uri place (be sure to replace with your database password, and with your database name)

Example in Go

client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://"))
  if err != nil {
  	log.Fatal(err)
  }
  ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
  err = client.Connect(ctx)
  if err != nil {
  	log.Fatal(err)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment