doupafu6980 2019-04-12 22:23
浏览 114

无法在Ubuntu上的Golang中连接到Mongo Cloud mongodb数据库

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!

  • 写回答

1条回答 默认 最新

  • duanhao4909 2019-04-17 06:56
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器