Skip to content

Commit

Permalink
Merge branch 'main' into railway-update
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur-arch authored Mar 22, 2024
2 parents 57c9ab0 + 4ce4b66 commit d55b360
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 71 deletions.
43 changes: 36 additions & 7 deletions content/200-orm/050-overview/500-databases/400-mysql.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
title: 'MySQL'
title: 'MySQL/MariaDB'
metaTitle: 'MySQL database connector'
metaDescription: 'This page explains how Prisma can connect to a MySQL database using the MySQL database connector.'
metaDescription: 'This page explains how Prisma can connect to a MySQL or MariaDB database using the MySQL database connector.'
tocDepth: 3
---

<TopBlock>

The MySQL data source connector connects Prisma ORM to a [MySQL](https://www.mysql.com/) database server.
The MySQL data source connector connects Prisma ORM to a [MySQL](https://www.mysql.com/) or [MariaDB](https://mariadb.org/) database server.

By default, the MySQL connector contains a database driver responsible for connecting to your database. You can use a [driver adapter](/orm/overview/databases/database-drivers#driver-adapters) (Preview) to connect to your database using a JavaScript database driver from Prisma Client.

Expand All @@ -26,7 +26,7 @@ datasource db {

The fields passed to the `datasource` block are:

- `provider`: Specifies the `mysql` data source connector.
- `provider`: Specifies the `mysql` data source connector, which is used both for MySQL and MariaDB.
- `url`: Specifies the [connection URL](#connection-url) for the MySQL database server. In this case, an [environment variable is used](/orm/prisma-schema/overview#accessing-environment-variables-from-the-schema) to provide the connection URL.

## Connection details
Expand All @@ -50,7 +50,7 @@ The following components make up the _base URL_ of your database, they are alway
| Name | Placeholder | Description |
| :------- | :---------- | :------------------------------------------------------------------------------------------------------------------ |
| Host | `HOST` | IP address/domain of your database server, e.g. `localhost` |
| Port | `PORT` | Port on which your database server is running, e.g. `5432` |
| Port | `PORT` | Port on which your database server is running, e.g. `5432` (default is `3306`, or no port when using Unix socket) |
| User | `USER` | Name of your database user, e.g. `janedoe` |
| Password | `PASSWORD` | Password for your database user |
| Database | `DATABASE` | Name of the [database](https://dev.mysql.com/doc/refman/8.0/en/creating-database.html) you want to use, e.g. `mydb` |
Expand Down Expand Up @@ -112,8 +112,8 @@ mysql://USER:PASSWORD@HOST:PORT/DATABASE?sslidentity=client-identity.p12&sslpass

### Connecting via sockets

To connect to your MySQL database via sockets, you must add a `socket` field as a _query parameter_ to the connection URL (instead of setting it as the `host` part of the URI).
The value of this parameter then must point to the directory that contains the socket, e.g.: `mysql://USER:POST@localhost/database?socket=/var/run/mysql/`
To connect to your MySQL/MariaDB database via a socket, you must add a `socket` field as a _query parameter_ to the connection URL (instead of setting it as the `host` part of the URI).
The value of this parameter then must point to the directory that contains the socket, e.g. on a default installation of MySQL/MariaDB on Ubuntu or Debian use: `mysql://USER:POST@localhost/database?socket=/run/mysqld/mysqld.sock`

Note that `localhost` is required, the value itself is ignored and can be anything.

Expand All @@ -139,6 +139,20 @@ The MySQL connector maps the [scalar types](/orm/prisma-schema/data-model/models
| `Json` | `JSON` | Supported in MySQL 5.7+ only |
| `Bytes` | `LONGBLOB` |

### Native type mapping from Prisma ORM to MariaDB

| Prisma ORM | MariaDB | Notes |
| ---------- | ---------------- | -------------------------------------------------- |
| `String` | `VARCHAR(191)` | |
| `Boolean` | `BOOLEAN` | In MariaDB `BOOLEAN` is a synonym for `TINYINT(1)` |
| `Int` | `INT` | |
| `BigInt` | `BIGINT` | |
| `Float` | `DOUBLE` | |
| `Decimal` | `DECIMAL(65,30)` | |
| `DateTime` | `DATETIME(3)` | |
| `Json` | `LONGTEXT | See https://mariadb.com/kb/en/json-data-type/ |
| `Bytes` | `LONGBLOB` | |

### Native type mappings

When introspecting a MySQL database, the database types are mapped to Prisma ORM according to the following table:
Expand Down Expand Up @@ -204,3 +218,18 @@ model Device {
## Engine

If you are using a version of MySQL where MyISAM is the default engine, you must specify `ENGINE = InnoDB;` when you create a table. If you introspect a database that uses a different engine, relations in the Prisma Schema are not created (or lost, if the relation already existed).

## Permissions

A fresh new installation of MySQL/MariaDB has by default only a `root` database user. Do not use `root` user in your Prisma configuration, but instead create a database and database user for each application. On most Linux hosts (e.g. Ubuntu) you can simply run this as the Linux `root` user (which automatically has database `root` access as well):

```
mysql -e "CREATE DATABASE IF NOT EXISTS $DB_PRISMA;"
mysql -e "GRANT ALL PRIVILEGES ON $DB_PRISMA.* TO $DB_USER@'%' IDENTIFIED BY '$DB_PASSWORD';"
```

The above is enough to run the `prisma db pull` and `prisma db push` commands. In order to also run `prisma migrate` commands these permissions need to be granted:

```
mysql -e "GRANT CREATE, DROP, REFERENCES, ALTER ON *.* TO $DB_USER@'%';"
```
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ The following caveats apply:

The following table shows which referential action each database supports.

| Database | Cascade | Restrict | NoAction | SetNull | SetDefault |
| :---------- | :------ | :------- | :------- | :------ | :--------- |
| PostgreSQL | ✔️ | ✔️ | ✔️ | ✔️⌘ | ✔️ |
| MySQL | ✔️ | ✔️ | ✔️ | ✔️ | ❌ (✔️†) |
| SQLite | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| SQL Server | ✔️ | ❌‡ | ✔️ | ✔️ | ✔️ |
| CockroachDB | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| MongoDB†† | ✔️ | ✔️ | ✔️ | ✔️ ||
| Database | Cascade | Restrict | NoAction | SetNull | SetDefault |
| :------------ | :------ | :------- | :------- | :------ | :--------- |
| PostgreSQL | ✔️ | ✔️ | ✔️ | ✔️⌘ | ✔️ |
| MySQL/MariaDB | ✔️ | ✔️ | ✔️ | ✔️ | ❌ (✔️†) |
| SQLite | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| SQL Server | ✔️ | ❌‡ | ✔️ | ✔️ | ✔️ |
| CockroachDB | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| MongoDB†† | ✔️ | ✔️ | ✔️ | ✔️ ||

- † See [special cases for MySQL](#mysql).
- ⌘ See [special cases for PostgreSQL](#postgresql).
Expand All @@ -168,9 +168,9 @@ The following table shows which referential action each database supports.

Referential actions are part of the ANSI SQL standard. However, there are special cases where some relational databases diverge from the standard.

#### MySQL
#### MySQL/MariaDB

MySQL, and the underlying InnoDB storage engine, does not support `SetDefault`. The exact behavior depends on the database version:
MySQL/MariaDB, and the underlying InnoDB storage engine, does not support `SetDefault`. The exact behavior depends on the database version:

- In MySQL versions 8 and later, and MariaDB versions 10.5 and later, `SetDefault` effectively acts as an alias for `NoAction`. You can define tables using the `SET DEFAULT` referential action, but a foreign key constraint error is triggered at runtime.
- In MySQL versions 5.6 and later, and MariaDB versions before 10.5, attempting to create a table definition with the `SET DEFAULT` referential action fails with a syntax error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ result.forEach((x) => {
})
```
> **Note**: The Prisma Client query engine standardizes the return type for all databases. **Using the raw queries does not**. If the database provider is MySQL, the values are `1` or `0`. However, if the database provider is PostgreSQL, the values are `true`, `false`, or `NULL`.
For regular CRUD queries, the Prisma Client query engine standardizes the return type for all databases. **Using the raw queries does not**. If the database provider is MySQL, the returned values are `1` or `0`. However, if the database provider is PostgreSQL, the values are `true` or `false`.
> **Note**: Prisma sends JavaScript integers to PostgreSQL as `INT8`. This might conflict with your user-defined functions that accept only `INT4` as input. If you use `$queryRaw` in conjunction with a PostgreSQL database, update the input types to `INT8`, or cast your query parameters to `INT4`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ In order to create and delete the shadow database when using `migrate dev`, Pris
| Database | Database user requirements |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| SQLite | No special requirements. |
| MySQL | Database user must have `CREATE, ALTER, DROP, REFERENCES ON *.*` privileges |
| MySQL/MariaDB | Database user must have `CREATE, ALTER, DROP, REFERENCES ON *.*` privileges |
| PostgreSQL | The user must be a super user or have `CREATEDB` privilege. See `CREATE ROLE` ([PostgreSQL official documentation](https://www.postgresql.org/docs/12/sql-createrole.html)) |
| Microsoft SQL Server | The user must be a site admin or have the `SERVER` securable. See the [official documentation](https://docs.microsoft.com/en-us/sql/relational-databases/security/permissions-database-engine?view=sql-server-ver15). |

Expand Down
6 changes: 6 additions & 0 deletions content/200-orm/500-reference/250-error-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ The included usage of the current plan has been exceeded. This can only occur on

The global timeout of Accelerate has been exceeded. You can find the limit [here](/accelerate/limitations#query-timeout-limit).

> Also see the [troubleshooting guide](/accelerate/troubleshoot#p6004-querytimeout) for more information.

#### <inlinecode>P6005</inlinecode> (<inlinecode>InvalidParameters</inlinecode>)

The user supplied invalid parameters. Currently only relevant for transaction methods. For example, setting a timeout that is too high. You can find the limit [here](/accelerate/limitations#interactive-transactions-query-timeout-limit).
Expand All @@ -486,10 +488,14 @@ The chosen Prisma version is not compatible with Accelerate. This may occur when

The engine failed to start. For example, it couldn't establish a connection to the database.

> Also see the [troubleshooting guide](/accelerate/troubleshoot#p6008-connectionerrorenginestarterror) for more information.
#### <inlinecode>P6009</inlinecode> (<inlinecode>ResponseSizeLimitExceeded</inlinecode>)

The global response size limit of Accelerate has been exceeded. You can find the limit [here](/accelerate/limitations#response-size-limit).

> Also see the [troubleshooting guide](/accelerate/troubleshoot#p6009-responsesizelimitexceeded) for more information.
### Prisma Pulse

Prisma Pulse-related errors start with <inlinecode>P61xx</inlinecode>.
Expand Down
5 changes: 3 additions & 2 deletions content/200-orm/500-reference/375-supported-databases.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ An asterisk (\*) indicates that the version number is not relevant; either all v
| Database | Version |
| -------------------- | ------- |
| CockroachDB | 21.2.4+ |
| MariaDB | 10 |
| MariaDB | 10.0+ |
| MariaDB | 11.0+ |
| Microsoft SQL Server | 2017 |
| Microsoft SQL Server | 2019 |
| Microsoft SQL Server | 2022 |
| MongoDB | 4.2+ |
| MySQL | 5.6 |
| MySQL | 5.7 |
| MySQL | 8 |
| MySQL | 8.0 |
| PostgreSQL | 9.6 |
| PostgreSQL | 10 |
| PostgreSQL | 11 |
Expand Down
4 changes: 2 additions & 2 deletions content/300-accelerate/200-getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ To get started with Accelerate, you will need the following:

- A GitHub account.
- A project that uses [Prisma Client](/orm/prisma-client) `4.16.1` or higher. If your project is using interactive transactions, you need to use `5.1.1` or higher. (We always recommend using the latest version of Prisma.)
- A hosted PostgreSQL, MySQL, PlanetScale, CockroachDB, or MongoDB database.
- A hosted PostgreSQL, MySQL/MariaDB, PlanetScale, CockroachDB, or MongoDB database.

## 1. Enable Accelerate in a project

Expand Down Expand Up @@ -134,7 +134,7 @@ import { withAccelerate } from '@prisma/extension-accelerate'
const prisma = new PrismaClient().$extends(withAccelerate())
```

If you are going to deploy to an edge runtime (like Cloudflare Workers, Vercel Edge Functions, Deno Deploy, or Netlify Edge Functions), use our edge client instead:
If you are going to deploy to an edge runtime (like Cloudflare Workers, Vercel Edge Functions, Deno Deploy, or Supabase Edge Functions), use our edge client instead:

```ts
import { PrismaClient } from '@prisma/client/edge'
Expand Down
4 changes: 4 additions & 0 deletions content/300-accelerate/500-limitations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Below are descriptions of known limitations when using Accelerate. If you are aw

Accelerate has a global timeout of `10s` for each query. Reach out to [[email protected]](mailto:[email protected]) with your use case if your application requires a greater timeout value.

> Also see the [troubleshooting guide](/accelerate/troubleshoot#p6004-querytimeout) for more information.
## Interactive transactions query timeout limit

Accelerate has a global timeout of `15s` for each [interactive transaction](/orm/prisma-client/queries/transactions#interactive-transactions). Reach out to [[email protected]](mailto:[email protected]) with your use case if your application requires a greater timeout value.
Expand All @@ -24,6 +26,8 @@ Accelerate has a global timeout of `15s` for each [interactive transaction](/orm

Accelerate has a global response size limit of `5MB`. Reach out to [[email protected]](mailto:[email protected]) with your use case if your application requires a larger response size.

> Also see the [troubleshooting guide](/accelerate/troubleshoot#p6009-responsesizelimitexceeded) for more information.
## Cannot cache raw queries

At the moment, it is not possible to cache the responses of [raw queries](/orm/prisma-client/queries/raw-database-access/raw-queries).
Expand Down
Loading

0 comments on commit d55b360

Please sign in to comment.