[GODRIVER-504] how to NewClient with SCRAM-SHA-1, usename, password and database Created: 20/Jul/18  Updated: 11/Sep/19  Resolved: 27/Jul/18

Status: Closed
Project: Go Driver
Component/s: Authentication, Connections
Affects Version/s: 0.0.9
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: orange.jackylee Assignee: Kristofer Brandow (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

linux



 Description   

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

{AuthMechanism: "SCRAM-SHA-1", AuthSource: "xxx", Username: "xxx", Password: "as@xxx"}

)

client, err := mongo.NewClientWithOptions("mongodb://localhost:27017", single, serverSTimeout, appname, sockTimout, conTimeout, auth)
if err != nil

{ log.WithError(err).Fatal("error connecting") }
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

{ log.WithError(err).Fatal("failed to find collection") }

defer cur.Close(context.Background())
for cur.Next(context.Background()) {
elem := bson.NewDocument()
err := cur.Decode(elem)
if err != nil

{ log.WithError(err).Fatal("failed to decode element") }

// do something with elem....
fmt.Printf("%v\n", elem.ToExtJSON(true))
}
if err := cur.Err(); err != nil

{ log.WithError(err).Fatal("failed to get cursor") }

}

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



 Comments   
Comment by orange.jackylee [ 21/Jul/18 ]

thank you kris brandow, after connection string add database, it works.

Comment by Kristofer Brandow (Inactive) [ 20/Jul/18 ]

Hello jackylee,

Does your connection string have a database in it? For instance, if your connection string is "mongodb://localhost:27017/foo" then "foo" would be the database.

Thanks,

Kris

Generated at Thu Feb 08 08:34:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.