MySQL Integration

Docker MySQL Environment

Configure a robust, containerized database workflow. Ensure consistent MySQL versions and networking across your development and production environments.

Docker Configuration

Define your services in docker-compose.yml. This setup links your PHP application (web) with a MySQL container (db).

docker-compose.yml
version: "3.8"
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "80:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: prisma_php
      MYSQL_DATABASE: prisma_php
    ports:
      - "3306:3306"

Environment Variables

Update your .env file. You need distinct connection strings: one for external access (localhost) and one for internal Docker networking.

.env
# For Prisma ORM (CLI running on host machine)
DATABASE_URL="mysql://root:prisma_php@localhost:3306/prisma_php"

# For Prisma PHP ORM (Running inside Docker container)
# Note host is 'db' (service name) not 'localhost'
PRISMA_PHP_ORM_DATABASE_URL="mysql://root:prisma_php@db:3306/prisma_php"

Update Prisma Schema

Change the provider to mysql.

prisma/schema.prisma
datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

Update Prisma PHP Config

Point the ORM to the Docker-internal URL.

Lib/Prisma/Classes/Prisma.php
$databaseUrl = $_ENV['PRISMA_PHP_ORM_DATABASE_URL'];

if (!$databaseUrl) {
    throw new \Exception('PRISMA_PHP_ORM_DATABASE_URL not set.');
}

Generate Client

After updating your configuration, remember to regenerate the client files by running npx ppo generate.