0

I'm trying to connect to my mongo db atlas database, but I'm getting an error that I can't solve it within mongo+go forums.

The code:

package main

import (
    "context"
    "log"
    "time"
    "fmt"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
    "go.mongodb.org/mongo-driver/bson"
)

func main(){

    client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://my-user:<my-pass>@datalake0-lesz0.a.query.mongodb.net/my-db?ssl=true&authSource=admin"))
    if err != nil {
        log.Fatal(err)
    }
    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
    err = client.Connect(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Disconnect(ctx)
    err = client.Ping(ctx, readpref.Primary())
    if err != nil {
        log.Fatal(err)
    }
    databases, err := client.ListDatabaseNames(ctx, bson.M{})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(databases)
}

The error:

connection() error occured during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) authentication failed, correlationID = 167bc5ba18415510a4144b7a exit status 1

5
  • Have you whitelisted your public IP address in MongoDB Atlas? Commented May 4, 2021 at 5:35
  • I have inputed "Allow access from anywhere" on Atlas Commented May 4, 2021 at 5:38
  • Try entering 0.0.0.0 in the IP whitelist section and make sure that the URL, username, and passwords are correct. Commented May 4, 2021 at 5:52
  • AuthenticationFailed means it connected and submitted the credentials, but the server doesn't think that is the right user/password/database. Commented May 4, 2021 at 6:28
  • You are right, Joe. I used "<>", forgot to delete. sorry bout that, and tks everyone! Commented May 4, 2021 at 12:31

1 Answer 1

1

If the URI in the connect string is verbatim, then this is your problem: mongodb://my-user:<my-pass> is incorrect, <my-pass> should be substituted with your password

You've probably cut and pasted the provided URI on completion of DB setup but this connect string does not include items like your password, also you gave my-user which, unless you set up the username my-user that also needs changing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.