CreateSession: no reachable servers - mgo

51 views
Skip to first unread message

J Doe via StackOverflow

unread,
Apr 21, 2017, 4:22:05 AM4/21/17
to google-appengin...@googlegroups.com

I'm trying to connect to MongoDB Atlas free cluster using mgo.

Golang Code -

package main

import (
     "fmt"
     "gopkg.in/mgo.v2"
     "time"
     "log"
)

const (
    AuthDatabase = "mydatabase"
    AuthUserName = "databaseadmin"
    AuthPassword = "databasepassword"
    ReplicaSetName = "myproject-shard-0"
)

func main(){

MongoDBHosts := []string{
    "myproject-shard-00-00-w4vds.mongodb.net:27017",
    "myproject-shard-00-01-w4vds.mongodb.net:27017",
    "myproject-shard-00-02-w4vds.mongodb.net:27017",
}

mongoDBDialInfo := &mgo.DialInfo{
    Addrs:    MongoDBHosts,
    Timeout:  60 * time.Second,
    Database: AuthDatabase,
    Username: AuthUserName,
    Password: AuthPassword,
    ReplicaSetName: ReplicaSetName,
}

mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
    log.Fatalf("CreateSession: %s\n", err)
}

defer  mongoSession.Close()
fmt.Printf("Connected to replica set %v!\n", mongoSession.LiveServers())
}

Error Message -

CreateSession: no reachable servers

Environment

I'm using mongodb free cluster with Google App Engine GO SDK



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/43537470/createsession-no-reachable-servers-mgo

J Doe via StackOverflow

unread,
Apr 21, 2017, 6:32:07 AM4/21/17
to google-appengin...@googlegroups.com

To connect to MongoDB Atlas, you need SSL

Prerequisites

TLS/SSL

    Clients must have support for TLS/SSL to connect to an Atlas cluster.

    Clients must have support for the SNI TLS extension to connect to an Atlas M0 Free Tier cluster.


Whitelist

    To access a cluster, you must connect from an IP address on the Atlas group’s IP whitelist. If you need to add an IP address to the whitelist, you can do so in the Connect dialog. You can also add the IP address from the Security tab.

Thus I had to change the following piece of code

mongoDBDialInfo := &mgo.DialInfo{
    Addrs:    MongoDBHosts,
    Timeout:  60 * time.Second,
    Database: AuthDatabase,
    Username: AuthUserName,
    Password: AuthPassword,
    ReplicaSetName: ReplicaSetName,
}

mongoDBDialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
        conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
        return conn, err
    }

I had to import the following too

"crypto/tls" 
"net"


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/43537470/createsession-no-reachable-servers-mgo/43540357#43540357
Reply all
Reply to author
Forward
0 new messages