skip to Main Content

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_POCStoken-auth-appnode_modulesmongodbsrccmapconnection.ts:450:20)
at MessageStream. (D:Mes_POCStoken-auth-appnode_modulesmongodbsrccmapconnection.ts:268:56)
at MessageStream.emit (node:events:514:28)
at processIncomingData (D:Mes_POCStoken-auth-appnode_modulesmongodbsrccmapmessage_stream.ts:194:14)
at MessageStream._write (D:Mes_POCStoken-auth-appnode_modulesmongodbsrccmapmessage_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 ?

3

Answers


  1. Chosen as BEST ANSWER

    It was an 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'),
    

  2. 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',
    })
    
    Login or Signup to reply.
  3. According to the error that has been displayed the Credentials for mongo dB aren’t corrects do double check please.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search