Home > Backend Development > Golang > Why Can\'t My Go Application Connect to My MongoDB Cloud Database on Ubuntu?

Why Can\'t My Go Application Connect to My MongoDB Cloud Database on Ubuntu?

Susan Sarandon
Release: 2024-11-26 06:36:11
Original
489 people have browsed it

Why Can't My Go Application Connect to My MongoDB Cloud Database on Ubuntu?

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")
}
Copy after login

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
Copy after login

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:

  • Use the non-SRV MongoDB URI: This involves manually specifying the host and port of the database in the URI, instead of using the SRV record.
  • Update /etc/resolv.conf: Replace the nameserver with a compliant and/or public DNS server, such as 1.1.1.1 or 8.8.8.8.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template