1

i am trying to run go fiber mongo db example provided by line below :

https://github.com/gofiber/recipes/blob/master/mongodb

and i have dockerized the mongo like this :

 mongo:
    container_name: mongo
    image: mongo
    restart: always
    environment:
       MONGO_INITDB_ROOT_USERNAME: root
       MONGO_INITDB_ROOT_PASSWORD: secret
    ports:
      - "27017:27017"
    volumes:
      - mongo:/data/db

and in my code i have this config right now :


// Database settings (insert your own database name and connection URI)
const dbName = "scrapping"
const mongoURI = "mongodb://root:secret@localhost:27017/" + dbName

// Connect configures the MongoDB client and initializes the database connection.
// Source: https://www.mongodb.com/blog/post/quick-start-golang--mongodb--starting-and-setup
func Connect() error {
    client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))

    if err != nil {
        return err
    }

    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    err = client.Connect(ctx)
    db := client.Database(dbName)

    if err != nil {
        return err
    }

    mg = MongoInstance{
        Client: client,
        Db:     db,
    }

    return nil
}

but when i hit the http://127.0.0.1:3000/employee url i get authentication error as below :

connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
Farshad
  • 1,830
  • 6
  • 38
  • 70
  • Does this answer your question? [Mongoexport auth error using mechanism "SCRAM-SHA-1"](https://stackoverflow.com/questions/68927857/mongoexport-auth-error-using-mechanism-scram-sha-1) – Zeke Lu Apr 13 '23 at 00:12
  • Add `authSource=admin` to the connection string: `const mongoURI = "mongodb://root:secret@localhost:27017/" + dbName + "?authSource=admin"` – Zeke Lu Apr 13 '23 at 00:13

0 Answers0