Prisma MySQL Setup
Configuration Steps
- Configure your
schema.prisma
file for MySQL. - Add a shadow database URL in your
schema.prisma
if necessary. - Update the
.env
file with your database URLs, ensuring they are secure. - Create and apply migrations as necessary.
schema.prisma
Configuration
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}
The shadowDatabaseUrl
is optional and used for shadow databases, which are essential when you working with online databases, allowing you to test schema changes without impacting your production database.
.env
Configuration
DATABASE_URL="mysql://USER:PASSWORD@HOST:PORT/DATABASE"
SHADOW_DATABASE_URL="mysql://USER:PASSWORD@HOST:PORT/SHADOW_DATABASE"
Replace USER
, PASSWORD
, HOST
, PORT
, and DATABASE
with your actual MySQL database details to ensure a secure and correct connection.
If you have access to your CPANEL and want to work with a remote database using Remote Database Access
, you can use the following configuration:
- Add your IP address to the Remote MySQL Database Access Hosts.
- Update the
.env
file with the remote database URL. The URL will be the same as before, but theHOST
will be your domain or IP address.
If you want to use a local database instead of a remote database for production, you can use the following configuration:
- Update the
.env
file with the local database URL. The URL will be the same as before, but theHOST
will belocalhost
. - This configuration is only recommended for localhost in production environments. In local development environments like XAMPP, you can use the same host name as production localhost.
- Using a local database for production is more secure than using a remote database.
Note: The difference between using a remote and a localhost database for production is that the remote database is accessible from anywhere, while the localhost database is only accessible from the server where the database is hosted. This makes the localhost database more secure for production environments. The host name change is only necessary for production environments. In local development environments like XAMPP, you can use the same host name as production localhost.
**NOTE**
Having two databases, one for production and one for shadow, is recommended for testing schema changes without impacting production. However, in local development environments like using XAMPP and MySQL, it's possible to use the same database for both production and shadow. Ensure the .env
file is correctly updated with the appropriate URLs.
Conclusion
You have now successfully integrated MySQL with Prisma PHP in a XAMPP environment. This setup allows you to develop full-stack applications using Prisma as your ORM with MySQL as the database.
Next Step
Go to Prisma Command to migrate your database.