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:
I really don’t understand what is going on. What am I missing.
Sorry for the dumb question…
2
Answers
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.
I think it might be better to set ports to
So you don’t need to delete anything from your system.
Now you can connect as ussual, just change port to
27012
instead of27017
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.