登录
首页 >  Golang >  Go问答

无法将 mongodb 2.4 与 GoLang 连接

来源:stackoverflow

时间:2024-04-06 19:27:36 356浏览 收藏

哈喽!今天心血来潮给大家带来了《无法将 mongodb 2.4 与 GoLang 连接》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我无法将 mongodb 与 golang 连接。

telnet mongohost 27017 没问题

ping mongohost 没问题

mongohost 是我的私有主机,不是 docker

mongodb版本:2.4.3

mongodriver:1.3.2

go版本:go版本go1.14.1 darwin/amd64

操作系统:mac

这是我的连接代码

clientoptions := &options.clientoptions{}
clientoptions.setserverselectiontimeout(4 * time.second)
clientoptions.setdirect(true)
clientoptions.setappname("tests")
clientoptions.sethosts([]string{"mongohost:27017"})
clientoptions.setreadpreference(readpref.secondary())
client, err := mongo.newclient(clientoptions)
if err != nil {
    log.fatal(err)
}
ctx, _ := context.withtimeout(context.background(), 5*time.second)
err = client.connect(ctx)
if err != nil {
    log.fatal(err)
}

我更改了超时,但每次收到此错误时它并没有更改连接:

2020/04/12 14:06:19 server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: mongoHost:27017, Type: Standalone, State: Connected, Average RTT: 13849652 }, ] }

解决方案


您设置的客户端设置相互矛盾。您要么建立到单个副本集成员的直接连接,要么建立到具有辅助读取首选项的副本集的连接。我建议将其更改为简单的内容:

client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://mongoHost:27017/?replicaSet=FOOBAR"))

另请参阅 mongo-driver: Connect 以查看有关如何连接的各种代码示例。

请升级您的 mongodb 服务器版本。 mongodb go 驱动程序版本的最不兼容版本是 mongodb 服务器版本 2.6。请参阅 MongoDB Compatibility chart 了解更多信息。

mongodb v2.4 于 2013 年(7 年前)发布,目前版本为 4.2。我强烈建议升级,请参阅 Upgrade MongoDB to v2.6,然后继续升级,直到满足您的要求的最新版本。

今天关于《无法将 mongodb 2.4 与 GoLang 连接》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>