2

I am witnessing something strange.

I have an app, written in GO but I am not sure this is relevant, that is using Mongo Atlas. The app can happily connect to the DB when I am using my home wireless.

If I change connection and I use the connectivity provided by my phone, the same app can not connect any more to the same database.

The code that fails is this

connString := "mongodb+srv://my_usr:my_pwd@my_cluster.mongodb.net/scopone?retryWrites=true&w=majority"
clientOptions := options.Client().ApplyURI(connString)
client, err := mongo.NewClient(clientOptions)
if err != nil {
    log.Fatal("Error creating Mongo Client", err)
}

An the error I get is

error parsing uri: lookup my_cluster.mongodb.net on 172.20.10.1:53: cannot unmarshal DNS message

Any clue?

Picci
  • 16,775
  • 13
  • 70
  • 113
  • Does this answer your question? [Can't connect to Mongo Cloud mongodb Database in Golang on Ubuntu](https://stackoverflow.com/questions/55660134/cant-connect-to-mongo-cloud-mongodb-database-in-golang-on-ubuntu) – Marc Jul 01 '20 at 16:18
  • Seems like a DNS issue, and not a go issue. – super Jul 01 '20 at 16:22
  • 172.20.* is part of the 172.16/12 Class B private subnet, meaning it's a LAN address. So it won't work on a different network. – Adrian Jul 01 '20 at 16:29
  • There is a bug report at https://github.com/golang/go/issues/37362 : `Building with Go 1.11+ and using connection strings with the "mongodb+srv"[1] scheme is incompatible with some DNS servers in the wild due to the change introduced in #10622. If you receive an error with the message "cannot unmarshal DNS message" while running an operation, we suggest you use a different DNS server.` – Joe Jul 02 '20 at 04:16

2 Answers2

0

If you try to access MongoDB Atlas from two different networks, Atlas will see two different IP addresses. To troubleshoot this, follow these steps

1 == Discover your IP address under each connection mode

Connect to each network and go to https://whatismyipaddress.com/ to find out what are the two IP Addresses


2 == On both networks make sure you can connect on ports 27017, 27015 and 27016.

You can use this website to check your port number: http://portquiz.net:27017/


3 == Check is your IP Whitelisting.

Inside of the MongoDB Atlas console, go on the left side menu, Security => Network Access => IP Whitelisting. Make sure that the CIDR range allows you to connect from both IP addresses.


Quick note: If this is a test cluster with anonymous fake data, you can run a quick test by whitelisting 0.0.0.0/0 (Access from anywhere).

Please note this is a security violation if you are hosting "real" data. This is something you can do only if this is a test cluster, and the data being used is either fake or public data.

BigDataKid
  • 1,199
  • 6
  • 10
0

I was experiencing the same issue but got if fixed.

I am not sure if you got a fix, but I will put this here incase there is someone who will encounter the same issue. I fixed this by not using the "mongo+srv://..." connection string.

A fix would be to choose the mongo Java driver and choose Version 3.4

package main

import (
  "go.mongodb.org/mongo-driver/mongo"
)

func main () {
  ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  defer cancel()
  client, err := mongo.Connect(ctx, options.Client().ApplyURI(
     "mongodb://<USERNAME>:<PASSWORD>@cluster0-shard-00-00.oospj.mongodb.net:27017,cluster0-shard-00-01.oospj.mongodb.net:27017,cluster0-shard-00-02.oospj.mongodb.net:27017/<DBNAME>?ssl=true&replicaSet=atlas-d5lvih-shard-0&authSource=admin&retryWrites=true&w=majority",
  ))
  if err != nil {
    log.Fatal(err)
  }
}

This worked for me and hope it does work for you and the next person who will attempt using mongo and GO.

0xdeadbeef
  • 101
  • 1
  • 5