Prisma 中文文档 下载 App

数据库连接失败:P1001、P1002、SSL 问题排查

Prisma 连接错误码速查

错误码含义排查方向
P1001Can't reach database server网络/DNS/防火墙
P1002Database server connection timeout连接池满/服务器负载高
P1003Database file not found(SQLite)文件路径不对
P1008Operations timed out操作本身太慢,加 timeout
P1017Server closed the connection数据库重启/连接数超限
P1020SSL 连接需要Serverless 平台常见,加 sslmode

排查步骤(从简到难)

第一步:直接用数据库客户端连

# PostgreSQL
psql "postgresql://user:pass@host:5432/dbname"

# MySQL
mysql -h host -u user -p dbname

# SQLite
sqlite3 prisma/dev.db

如果数据库客户端也连不上,那问题在数据库本身或网络,不是 Prisma。

第二步:检查 DNS

# 看数据库主机名能不能解析
nslookup your-db-host.example.com
# 或
ping your-db-host.example.com

Vercel / Railway 等平台上的 Neon、Supabase 托管数据库偶尔会有 DNS 问题。

第三步:检查防火墙

第四步:加 SSL

Serverless 平台上的云数据库几乎都强制 SSL 连接。在连接串里加:

# PostgreSQL
postgresql://user:pass@host:5432/dbname?schema=public&sslmode=require

# MySQL
mysql://user:pass@host:3306/dbname?sslmode=required

如果 Prisma 报 P1020,大概率是缺 SSL。

第五步:检查 Direct URL

如果你用了 Accelerate 或其他连接池代理,确保:

没有 DIRECT_URL 时 CLI 命令会尝试走连接池,可能报连接失败或锁表超时。

第六步:连接池耗尽

如果报的是 P1002(超时)或 P1017(服务器主动断连),很可能是数据库连接数满了。

-- PostgreSQL 查当前连接数
SELECT count(*) FROM pg_stat_activity;

-- 查连接上限
SHOW max_connections;

解决:

第七步:Neon / Supabase 特定

Neon 有特殊注意:

# Neon Pooler 连接(应用运行)
postgresql://user:pass@ep-xxx-pooler.us-east-2.aws.neon.tech/neondb

# Neon 直连(migrate / CLI 用)
postgresql://user:pass@ep-xxx.us-east-2.aws.neon.tech/neondb

Supabase:

数据库 URL 格式错误

常见手误:

错误正确
postgresql://localhost/mydb?schema=publicpostgresql://postgres:password@localhost:5432/mydb?schema=public(漏了密码和端口)
mysql://root@localhost:3306/mydbmysql://root:password@localhost:3306/mydb(漏了密码)
SQLite 写成 sqlite:///dev.dbPrisma 用 file:./dev.db

Prisma CLI 报错时会打印你配置的 DATABASE_URL 的一部分(会隐藏密码),检查一下是不是格式有问题。

prisma doctor

Prisma 自带一个诊断命令:

npx prisma doctor

它会检查:

这个命令输出的信息比普通报错更详细,遇到连接问题先跑一下。