6

I have this Go Code to connect to my Mongo Cloud Database:

func connectToDataBase() {
    ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    defer cancel()
    client, err := mongo.Connect(ctx, options.Client().ApplyURI(dbURL))
    if err != nil {
        log.Fatal("Error connecting to Database: ", err.Error())
    }
    DB = client.Database("storyfactory")
}

I already ran this code on a Windows machine, and it worked. Now I tried to run it on ubuntu, and I get following error:

2019/04/13 00:20:37 Error connecting to Database: error parsing uri (mongodb+srv://User:Password@cluster0-gpxjk.gcp.mongodb.net/test?retryWrites=true): lookup cluster0-gpxjk.gcp.mongodb.net on 127.0.0.53:53: cannot unmarshal DNS message
exit status 1

I don't know, why it worked on windows, and now it doesn't on ubuntu.
Thanks for your help!

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Tobi696
  • 448
  • 5
  • 16
  • Questions: 1) Are you using Golang version 1.11 ? 2) Are you using Docker for the Ubuntu ? Or the Windows and the Ubuntu are completely different machine ? – Wan B. Apr 15 '19 at 04:32

3 Answers3

19

cannot unmarshal DNS message

This is not quite related to MongoDB Go driver.

There is a patch in Go version 1.11.x #10622 net: target domain names in SRV records should not be compressed that tighten the way SRV records are read to follow RFC-2782.

If an authoritative DNS server (non-compliantly) sends an SRV records using domain name compression, the net.lookupSRV() will throw an error with cannot unmarshal DNS message (net/lookup_unix.go#L130). For example, the embedded Docker DNS maybe doing the server name compression.

The workarounds for Go v1.11 are:

  • Use the non-SRV MongoDB URI
  • Update the content of /etc/resolv.conf by replacing the nameserver to use a compliant and/or public DNS server i.e. 1.1.1.1 or 8.8.8.8

See also GODRIVER-829

Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • 4
    actually updating the resolv.conf worked fine. thank you – OWADVL Jul 23 '19 at 12:36
  • 2
    @OWADVL: beware that [changes to resolv.conf do not persist reboots](https://unix.stackexchange.com/questions/174349/what-overwrites-etc-resolv-conf-on-every-boot), and it's [non-trivial to fix that](https://superuser.com/questions/677343/how-to-make-name-server-address-permanent-in-etc-resolv-conf). – Dan Dascalescu Oct 21 '19 at 04:47
6

The work around is to use non SRV connection string. Go to Mongo Atlas to get your connection string as per normal.

Driver select Java, version 3.4 or later

You should now see you connection string. Works with go1.13.8.

Chuk Lee
  • 3,570
  • 22
  • 19
1

Another option, found here suggests installing resolvconf (for Ubuntu apt install resolvconf), add the line nameserver 8.8.8.8 to /etc/resolvconf/resolv.conf.d/base, then run sudo resolvconf -u and to be sure service resolvconf restart. To verify run systemd-resolve --status.

You should see on the first line your DNS server like here:

         DNS Servers: 8.8.8.8
          DNS Domain: sa-east-1.compute.internal
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
eli
  • 86
  • 1
  • 3