3

I have a problem when trying to connecting my express js with mongodb on docker-compose environment. I have already tried multiple solutions on the internet but it did not solve my problem. This is my express js docker log:

MongoServerError: Authentication failed.
    at Connection.onMessage (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:207:30)
    at MessageStream.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/connection.js:60:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:132:20)
    at MessageStream._write (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:766:22)
    at Socket.emit (node:events:513:28) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  connectionGeneration: 24,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}

This is mongodb log:

Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"tausr","authenticationDatabase":"admin","remote":"172.18.0.9:55028","extraInfo":{},"error":"UserNotFound: Could not find user \"tausr\" for db \"admin\"

This is my docker-compose:

    express:
        build: ./express
        restart: on-failure
        depends_on:
          - mongo
          - couchdb
        env_file: ./.env
        environment:
          - DB_HOST=mongo
          - DB_USERNAME=$MONGODB_USER
          - DB_PASSWORD=$MONGODB_PASSWORD
          - DB_PORT=$MONGODB_DOCKER_PORT
          - DB_NAME=$MONGODB_DATABASE
        ports:
          - "3000:3000"
        links:
         - mongo
        networks:
          - logging
      mongo:
        image: mongo:latest
          #    command: [--auth]
        env_file: ./.env
        environment:
          - MONGO_INITDB_ROOT_USERNAME=$MONGODB_USER
          - MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD
            #      MONGO_INITDB_USERNAME: "taUser"
            #      MONGO_INITDB_PASSWORD: "taPass"
          - MONGO_INITDB_DATABASE=$MONGODB_DATABASE
        volumes:
          - ./data:/data/db
            #- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js
          ports:
      - '27018:27017'
    networks:
      - logging

This is my .env file

MONGODB_USER=tausr
MONGODB_PASSWORD=tapass
MONGODB_DATABASE=ta
MONGODB_LOCAL_PORT=27018
MONGODB_DOCKER_PORT=27017

NODE_LOCAL_PORT=6868
NODE_DOCKER_PORT=8080

and this is my mongo connection

mongoose.connect(`mongodb://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?authSource=admin`).then((response) => console.log("connect success")).catch((e) => console.log(e))
Ruli
  • 2,592
  • 12
  • 30
  • 40
  • I have no clue about docker, but it looks like you create the root user in database `ta` rather than in `admin`. See https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Nov 21 '22 at 20:25
  • Please don't paste screenshots, use formatted text. See https://meta.stackoverflow.com/q/285551 – Wernfried Domscheit Nov 21 '22 at 20:26
  • @WernfriedDomscheit hey thank you for the reply, but i already tried to change the database user or create it under admin(without inserting MONGODB_INITDB_DB or changing authSource database to ```ta```) but it seems not working at all – Dary Winata Nov 22 '22 at 01:20
  • Could it be that the database has already been initialized? In that case your INITDB environment variables won't do anything. You can find out by changing your volume to a different place on disk, or removing it altogether, and seeing if the auth works in that case. – Aurast Nov 22 '22 at 01:32
  • @Aurast oh yeah i think my volume is created first and then i used it repeatedly, can u tell me how to clean the volume? can i just remove file on volume directory? – Dary Winata Nov 22 '22 at 01:44
  • Yes, you can delete your `./data` directory, if you are okay with losing all data and starting a new database from zero. – Aurast Nov 22 '22 at 01:51

0 Answers0