连接 URL
Prisma ORM 需要一个连接 URL 才能连接到你的数据库,例如 当使用 Prisma 客户端 发送查询或使用 Prisma 迁移 更改数据库架构时。
¥Prisma ORM needs a connection URL to be able to connect to your database, e.g. when sending queries with Prisma Client or when changing the database schema with Prisma Migrate.
连接 URL 通过 Prisma 架构中 datasource
块的 url
字段提供。它通常由以下组件组成(SQLite 和 Prisma Postgres 除外):
¥The connection URL is provided via the url
field of a datasource
block in your Prisma schema. It usually consists of the following components (except for SQLite and Prisma Postgres):
-
用户:你的数据库用户的名称
¥User: The name of your database user
-
密码:你的数据库用户的密码
¥Password: The password for your database user
-
主持人:运行数据库服务器的计算机的 IP 或域名
¥Host: The IP or domain name of the machine where your database server is running
-
港口:数据库服务器运行的端口
¥Port: The port on which your database server is running
-
数据库名称:你要使用的数据库的名称
¥Database name: The name of the database you want to use
确保你在开始使用 Prisma ORM 时手头有这些信息。如果你还没有运行数据库服务器,则可以使用本地 SQLite 数据库文件(请参阅 快速开始)或 使用 Prisma Postgres 设置免费的 PostgreSQL 数据库。
¥Make sure you have this information at hand when getting started with Prisma ORM. If you don't have a database server running yet, you can either use a local SQLite database file (see the Quickstart) or setup a free PostgreSQL database with Prisma Postgres.
格式
¥Format
连接 URL 的格式取决于你使用的数据库连接器。Prisma ORM 通常支持每个数据库的标准格式。你可以在专用文档页面上找到有关数据库连接 URL 的更多信息:
¥The format of the connection URL depends on the database connector you're using. Prisma ORM generally supports the standard formats for each database. You can find out more about the connection URL of your database on the dedicated docs page:
特殊字符
¥Special characters
对于 MySQL、PostgreSQL 和 CockroachDB,你必须在连接 URL 的任何部分添加 对特殊字符进行百分比编码 - 包括密码。例如,p@$$w0rd
变为 p%40%24%24w0rd
。
¥For MySQL, PostgreSQL and CockroachDB you must percentage-encode special characters in any part of your connection URL - including passwords. For example, p@$$w0rd
becomes p%40%24%24w0rd
.
对于 Microsoft SQL Server,你必须在连接字符串的任何部分中添加 转义特殊字符。
¥For Microsoft SQL Server, you must escape special characters in any part of your connection string.
示例
¥Examples
以下是 Prisma ORM 支持的数据库的连接 URL 示例:
¥Here are examples for the connection URLs of the databases Prisma ORM supports:
Prisma Postgres
Prisma Postgres 是在 unikernel 上运行的托管 PostgreSQL 服务。有几种方法可以连接到 Prisma Postgres:
¥Prisma Postgres is a managed PostgreSQL service running on unikernels. There are several ways to connect to Prisma Postgres:
-
通过直接 TCP 连接(允许你通过任何 ORM 或数据库工具进行连接)
¥via direct TCP connections (lets you connect via any ORM or database tool)
-
通过 Prisma 加速(仅支持 Prisma ORM)
¥via Prisma Accelerate (only supported with Prisma ORM)
-
locally
这些连接字符串的格式如下。
¥The connection string formats of these are covered below.
直接 TCP
¥Direct TCP
通过直接 TCP 连接到 Prisma Postgres 时,你的连接字符串如下所示:
¥When you connect to Prisma Postgres via direct TCP, your connection string looks as follows:
DATABASE_URL="postgres://USER:PASSWORD@postgres.prisma-data.net:5432/?sslmode=require"
当你在 中为 Prisma Postgres 实例生成凭据时,会提供 USER
和 PASSWORD
值。以下是包含示例值的示例:
¥The USER
and PASSWORD
values are provided when you generate credentials for your Prisma Postgres instance in the . Here is an example with sample values:
DATABASE_URL="postgres://2f9881cc7eef46f094ac913df34c1fb441502fe66cbe28cc48998d4e6b20336b:sk_QZ3u8fMPFfBzOID4ol-mV@postgres.prisma-data.net:5432/?sslmode=require"
通过 Prisma Accelerate (HTTP)
¥Via Prisma Accelerate (HTTP)
通过 Prisma Accelerate 连接时,连接字符串不需要像传统连接字符串那样输入用户名/密码。相反,身份验证通过 API 密钥进行:
¥When connecting via Prisma Accelerate, the connection string doesn't require a user/password like a conventional connection string does. Instead, authentication works via an API key:
datasource db {
provider = "postgresql"
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
}
在此代码片段中,API_KEY
是你通过 设置新的 Prismas Postgres 实例时收到的 API 密钥的占位符。以下是 Prisma Postgres 的实际连接 URL 示例:
¥In this snippet, API_KEY
is a placeholder for the API key you are receiving when setting up a new Prismas Postgres instance via the . Here is an example for what a real connection URL to Prisma Postgres may look like:
datasource db {
provider = "postgresql"
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiMGNkZTFlMjQtNzhiYi00NTY4LTkyM2EtNWUwOTEzZWUyNjU1IiwidGVuYW50X2lkIjoiNzEyZWRlZTc1Y2U2MDk2ZjI4NDg3YjE4NWMyYzA2OTNhNGMxNzJkMjhhOWFlNGUwZTYxNWE4NWIxZWY1YjBkMCIsImludGVybmFsX3NlY3JldCI6IjA4MzQ2Y2RlLWI5ZjktNDQ4Yy04NThmLTMxNjg4ODEzNmEzZCJ9.N1Za6q6NfInzHvRkud6Ojt_-RFg18a0601vdYWGKOrk"
}
本地 Prisma Postgres
¥Local Prisma Postgres
用于连接到 本地 Prisma Postgres 实例的连接字符串与通过 Accelerate 连接到远程实例的结构相同:
¥The connection string for connecting to a local Prisma Postgres instance mirrors the structure of a remote instance via Accelerate:
datasource db {
provider = "postgresql"
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
}
但是,在这种情况下,API_KEY
不提供身份验证详细信息。相反,它会对本地 Prisma Postgres 实例的信息进行编码。你可以通过 prisma dev
命令获取本地连接字符串。
¥However, in this case the API_KEY
doesn't provide authentication details. Instead, it encodes information about the local Prisma Postgres instance. You can obtain a local connection string via the prisma dev
command.
PostgreSQL
datasource db {
provider = "postgresql"
url = "postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample"
}
MySQL
datasource db {
provider = "mysql"
url = "mysql://janedoe:mypassword@localhost:3306/mydb"
}
微软 SQL 服务器
¥Microsoft SQL Server
datasource db {
provider = "sqlserver"
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
}
SQLite
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
CockroachDB
datasource db {
provider = "cockroachdb"
url = "postgresql://janedoe:mypassword@localhost:26257/mydb?schema=public"
}
MongoDB
datasource db {
provider = "mongodb"
url = "mongodb+srv://root:<password>@cluster0.ab1cd.mongodb.net/myDatabase?retryWrites=true&w=majority"
}
.env
你还可以提供连接 URL 作为环境变量:
¥You can also provide the connection URL as an environment variable:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
然后,你可以在终端中设置环境变量,或者提供名为 .env
.txt 的 dotenv 文件。Prisma CLI 将自动获取该信息。
¥You can then either set the environment variable in your terminal or by providing a dotenv file named .env
. This will automatically be picked up by the Prisma CLI.
Prisma ORM 在以下情况下从 dotenv 文件读取连接 URL:
¥Prisma ORM reads the connection URL from the dotenv file in the following situations:
-
当它在构建期间更新架构时
¥When it updates the schema during build time
-
当它在运行时连接到数据库时
¥When it connects to the database during run time
DATABASE_URL=postgresql://janedoe:mypassword@localhost:5432/mydb