3

I tried to connect to mongodb Atlas using golang drivers.

tlsConfig := &tls.Config{}

var mongoURI = "mongodb+srv://admin:password@prefix.mongodb.net:27017/dbname"
dialInfo, err := mgo.ParseURL(mongoURI)
if err != nil {
    panic(err)
}
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)
if err != nil {
    println("error")
    log.Fatal(err)
}
_ = session
c := session.DB("Token").C("_Users")
user := &User{firstName: "username"}
err = c.Insert(user)
if err != nil {
    println("error Again")
}

I am not getting an error not getting connected. I am wondering what could be the reason.' Any help is appreciated.

I tried to create DialInfo using below code

    dialInfo := &mgo.DialInfo{
    Addrs:     []string{"prefix.mongodb.net:27017"},
    Database:  "dbname",
    Mechanism: "SCRAM",
    Timeout:   10 * time.Second,
    Username:  "admin",
    Password:  "passwrd",
}

Now I am getting no reachable servers

joss
  • 695
  • 1
  • 5
  • 16
  • _"I am not getting an error not getting connected."_ This is contradictory. If you can't connect, the `c.Insert()` operation would surely return an error. If you truly don't get any errors, then my guess is that you connect just fine, you insert just fine, but when you want to verify, you're looking in the wrong db/collection? – icza Aug 28 '18 at 07:39
  • I could only see that the code started, then nothing.. My assumption is it is not getting connection itself. What i need to know is that if mgo supports new connection string – joss Aug 28 '18 at 15:23

2 Answers2

4

I could only see that the code started, then nothing

As you have figured out, this is because DialInfo by default has a zero timeout. The call will block forever waiting for a connection to be established. You can also specify a timeout with:

dialInfo.Timeout = time.Duration(30)
session, err := mgo.DialWithInfo(dialInfo)

Now I am getting no reachable servers

This is because globalsign/mgo does not currently support SRV connection string URI yet. See issues 112. You can use the non-srv connection URI format (MongoDB v3.4), see a related question StackOverflow: 41173720.

You can use mongo-go-driver instead if you would like to connect using the SRV connection URI, for example:

mongoURI := "mongodb+srv://admin:password@prefix.mongodb.net/dbname?ssl=true&retryWrites=true"

client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
if err != nil {
    log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
err = client.Connect(ctx)
defer client.Disconnect(ctx)

if err != nil {
    log.Fatal(err)
}
database := client.Database("go")
collection := database.Collection("atlas")

The above example is compatible with the current version v1.0.0

Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • 2
    Thanks for your help. I tried it now I am getting a new error " cannot unmarshal DNS message". I checked online and I found that this was a bug that was already fixed long back. Is there anything I am missing here. Thanks in advance – joss Aug 29 '18 at 16:11
  • Which driver and which version are you using? – Wan B. Aug 29 '18 at 16:57
  • I used Mongodb 3.4 or below type of string connection and it works. As you told the new string style connections are still in dev. Again thanks for your help. – joss Sep 02 '18 at 17:47
  • To clarify, the SRV string style is not in development however the `mongo-go-driver` version currently is. – Wan B. Sep 03 '18 at 01:36
0

For MongoDB Atlas

 serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
  clientOptions := options.Client().
    ApplyURI("mongodb://username:password@prefix0.mongodb.net:27017,prefix1.mongodb.net:27017,prefix2.mongodb.net:27017/?retryWrites=true&w=majority&replicaSet=atlas-zhqegh-shard-0&tls=true").
    SetServerAPIOptions(serverAPIOptions)
  ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  defer cancel()
  client, err := mongo.Connect(ctx, clientOptions)
  if err != nil {
    log.Fatal(err)
  }

To clarify the MongoDB Atlas replicaSet and hosts you can for instance utilize MongoDB Compass: just connect to the cluster and you will see all that data.

wan_keen
  • 116
  • 2
  • 5