[Docker/Mongodb] Authentication Failed when connecting mongodb server even if you set correct name and password

Docker

Artifact

Problem

I create this artifact by docker compose

before executing compose inside directory

$ ls -a
docker-compose.yml .env

after

$ ls -a
configdb db docker-compose.yml .env

docker-compose.yaml

version: "3"
services:
  mongodb:
    container_name: "mongodb"
    hostname: mongodb
    image: mongo
    restart: always
    ports:
      - 27017:27017
    volumes:
      - ./db:/data/db
      - ./configdb:/data/configdb
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${DB_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${DB_PASSWORD}

you make sure that configdb and db are created after docker compose command.

But I could not connect because of Authentication Failer

I tried to execute docker compose down and set .env at proper location and re-execute docker compose up. But Authentication didn't work and always emitted failer !!.

The mistake was that not preparing .env in mongo_db directory.

Reason

I made a mistake when creating db by docker-compose.

You have to remove db and configdb in this directory to not load cached data.

After removing these files or directories and I re-execute docker compose up to restart mongo db process.

I could connect mongo db!

Happy coding!

コメント

タイトルとURLをコピーしました