Connecting to MongoDB Atlas Using Golang Mgo: Resolving "No Reachable Server to Replica Set" Issue
Question:
When connecting to a replica set on MongoDB Atlas using the Golang mgo driver, an error message of "no reachable server" is encountered despite successful connections with other languages and the regular Mongo client.
Answer:
A modified version of the mgo code snippet below successfully establishes a connection to MongoDB Atlas using the provided example parameters:
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)
Consideration:
The above is the detailed content of Why Does My Go mgo Driver Fail to Connect to MongoDB Atlas Replica Set with a \'No Reachable Server\' Error?. For more information, please follow other related articles on the PHP Chinese website!