Skip to main content

Connect your database

To connect your database, you need to set the url field of the datasource block in your Prisma schema to your database connection URL:

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

Note that the default schema created by prisma init uses PostgreSQL, so you first need to switch the provider to mysql:

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

In this case, the url is set via an environment variable which is defined in .env:

.env
DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
info

We recommend adding .env to your .gitignore file to prevent committing your environment variables.

You now need to adjust the connection URL to point to your own database.

The format of the connection URL for your database typically depends on the database you use. For MySQL, it looks as follows (the parts spelled all-uppercased are placeholders for your specific connection details):

mysql://USER:PASSWORD@HOST:PORT/DATABASE

Here's a short explanation of each component:

  • USER: The name of your database user
  • PASSWORD: The password for your database user
  • PORT: The port where your database server is running (typically 3306 for MySQL)
  • DATABASE: The name of the database

As an example, for a MySQL database hosted on AWS RDS, the connection URL might look similar to this:

.env
DATABASE_URL="mysql://johndoe:XXX@mysql–instance1.123456789012.us-east-1.rds.amazonaws.com:3306/mydb"

When running MySQL locally, your connection URL typically looks similar to this:

.env
DATABASE_URL="mysql://root:randompassword@localhost:3306/mydb"