mysqldockerdocker-composebackendprisma

Database `my-db` does not exist on the database server at `db:3306`


I have two Docker containers, one for the backend (Node.js) and another for the MySQL database. I managed to connect the two, as I received the error "Make sure the database is running at port 3306" when changing the Prisma URL from db:3306 to localhost:3306, indicating that the connection was established correctly.

However, after verifying the connection, I am facing a new problem when trying to establish the Prisma connection with the database container. I receive the error "Database itm-db does not exist on the database server at db:3306".

Soo i had to verify if the database 'itm-db' was created inside mysql container, and it was there

Here is my configuration

Prisma Schema---------

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = "mysql://root:mypass@db:3306/itm-db"
}

Docker Compose-----------
version: '3'
    services:
      backend:
        build:
          context: ./Backend
          dockerfile: Dockerfile
        ports:
          - '3000:3000'
        depends_on:
          - db
        volumes:
          - ./Backend:/app
      db:
        image: mysql:latest
        restart: always
        environment:
          - MYSQL_DATABASE="itm-db"
          - MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
        ports:
          - '3306:3306'
        expose:
          - '3306'
        volumes:
          - mysql_data:/var/lib/mysql
      frontend:
        build:
          context: ./Frontend
          dockerfile: Dockerfile
        ports:
          - '80:80'
        environment:
          - VITE_API_URL=http://backend:3000
        volumes:
          - './Frontend:/app'
    
    volumes:
      mysql_data:

Any insights into what might be causing the issue and how to resolve it would be greatly appreciated. Thank you!

I tried to verifiy if 'itm-db' was created inside container and i tried to remove all volumes and images and create from scratch


Solution

  • This post helped me:Docker compose not passing variables to container in service

    The problem wasn't the network as docker can handle communication on containers created on the same docker compose file. The problem was how i wrote environments variables. Quotes are not special characters on this context.

    Wrong

    environment:
      - my_environment:"1234"
    

    Right

    environment:
      - my_environment:1234