Search code examples
mysqlnestjstypeorm

typeorm "CREATE DATABASE" migration


I need to create database before connection and work with db. I'm using nest.js typeorm, provided all configurations. When I start my application it says "Unable to connect to the database. Error: ER_BAD_DB_ERROR: Unknown database 'test'".

Once again: there is not DB "Test" in my MySQL Workbench => when I start the application I want the application to create the database itself (not by me manually)

Is it possible?


Solution

  • NestJS can't create the database, you need to manually create it before starting your application.

    If you want it to be automatic, you can use a docker service to create your database when starting your docker compose with docker-compose up

    version: "3.6"
    
    services:
      db:
        image: mysql:8.0.20
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        ports:
          - 3306:3306
        environment:
          - MYSQL_DATABASE=<database-name>
          - MYSQL_ROOT_PASSWORD=<password>