使用Golang的mgo连接到MongoDB Atlas:无法连接到可用的副本集服务器

huangapple go评论94阅读模式
英文:

Connecting to MongoDB Atlas using Golang mgo: Persistent no reachable server to replica set

问题

我有一个来自MongoDB Atlas的副本集,可以用任何其他语言和常规的mongo客户端连接,提供的URL格式如下:

mongodb://user:pass@prefix1.mongodb.net:27017,prefix2.mongodb.net:27017,prefix3.mongodb.net:27017/test?&replicaSet=Cluster0-shard-0&authSource=admin

无论我尝试什么,添加ssl=true或者移除,都不起作用。始终显示"没有可达的服务器"。

我尝试了URL的每种组合,每种dialConfig的组合,还有Dial和DialWithConfig的配置。

可能的原因是什么?

英文:

I have a replica set from MongoDB atlas, to which I can connect with ANY other language, and regular mongo client, with the URL provided with the format :

mongodb://user:pass@prefix1.mongodb.net:27017,prefix2.mongodb.net:27017,prefix3.mongodb.net:27017/test?&replicaSet=Cluster0-shard-0&authSource=admin

No matter what I tried, adding ssl=true and removing, nothing works. It is always "no reachable server".

I tried every single combination for URL, every combination for dialConfig, and also Dial and DialWithConfig configurations.

What could be the reason ?

答案1

得分: 20

使用MongoDB Go驱动程序mgo的代码片段连接到MongoDB Atlas是有效的,使用您提供的示例数据:

import (
    "gopkg.in/mgo.v2"
    "crypto/tls"
    "net"
)

tlsConfig := &tls.Config{}

dialInfo := &mgo.DialInfo{
    Addrs: []string{"prefix1.mongodb.net:27017", 
                    "prefix2.mongodb.net:27017",
                    "prefix3.mongodb.net:27017"},
    Database: "authDatabaseName",
    Username: "user",
    Password: "pass",
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
    conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
    return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)

请注意,您还可以仅指定副本集成员中的一个作为种子。例如:

Addrs: []string{"prefix2.mongodb.net:27017"}

另请参阅:

更新

您还可以使用ParseURL()方法解析MongoDB Atlas URI字符串。但是,目前此方法不支持SSL(mgo.V2 PR:304)。

解决方法是在解析之前删除ssl=true行。

//没有ssl=true的URI
var mongoURI = "mongodb://username:password@prefix1.mongodb.net,prefix2.mongodb.net,prefix3.mongodb.net/dbName?replicaSet=replName&authSource=admin"
 
dialInfo, err := mgo.ParseURL(mongoURI)

//以下部分与上面类似。
tlsConfig := &tls.Config{}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
    conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
    return conn, err
}
session, _ := mgo.DialWithInfo(dialInfo)
英文:

Using MongoDB Go driver mgo code snippet below to connect to MongoDB Atlas works, using your example data:

import (
    "gopkg.in/mgo.v2"
    "crypto/tls"
    "net"
)

tlsConfig := &tls.Config{}

dialInfo := &mgo.DialInfo{
    Addrs: []string{"prefix1.mongodb.net:27017", 
                    "prefix2.mongodb.net:27017",
                    "prefix3.mongodb.net:27017"},
    Database: "authDatabaseName",
    Username: "user",
    Password: "pass",
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
    conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
    return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)

Note that you can also specify only one of the replica set members as a seed. For example:

Addrs: []string{"prefix2.mongodb.net:27017"}

See also:

Update:

You could also use ParseURL() method to parse MongoDB Atlas URI string. However, currently this method does not support SSL (mgo.V2 PR:304)

A work around is to take out the ssl=true line before parsing.

//URI without ssl=true
var mongoURI = "mongodb://username:password@prefix1.mongodb.net,prefix2.mongodb.net,prefix3.mongodb.net/dbName?replicaSet=replName&authSource=admin"
 
dialInfo, err := mgo.ParseURL(mongoURI)

//Below part is similar to above. 
tlsConfig := &tls.Config{}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
    conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
    return conn, err
}
session, _ := mgo.DialWithInfo(dialInfo)

huangapple
  • 本文由 发表于 2016年12月16日 05:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/41173720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定