Authentication Failed for mongodb cluster in docker-compose

Hello everyone, I need to run replica set locally because I am using Prisma.

So my setup was as following

MONGODB_URI="mongodb://ruser:rpassword@localhost:27017/database?authSource=admin&directConnection=true"

//Docker-compose
mongodb:
image: 'bitnami/mongodb:latest'
environment:
  - MONGODB_ADVERTISED_HOSTNAME=127.0.0.1
  - MONGODB_REPLICA_SET_MODE=primary
  - MONGODB_ROOT_USER=ruser
  - MONGODB_ROOT_PASSWORD=rpassword
  - MONGODB_DATABASE=database
  - MONGODB_REPLICA_SET_KEY=replicasetkey123
ports:
  - '27017:27017'
volumes:
  - 'mongo-db:/bitnami/mongodb'


// Connection code
export default async (): Promise<void> => {
    LoggerInstance.info("Connecting to database at %s", config.databaseUrl);
    await mongoose.connect(config.databaseUrl!);
    
};

But when I try to run my app, I am getting this error

 info:    Connecting to database at mongodb://ruser:rpassword@localhost:27017/database?authSource=adm
rectConnection=true
 /monorepo/node_modules/mongoose/node_modules/mongodb/lib/cmap/con
on.js:207
                     callback(new error_1.MongoServerError(document));
                              ^

 MongoServerError: Authentication failed.
     at Connection.onMessage (/monorepo/node_modules/mongoose/node
les/mongodb/lib/cmap/connection.js:207:30)
     at MessageStream.<anonymous> (/monorepo/node_modules/mongoose
_modules/mongodb/lib/cmap/connection.js:60:60)
     at MessageStream.emit (node:events:390:28)
     at MessageStream.emit (node:domain:475:12)
     at processIncomingData (/monorepo/node_modules/mongoose/node_
es/mongodb/lib/cmap/message_stream.js:132:20)
     at MessageStream._write (/monorepo/node_modules/mongoose/node
les/mongodb/lib/cmap/message_stream.js:33:9)
     at writeOrBuffer (node:internal/streams/writable:389:12)
     at _write (node:internal/streams/writable:330:10)
     at MessageStream.Writable.write (node:internal/streams/writable:334:10)
     at Socket.ondata (node:internal/streams/readable:754:22) {
   ok: 0,
   code: 18,
   codeName: 'AuthenticationFailed',
   '$clusterTime': {
     clusterTime: Timestamp { low: 1, high: 1676708807, unsigned: true },
     signature: {
       hash: Binary {
         sub_type: 0,
         buffer: Buffer(20) [Uint8Array] [
            51, 129, 179, 197, 133, 216,
           110, 109, 120,  20,  24, 215,
           211,  82,  72,   5, 192,  89,
           198,  39
         ],
         position: 20
       },
       keyId: Long { low: 7, high: 1676706790, unsigned: false }
     }
   },
   operationTime: Timestamp { low: 1, high: 1676708807, unsigned: true },
   [Symbol(errorLabels)]: Set(1) { 'HandshakeError' }
 }
 [nodemon] app crashed - waiting for file changes before starting...

The server connecting to this db is not inside docker-compose,

My suspicion is that the root user is not present in the database DB but rather admin DB, but not sure how to connect to one and use the other

Add the authSource option to your connection string. authSource=admin

mongodb://ruser:rpassword@localhost:27017/database?authSource=admin&directConnection=true

That is already present in the connection URL :confused:

@chris

Possibly the opposite then, set it to database I’m not familiar with the bitnami image and how they work.