MongoError: Authentication failed with docker-compose - mongodb

I am trying to build this app https://github.com/dwgebler/node-express-example. I want to use it as a template for making a Mongodb http express server. I tried to build the docker compose file in this repo which looks like this (modified slightly):
version: "3.8"
services:
database:
image: mongo
container_name: mongo-database
restart: always
volumes:
- "mongodata:/data/db"
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_SERVER: mongo-database
web:
image: node:16-alpine
container_name: node-server
restart: always
user: "node"
ports:
- "9443:443"
volumes:
- "./app:/var/app/"
working_dir: "/var/app"
environment:
NODE_ENV: dev
DB_USER: root
DB_PASSWORD: example
DB_NAME: mongo-database
command: "./wait.sh mongo-database 27017 'npm start'"
volumes:
mongodata:
But when I run the command docker compose up -d in the same directory as the docker-compose.yml file, I get this in the "node-server" container logs:
Mongo started
MongoError: Authentication failed.
at MessageStream.messageHandler (/var/app/node_modules/mongodb/lib/cmap/connection.js:272:20)
at MessageStream.emit (node:events:513:28)
at processIncomingData (/var/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/var/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at writeOrBuffer (node:internal/streams/writable:391:12)
at _write (node:internal/streams/writable:332:10)
at MessageStream.Writable.write (node:internal/streams/writable:336:10)
at Socket.ondata (node:internal/streams/readable:754:22)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:315:12) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed'
}
Why am I getting a MongoError: Authentication failed? Notice that I set
DB_USER: root
DB_PASSWORD: example
DB_NAME: mongo-database
In the web section of the docker file which matches the MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD, from the database section and I set DB_NAME to mongo-database which matches the database section name. So why isn't it authenticating?

MONGO_INITDB_ROOT_USERNAME and PASSWORD create a user in the Mongo admin database. Your connection string tries to authenticate against the mongo-database database and it can't find the user. There are messages in the Mongo logs that tell you this.
To fix it, you need to authenticate against the admin database by adding authSource=admin to your connection string, like this
const uri = `mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}#mongo-database/${process.env.DB_NAME}?retryWrites=true&writeConcern=majority&authSource=admin`;

Related

Getting "MongoServerError: Authentication failed" when connecting to MongoDB in Docker container

Here is my docker-compose file:
version: '3' services: mongo:
image: mongo
container_name: mongo
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME: admin
- MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- ./mongo-data:/data/db
command: --wiredTigerCacheSizeGB 1.5
I try to connect to Mongo container but constantly receive the same error:
MongoServerError: Authentication failed
Here are 2 variants of connection strings I've used, neither worked:
mongodb://admin:root#localhost:27017/admin
mongodb://admin:root#localhost:27017/admin?authSource=admin
What can I try next?

Mongo image does not created with credentials

I am creating mongo:latest container on my machine using docker-compose.
This is my docker-compose.yaml:
version: '3.6'
services:
backend:
image: backend:1.0.0
container_name: backend
ports:
- '5000:5000'
links:
- mongodb
environment:
NODE_ENV: PRODUCTION
API_PORT: 5000
MONGO_USERNAME: root
MONGO_PASSWORD: rootPassXXX
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_DATABASE: automatic
JWT_TOKEN: Auto
scheduler:
image: scheduler:1.0.0
container_name: scheduler
links:
- mongodb
environment:
NODE_ENV: PRODUCTION
API_PORT: 5000
MONGO_USERNAME: root
MONGO_PASSWORD: rootPassXXX
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_DATABASE: automatic
mongodb:
image: mongo
container_name: mongodb
expose:
- 27017
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=rootPassXXX
volumes:
- ./conf/mongodb:/data/db
volumes:
autoc:
external: true
networks:
default:
external:
name: automatic
After creating those containers I am receiving from them an error: MongoServerError: Authentication failed.
This is the URI that the application is trying to connect: mongodb://root:rootPassXXX#mongodb:27017/matiautomai.
While trying to investigate what was happening, I was trying to log in to the same mongo container
that initializes with the user name and password variables without the credentials using that way: mongodb://mongodb:27017/automatic, And the application manages to connect to it.
I've reviewed the docs inside the Docker hub and it seems to be that I used the environment variables in the correct way.
What am I missing here?
After seeing your helpful comments, I found the solution for this issue.
I needed to initialize a one-time docker-compose with an initial script that located inside.
docker-compose.yaml:
version: '3.7'
services:
mongodb:
container_name: mongodb
image: mongo:latest
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: root-db
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
This is the script that located inside ./docker-entrypoint-initdb.d/mongo-init.js:
print('start ~~~~');
db = db.getSiblingDB('yourDBname');
db.createUser(
{
user: 'username',
pwd: 'password',
roles: [{ role: 'readWrite', db: 'yourDBname' }],
},
);
db.createCollection('users');
print('end ~~~~');
After that, you will be able to connect to mongo with the credential you have set in the .js file.

docker-compose service name problem using mongo official image

I followed official mongo image description trying to using it with docker-compose.But i've noticed that the mongo service name can not be the same as the image name.Here is the situation:
created a docker-compose.yml file which filled with this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#mongo:27017/
execute docker-compose
docker-compose -f docker-compose.yml up
Then the error appears:
Could not connect to database using connectionString: mongodb://root:example#mongo:27017/"
mongo-express_1 | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19)
mongo-express_1 | at Connection.emit (events.js:314:20)
But when I change the service name mongo to myMongo,it just work out!:
version: '3.1'
services:
myMongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#myMongo:27017/
So, where am i wrong?

MongoDB authentication fails in Docker

My docker-compose.yml
mongo:
image: mongo
volumes:
- ./mongo:/data/db
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: pass
ME_CONFIG_MONGODB_URL: mongodb://root:pass#mongo:27017/
When I run docker-compose up it logs the following errors
for the mongo service:
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
and for the mongo-express service:
"error":"UserNotFound: Could not find user "root" for db "admin"
I tried adding ?authSource=admin to the ME_CONFIG_MONGODB_URL but the error remains.

Can't connect to MongoDB inside docker container

I'm trying to connect to a mongodb container inside docker but I'm getting this error:
getaddrinfo ENOTFOUND mongo.ks.local
The database works just fine when I access it from docker but I'm not able to access it from MongoDBCompass
This is my docker-compose.yml file:
version: '3.8'
services:
mongo:
image: mongo
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: <username>
MONGO_INITDB_ROOT_PASSWORD: <password>
ports:
- 27017:27017
volumes:
- ks_mongodb:/data/db
I'm quite new to docker and will appreciate any help I can get
You need the ip address of the container:
try:
docker inspect <CONTAINER NAME>
And search for the ip address. Then use the ip address to connect to mongodb
I have tried connecting to the container using mongo compass. It failed but when I removed the line of mounting the volume and tried again it connects to the database.
Then tried to mount the volume like below it worked. I have changed the username and password and specified the INITDB env variable.
version: "3.8"
services:
mongo:
image: mongo:latest
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 1234
MONGO_INITDB_DATABASE: admin
ports:
- 27017:27017
volumes:
- /data/db