Error Connecting to Mongo Cloud Database in Go on Ubuntu
Problem
When attempting to connect to a Mongo Cloud database in Go on Ubuntu using the following code:
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") }
an error message appears:
2019/04/13 00:20:37 Error connecting to Database: error parsing uri (mongodb+srv://User:[email protected]/test?retryWrites=true): lookup cluster0-gpxjk.gcp.mongodb.net on 127.0.0.53:53: cannot unmarshal DNS message exit status 1
Solution
This issue is not directly related to the Go MongoDB driver but rather to a change in Go version 1.11.x #10622 that tightened the way SRV records are read, following RFC-2782.
If an authoritative DNS server sends an SRV record using domain name compression, the net.lookupSRV() function throws an error with the message "cannot unmarshal DNS message".
To resolve this issue, consider the following workarounds:
For further information, refer to GODRIVER-829.
The above is the detailed content of Why Can\'t My Go Application Connect to My MongoDB Cloud Database on Ubuntu?. For more information, please follow other related articles on the PHP Chinese website!