S
S
Semen Frolov2020-02-10 07:34:05
MongoDB
Semen Frolov, 2020-02-10 07:34:05

How to connect from Go to mongodb in docker with authorization?

Good afternoon!

There is a Go code in a docker container:

mc, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://"+user+":"+password+"@"+host+":"+port+"/"+common.StoreName))
  if err != nil {
    return fmt.Errorf("Mongo connection error: %+v", err)
  }

  err = mc.Ping(ctx, readpref.Primary())
  if err != nil {
    return fmt.Errorf("Mongo connection ping: %+v", err)
  }


There is mongodb in docre container:
mongodb:
    container_name: mongodb
    image: mongo:latest
    environment:
     MONGO_INITDB_ROOT_USERNAME: user
     MONGO_INITDB_ROOT_PASSWORD: password
     MONGO_INITDB_DATABASE: database
    volumes:
      - './data:/data/db'


I get an error
Mongo connection ping: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question