Details
-
Bug
-
Resolution: Gone away
-
Major - P3
-
None
-
None
-
None
-
go version: 1.12
Mongo version: 3.4
Description
Using the mongo-driver I am able to connect to the replica set cluster and ping is successful but not able to run any queries.
All the queries return the following error:
auth error: unable to authenticate using mechanism "<Mechanism Name>": (AuthenticationFailed) auth failed
But I am able to connect to the db and make queries using mgo, pymongo and other drivers.
Code :
package main
|
import (
|
"context"
|
"fmt"
|
"time"
|
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
)
|
|
|
func main() {
|
|
|
cred := options.Credential{
|
AuthMechanism: "<Mechanism Name>",
|
Username: "<UserName>",
|
Password: "<Password>",
|
}
|
clientOptions := options.Client()
|
clientOptions.ApplyURI("<connection string>")
|
clientOptions.SetAuth(cred)
|
clientOptions.SetDirect(true)
|
clientOptions.SetReplicaSet("<replicaSet>")
|
err := clientOptions.Validate()
|
if err != nil {
|
fmt.Println("Invalid client options:", err)
|
return
|
}
|
client, err := mongo.NewClient(clientOptions)
|
if err != nil {
|
fmt.Println("cannot create client:", err)
|
return
|
}
|
|
|
ctx, _ := context.WithTimeout(context.Background(), time.Duration(30)*time.Second)
|
err = client.Connect(ctx)
|
if err != nil {
|
fmt.Println("connection error", err)
|
return
|
}
|
err = client.Ping(ctx, nil)
|
if err != nil {
|
fmt.Println("ping error", err)
|
return
|
}
|
fmt.Println("ping successful")
|
Database := client.Database("collect-dev")
|
fmt.Println(Database.ListCollections(ctx, bson.M{}))
|
}
|
The above code works as expected when trying to connect to standalone mongodb.
When i try to do the same using mgo or pymongo it works.
package main
|
|
|
import (
|
"fmt"
|
"time"
|
|
|
"github.com/globalsign/mgo"
|
)
|
|
|
func main() {
|
infog := &mgo.DialInfo{
|
Addrs: []string{"<host>"},
|
Timeout: 60 * time.Second,
|
Database: "<dbName>",
|
ReplicaSetName: "<replicaSet>",
|
Username: "<Username>",
|
Password: "<Password>",
|
Direct: true,
|
Mechanism: "<Mechanism>",
|
}
|
|
|
session, err := mgo.DialWithInfo(infog)
|
if err != nil {
|
fmt.Println("dial error:", err)
|
return
|
}
|
err = session.Ping()
|
if err != nil {
|
fmt.Println("ping error:", err)
|
return
|
}
|
fmt.Println("ping successful")
|
db := session.DB("<dbName>")
|
fmt.Println(db.CollectionNames())
|
}
|