skip to Main Content

I’ve created the following docker-compose.yml:

version: "3"
services:
  mongo:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
    ports:
      - '27017:27017'

I then start my containers:

docker-compose up

then I try to connect into MongoDb Compass(also tried through c# code), with the following:

  • mongodb://admin:admin@localhost:27017/?authSource=admin
  • mongodb://admin:admin@localhost:27017
  • mongodb://admin:[email protected]:27017

But I always get a "Authentication failed" message:

enter image description here

I really don’t understand what is going on. What am I missing.

Sorry for the dumb question…

2

Answers


  1. The behavior you’re seeing suggests that there is already another mongodb instance running on your system (with different authentication credentials). Stop the Docker container and check to see if there is still a mongodb service listening on port 27017.

    Login or Signup to reply.
  2. I think it might be better to set ports to

    ports:
       - '27012:27017'
    

    So you don’t need to delete anything from your system.
    Now you can connect as ussual, just change port to 27012 instead of 27017

    Also this way you can run multiple monogdb’s using more ports wthout any issues. In my case I still needed local mongodb running and because of that port 27017 was automatically connecting to it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search