Getting storeKey mismatch when trying to connect to MongoDB via Docker

Viewed 832

I have this backend project that works just fine when starting it locally with environment variables set on .env, but when I tried to create a docker-compose to continue working on it using Docker, the following error occurred AuthenticationFailed: SCRAM authentication failed, storedKey mismatch.

The problem is that both user and password are exactly the same I use locally and set them up using mongo-init.js file and with MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD on docker-compose.yml.

What am I doing wrong that is causing my Node server don't connect with Mongo? It connects with Redis perfectly, but with Mongo I'm having this authentication error.

. Docker logs

server | [2021-03-30T11:49:22.346Z] [DEBUG] : mongo.connection connect Creating connection to MongoDB - data: "mongodb://user:password@database:27017/dbname?authSource=admin"

mongo | {"t":{"$date":"2021-03-30T11:49:22.701+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn2","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","principalName":"user","authenticationDatabase":"admin","client":"172.30.0.4:32986","result":"AuthenticationFailed: SCRAM authentication failed, storedKey mismatch"}}

mongo | {"t":{"$date":"2021-03-30T11:49:22.708+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn1","msg":"Connection ended","attr":{"remote":"172.30.0.4:32984","connectionId":1,"connectionCount":1}}

server | [2021-03-30T11:49:22.712Z] [WARN] : mongo.connection _registerConnectionEvents MongoDB disconnected

server | [2021-03-30T11:49:22.715Z] [ERROR] : mongo.connection connect MongoDB connection error - data: {"type":"MongoError","message":"Authentication failed."}

server | [2021-03-30T11:49:22.717Z] [ERROR] : mongo.connection _registerConnectionEvents MongoDB event error - data: {"type":"MongoError","message":"Authentication failed."}

. docker-compose.yml

...

database:
    image: mongo
    container_name: mongo
    restart: always
    ports: 
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=some_user
      - MONGO_INITDB_ROOT_PASSWORD=some_password
    volumes:
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - ./mongo-volume:/data/db

redis:
    ...

server:
    build: .
    container_name: server
    depends_on:
      - database
      - redis
    environment:
      - DB_HOST=database
      - DB_PORT=27017
      - DB_NAME=dbname
      - DB_USER=user
      - DB_PASS=password
      - DB_POOL_SIZE=5,
      - DB_CONNECTION_RETRY_INTERVAL=5000
      - PORT=3000
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=password
    ports:
      - "3000:3000"
    volumes:
      - .:/usr/app

...

. mongo-init.js

db.auth("some_user", "some_password");
db.createUser(
    {
        user: "user",
        pwd: "password",
        roles: [
            {
                role: "readWrite",
                db: "dbname"
            }
        ]
    }
);

. Connection URL on Node

_getConnectionUrl() {
    const { DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS } = process.env;
    return `mongodb://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?authSource=admin`;
}
0 Answers
Related