14

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 ?

Muhammad Tariq
  • 3,318
  • 5
  • 38
  • 42
hece
  • 364
  • 2
  • 15

1 Answers1

20

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)
Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • Would you be able to advise on how the connection string would look if `dialInfo` was generated via this way instead? `dialInfo, err := mgo.ParseURL(url)`. Appreciated. – Marcel Gruber May 17 '17 at 15:53
  • Hi @Wan Bachtiar, any idea in how to parse the new URLS for Atlas in Mongo 3.6. Eg. "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/?serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000" – rsan Aug 21 '18 at 17:13
  • @rsan the new official mongo-go-driver should support that. If you’re still having an issue please open a new question and link it here. Please provide the go driver and version that you’re using. Cheers – Wan B. Aug 22 '18 at 12:49
  • @WanBachtiar thanks Wan, Tested and working using the official driver. – rsan Aug 23 '18 at 01:15
  • just small update set ```Source``` in ```DialInfo``` else you can get following error ```panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1455022]``` I have set ```source: "admin"``` which works fine to me. – Snehal Dangroshiya Nov 24 '18 at 07:39