我有这个Go Code连接到我的Mongo Cloud数据库:
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")
}
我已经在Windows机器上运行了这个代码,但它确实有效。现在我试着在ubuntu上运行它,我得到以下错误:
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
我不知道,为什么它在Windows上工作,现在它不在ubuntu上。 谢谢你的帮助!
无法解组DNS消息
这与MongoDB Go驱动程序并不完全相关。
Go版本1.11.x #10622 net: target domain names in SRV records should not be compressed中有一个补丁,它强调SRV记录的读取方式遵循RFC-2782。
如果权威DNS服务器(不合规)使用域名压缩发送SRV记录,则net.lookupSRV()
将使用cannot unmarshal DNS message
(net/lookup_unix.go#L130)引发错误。例如,嵌入式Docker DNS可能会进行服务器名称压缩。
Go v1.11的变通方法是:
/etc/resolv.conf
替换为使用兼容和/或公共DNS服务器(即nameserver
或1.1.1.1
)来更新8.8.8.8
的内容