无法在Ubuntu上的Golang中连接到Mongo Cloud mongodb数据库 [英] Can't connect to Mongo Cloud mongodb Database in Golang on Ubuntu
问题描述
我有以下Go Code可以连接到我的Mongo Cloud数据库:
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")
}
我已经在Windows计算机上运行了此代码,并且可以正常工作.现在,我尝试在ubuntu上运行它,并且出现以下错误:
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
我不知道为什么它可以在Windows上运行,而现在在ubuntu上却不可用.
感谢您的帮助!
I don't know, why it worked on windows, and now it doesn't on ubuntu.
Thanks for your help!
推荐答案
无法解组DNS邮件
cannot unmarshal DNS message
这与MongoDB Go驱动程序不太相关.
This is not quite related to MongoDB Go driver.
Go版本1.11.x中有一个修补程序#10622净:SRV记录中的目标域名不应压缩,以加强读取SRV记录的方式以遵循RFC-2782.
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.
如果权威DNS服务器(非兼容)使用域名压缩发送SRV记录,则net.lookupSRV()
将使用cannot unmarshal DNS message
引发错误(
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.
Go v1.11的解决方法是:
The workarounds for Go v1.11 are:
- 使用非SRV MongoDB URI
- 通过替换
nameserver
以使用兼容和/或公共DNS服务器(即1.1.1.1
或8.8.8.8
)来更新
/etc/resolv.conf
的内容
- Use the non-SRV MongoDB URI
- Update the content of
/etc/resolv.conf
by replacing thenameserver
to use a compliant and/or public DNS server i.e.1.1.1.1
or8.8.8.8
另请参见 GODRIVER-829
这篇关于无法在Ubuntu上的Golang中连接到Mongo Cloud mongodb数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!