Can't connect my Nest.JS project to dockerized mongodb database

157 Views Asked by At

I have a dockerized database :

 mongodb:
    image: mongo:latest
    container_name: mongoose-db
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: 1234
    volumes:
      - mongodb_data:/data/db

I have a Nest.JS project running on http://localhost:3000/ and not inside a dockerized stack.

I want to correctly connect my Nest.JS project to the database :

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MongooseModule.forRoot('mongodb://root:1234@localhost:27017', {
      dbName: 'myappdb',
      auth: {
        username: 'root',
        password: '1234',
      },
    }),
    UsersModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {

}

I tried this confi :

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MongooseModule.forRoot('mongodb://root:[email protected]:27017/myappdb'),
    UsersModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

I'm getting this error :

[Nest] 16036 - 20/08/2023 14:40:34 ERROR [ExceptionHandler] Authentication failed. MongoServerError: Authentication failed. at Connection.onMessage (D:\Mes_POCS\token-auth-app\node_modules\mongodb\src\cmap\connection.ts:450:20) at MessageStream. (D:\Mes_POCS\token-auth-app\node_modules\mongodb\src\cmap\connection.ts:268:56) at MessageStream.emit (node:events:514:28) at processIncomingData (D:\Mes_POCS\token-auth-app\node_modules\mongodb\src\cmap\message_stream.ts:194:14) at MessageStream._write (D:\Mes_POCS\token-auth-app\node_modules\mongodb\src\cmap\message_stream.ts:71:5) at writeOrBuffer (node:internal/streams/writable:392:12) at _write (node:internal/streams/writable:333:10) at MessageStream.Writable.write (node:internal/streams/writable:337:10) at Socket.ondata (node:internal/streams/readable:766:22) at Socket.emit (node:events:514:28)

What is the correct database url ?

4

There are 4 best solutions below

0
Khaled Boussoffara On BEST ANSWER

It was a problem in docker compose conf file :

  mongodb:
    image: mongo:latest
    container_name: mongoose-db
    ports:
      - "27017:27017"
    environment:
      # MONGO_INITDB_ROOT_USERNAME: root
      # MONGO_INITDB_ROOT_PASSWORD: 1234
      MONGODB_USER: root
      MONGODB_DATABASE: db
      MONGODB_PASS: 1234
    volumes:
      - mongodb_data:/data/db

And the good root is :

MongooseModule.forRoot('mongodb://localhost:27017/db'),
4
Robin Thomas On

You need to use 127.0.0.1 instead of localhost in your mongoose connection.

For local MongoDB databases, we recommend using 127.0.0.1 instead of localhost. That is because Node.js 18 and up prefer IPv6 addresses, which means, on many machines, Node.js will resolve localhost to the IPv6 address ::1 and Mongoose will be unable to connect, unless the mongodb instance is running with ipv6 enabled.

https://mongoosejs.com/docs/connections.html

That means, you need to use:

MongooseModule.forRoot('mongodb://127.0.0.1:27017/myappdb', {
  authSource: 'admin',
  user: 'root',
  pass: '1234',
})
1
Shekhar Kumar On

According to the error that has been displayed the Credentials for mongo dB aren't corrects do double check please.

0
Hai Alison On

You should use mongodb (the same as the service name in docker-compose) the connection will be:

MongooseModule.forRoot('mongodb://mongodb:27017/myappdb'),