Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown database - [TypeOrmModule] Unable to connect to the database

Tags:

typeorm

nestjs

I'm writing my first project with Nestjs and I'm having trouble connecting to the database. I installed mysql and set up the connection. TypeOrm creates the database itself? or do i have to do it with mysql?

Nest] 13684   - 07/24/2020, 12:44:50 AM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +42ms
Error: ER_BAD_DB_ERROR: Unknown database 'my-database'

app.module.ts

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      username: 'root',
      password: 'password',
      database: 'my-database',
      entities: [User],
      synchronize: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
}) 
like image 581
JanuszFrontEnd'u Avatar asked Oct 27 '25 07:10

JanuszFrontEnd'u


1 Answers

First you should create the my-database database in your mysql server.

The TypeOrmModule create only tables (in in first time.)

  1. Create database in your mysql server: CREATE DATABASE myDatabase; (I recommend, use this format: myDatabase.)
  2. Restart the Nestjs.

That's it!

like image 107
Levente Avatar answered Oct 29 '25 09:10

Levente