MongoError: Authentication failed

I am getting MongoError: Authentication failed error when I am trying to connect my node.js app container to a local mongo container.
error:

api        |     MongoError: Authentication failed.
api        |     at MessageStream.messageHandler (/home/app/node_modules/mongodb/lib/cmap/connection.js:272:20)
api        |     at MessageStream.emit (events.js:375:28)
api        |     at processIncomingData (/home/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
api        |     at MessageStream._write (/home/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
api        |     at writeOrBuffer (internal/streams/writable.js:358:12)
api        |     at MessageStream.Writable.write (internal/streams/writable.js:303:10)
api        |     at Socket.ondata (internal/streams/readable.js:726:22)
api        |     at Socket.emit (events.js:375:28)
api        |     at addChunk (internal/streams/readable.js:290:12)
api        |     at readableAddChunk (internal/streams/readable.js:265:9)
api        |     at Socket.Readable.push (internal/streams/readable.js:204:10)
api        |     at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
api        |   ok: 0,
api        |   code: 18,
api        |   codeName: 'AuthenticationFailed'
api        | }

docker-compose.yml

version: "3.9"

services:
  api:
    restart: always
    build:
      context: ./server
      dockerfile: Dockerfile.dev
    container_name: api
    volumes:
      - /home/app/node_modules
      - ./server:/home/app
    ports:
      - 8000:5000
    depends_on:
      - mongodb
    environment:
      NODE_ENV: ${NODE_ENV}
      MONGO_URI: mongodb://${MONGO_ROOT_USERNAME}:${MONGO_ROOT_PASSWORD}@mongodb
      CHOKIDAR_USEPOLLING: "true"
    networks:
      - lib_api

  mongodb:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
    volumes:
      - mongodb-data:/data/db
    networks:
      - lib_api

networks:
  lib_api:
    driver: bridge

volumes:
  mongodb-data:
    driver: local

dbconfig.js

const mongoose = require("mongoose");

mongoose
    .connect(process.env.MONGO_URI, {
        dbName: process.env.DB_NAME,
        useCreateIndex: true,
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useFindAndModify: false,
    })
    .then(() => console.log("database connected"))
    .catch((err) => console.log(err));

.env

NODE_ENV=developement
MONGO_ROOT_USERNAME=niranjan
MONGO_ROOT_PASSWORD=password
DB_NAME=library

Can somebody tell me where I am wrong?

1 Like

Any funny characters in the password that may need URL-encoding?

Also, Mongo defaults to using SCRAM. Is Mongoose handling that for you?

And the MongoDB documentation states you may need to use mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}. If true, then again: does Mongoose translate that for you?

I don’t think this causes your problem, but regardless: you’ve got a typo in the word development.

It is as simple as this: mongodb://${username}:${password}@localhost:27017/database?authSource=${password}