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
Configure
schema.prismaprovider to MySQL. - 2 Add a Shadow Database URL (Recommended for schema diffing).
-
3
Securely update your
.envfile. - 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.
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.
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.
- Log in to cPanel and find Remote MySQL.
- Add your current IP address to the access list.
- Update
HOSTin your .env to your domain/IP.
Scenario B: Local Production
Using localhost for production securely.
- Set
HOSTtolocalhost. - Ensure strict firewall rules on your server.
- 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