-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: 0.0.9
-
Component/s: Authentication, Connections
-
None
-
Environment:linux
package main
import (
"context"
"fmt"
"github.com/apex/log"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/mongodb/mongo-go-driver/mongo/clientopt"
"time"
)
func main()
{ f1() }func f1() {
appname := clientopt.AppName("myapp")
conTimeout := clientopt.ConnectTimeout(time.Second)
sockTimout := clientopt.SocketTimeout(time.Second)
serverSTimeout := clientopt.ServerSelectionTimeout(time.Second * 3)
single := clientopt.Single(true)
auth := clientopt.Auth(clientopt.Credential
)
client, err := mongo.NewClientWithOptions("mongodb://localhost:27017", single, serverSTimeout, appname, sockTimout, conTimeout, auth)
if err != nil
err = client.Connect(context.Background())
if err != nil { log.WithError(err).Fatal("error connecting") }
fmt.Println(client.ListDatabases(context.Background(), nil))
collection := client.Database("xxx").Collection("test")
cur, err := collection.Find(context.Background(), nil)
if err != nil
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
elem := bson.NewDocument()
err := cur.Decode(elem)
if err != nil
// do something with elem....
fmt.Printf("%v\n", elem.ToExtJSON(true))
}
if err := cur.Err(); err != nil
}
than go run main.go, it outputs
command-line-arguments
{[] 0} auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": Authentication failed.
2018/07/20 17:48:35 fatal failed to find collection error=auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": Authentication failed.
exit status 1
how to make it works