Prisma MySQL Setup

A comprehensive guide to integrating MySQL with Prisma PHP. Configure your schema, manage environments, and secure your connections.

Configuration Workflow

Follow these core steps to get started.

  1. 1 Configure schema.prisma provider to MySQL.
  2. 2 Add a Shadow Database URL (Recommended for schema diffing).
  3. 3 Securely update your .env file.
  4. 4 Run migrations to sync your schema.

Why use a Shadow Database?

The shadowDatabaseUrl is essential when working with cloud databases. It allows Prisma to detect schema drift by creating a temporary database, applying migrations, and checking the result without ever touching your production data.


Schema Configuration

Define your datasource in schema.prisma.

schema.prisma
generator client {
    provider = "prisma-client-js"
}
    
datasource db {
    provider          = "mysql"
}

Environment Variables

Configure your connection strings in the .env file. Replace the placeholders with your actual credentials.

.env
DATABASE_URL="mysql://USER:PASSWORD@HOST:PORT/DATABASE"
SHADOW_DATABASE_URL="mysql://USER:PASSWORD@HOST:PORT/SHADOW_DATABASE"

Scenario A: Remote Database (cPanel)

Connecting to a live server from local.

  1. Log in to cPanel and find Remote MySQL.
  2. Add your current IP address to the access list.
  3. Update HOST in your .env to your domain/IP.

Scenario B: Local Production

Using localhost for production securely.

  1. Set HOST to localhost.
  2. Ensure strict firewall rules on your server.
  3. This is more secure as the DB is not exposed to the public web.
Environment Config

While separating production and shadow databases is best practice, in a local XAMPP environment, you can technically use the same database for both if resources are limited. Just ensure your .env URLs reflect this.

Ready to Migrate?

You have successfully integrated MySQL with Prisma PHP. The next step is to apply these changes to your database structure.

Go to Prisma Command