I’m encountering an authentication issue while trying to use MongoDB via a Docker image. Here are the relevant sections from my configuration files:
application.yml:
springs:
data:
mongodb:
username: cihatsaman
password: cihatsaman
host: localhost
port: 27017
database: cihatiscoding
authentication-database: admin
docker-compose.yml:
services:
mongodb:
image: mongo
container_name: mongo_db
ports:
- 27017:27017
volumes:
- mongo:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=cihatsaman
- MONGO_INITDB_ROOT_PASSWORD=cihatsaman
mongo_express:
image: mongo-express
container_name: mongo_express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMIN_USERNAME=cihatsaman
- ME_CONFIG_MONGODB_ADMIN_PASSWORD=cihatsaman
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
mongo: {}
The issue arises with the following error message:
Caused by: com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): ‘Command insert requires authentication’ on server localhost:27017. The full response is {“ok”: 0.0, “errmsg”: “Command insert requires authentication”, “code”: 13, “codeName”: “Unauthorized”}
I’ve followed the usual steps for setting up authentication in both my application.yml and docker-compose.yml files. Can anyone help me identify the root cause of this authentication error and suggest a solution? Thanks!