从现有 MySQL 数据库导入数据
本指南提供了将数据从现有 MySQL 数据库导入 Prisma Postgres 的分步说明。
¥This guide provides step-by-step instructions for importing data from an existing MySQL database into Prisma Postgres.
你可以通过四个步骤完成此迁移:
¥You can accomplish this migration in four steps:
-
创建一个新的 Prisma Postgres 数据库。
¥Create a new Prisma Postgres database.
-
使用 直接连接 直接连接到 Prisma Postgres 实例。
¥Connect directly to a Prisma Postgres instance using a direct connection.
-
使用 pgloader 将你的 MySQL 数据迁移到 Prisma Postgres。
¥Migrate your MySQL data to Prisma Postgres using pgloader.
-
为 Prisma Postgres 配置你的 Prisma 项目。
¥Configure your Prisma project for Prisma Postgres.
先决条件
¥Prerequisites
-
连接到现有 MySQL 数据库的 URL。
¥The connection URL to your existing MySQL database.
-
账户。
¥A account.
-
已安装 Node.js 18+。
¥Node.js 18+ installed.
-
已安装 pgloader。
¥pgloader installed.
我们建议在单独的 git 开发分支中尝试此迁移。
¥We recommend attempting this migration in a separate git development branch.
1. 创建新的 Prisma Postgres 数据库
¥ Create a new Prisma Postgres database
按照以下步骤创建新的 Prisma Postgres 数据库:
¥Follow these steps to create a new Prisma Postgres database:
-
登录 并打开控制台。
¥Log in to and open the Console.
-
在你选择的 workspace 中,单击新建项目按钮。
¥In a workspace of your choice, click the New project button.
-
在名称字段中为你的项目输入一个名称,例如 hello-ppg。
¥Type a name for your project in the Name field, e.g. hello-ppg.
-
在 Prisma Postgres 部分中,单击“开始”按钮。
¥In the Prisma Postgres section, click the Get started button.
-
在“区域”下拉菜单中,选择最接近你当前位置的区域,例如美国东部(弗吉尼亚北部)。
¥In the Region dropdown, select the region that's closest to your current location, e.g. US East (N. Virginia).
-
单击创建项目按钮。
¥Click the Create project button.
数据库配置完成后,找到 Prisma Postgres 的直接连接字符串:
¥Once your database was provisioned, find your direct Prisma Postgres connection string:
-
导航到活动的 Prisma Postgres 实例。
¥Navigate to your active Prisma Postgres instance.
-
点击项目侧边栏中的“API 密钥”选项卡。
¥Click the API Keys tab in the project's sidenav.
-
点击“创建 API 密钥”按钮。
¥Click the Create API key button.
-
在弹出窗口中,输入 API 密钥的名称,然后单击“创建”。
¥In the popup, provide a Name for the API key and click Create.
-
复制以
postgres://
开头的连接字符串,这是你的直接连接字符串。¥Copy the connection string starting with
postgres://
, this is your direct connection string.
保存连接字符串,下一步你将需要它。
¥Save the connection string, you'll need it in the next step.
2. 直接连接到 Prisma Postgres 实例
¥ Connect directly to a Prisma Postgres instance
在此步骤中,你将使用 直接连接 连接到你的 Prisma Postgres 实例。
¥In this step, you'll use a direct connection to connect to your Prisma Postgres instance.
你需要来自 步骤 1 的 Prisma Postgres 连接 URL:
¥You'll need the Prisma Postgres connection URL from step 1:
prisma+postgres://accelerate.prisma-data.net/?api_key=ey...
3. 使用 pgloader 将 MySQL 数据迁移到 Prisma Postgres
¥ Migrate your MySQL data to Prisma Postgres using pgloader
现在你已与 Prisma Postgres 实例建立活动连接,你将使用 pgloader 将数据从 MySQL 数据库导出到 Prisma Postgres。
¥Now that you have an active connection to your Prisma Postgres instance, you'll use pgloader to export data from your MySQL database to Prisma Postgres.
打开一个单独的终端窗口并创建一个 config.load
文件:
¥Open a separate terminal window and create a config.load
file:
touch config.load
在你喜欢的文本编辑器中打开 config.load
文件并复制粘贴以下配置:
¥Open the config.load
file in your preferred text editor and copy-paste the following configuration:
LOAD DATABASE
FROM mysql://username:password@host:PORT/database_name
INTO postgres://__USER__:__PASSWORD__@postgres.prisma-data.net:5432/?sslmode=require
WITH quote identifiers, -- preserve table/column name case by quoting them
include drop,
create tables,
create indexes,
reset sequences
ALTER SCHEMA 'database_name' RENAME TO 'public';
确保在 config.load
文件中更新以下详细信息:
¥Make sure to update the following details in the config.load
file:
-
FROM
url(MySQL 数据库 URL):¥
FROM
url (MySQL database URL):-
将
username
、password
、host
、PORT
和database_name
替换为你的 MySQL 数据库的实际连接详细信息。¥Replace
username
,password
,host
,PORT
, anddatabase_name
with the actual connection details for your MySQL database. -
如果需要 SSL,请确保你的连接字符串包含
useSSL=true
,例如:mysql://username:password@host:PORT/database_name?useSSL=true
。请注意,使用 PlanetScale 时,附加sslaccept=strict
将不起作用。¥Ensure that your connection string includes
useSSL=true
if SSL is required, for example:mysql://username:password@host:PORT/database_name?useSSL=true
. Note that when using PlanetScale, appendingsslaccept=strict
will not work.
-
-
INTO
url(Postgres 数据库 URL):¥
INTO
url (Postgres database URL):-
使用上面的直接连接字符串更新此文件,替换
__USER__
和__PASSWORD__
占位符。¥Update this with your direct connection string from above, replacing the
__USER__
and__PASSWORD__
placeholders.
-
-
更新
ALTER SCHEMA 'database_name' RENAME TO 'public';
中的database_name
以完全匹配 MySQL 连接字符串中的database_name
。¥Update the
database_name
inALTER SCHEMA 'database_name' RENAME TO 'public';
to exactly match thedatabase_name
in your MySQL connection string.
使用更新后的凭据保存配置文件后,在同一个终端窗口中执行以下命令:
¥After saving the configuration file with your updated credentials, in the same terminal window, execute the following command:
pgloader config.load
你应该看到类似于此的日志,它确认你的数据已成功迁移:
¥You should see a log similar to this, which confirms the successful migration of your data:
LOG report summary reset
table name errors rows bytes total time
------------------------- --------- --------- --------- --------------
fetch meta data 0 9 2.546s
Create Schemas 0 0 0.325s
Create SQL Types 0 0 0.635s
Create tables 0 6 5.695s
Set Table OIDs 0 3 0.328s
------------------------- --------- --------- --------- --------------
public.post 0 8 0.5 kB 4.255s
public."user" 0 4 0.1 kB 2.775s
public._prisma_migrations 0 1 0.2 kB 4.278s
------------------------- --------- --------- --------- --------------
COPY Threads Completion 0 4 5.095s
Index Build Completion 0 5 9.601s
Create Indexes 0 5 4.116s
Reset Sequences 0 2 4.540s
Primary Keys 0 3 2.917s
Create Foreign Keys 0 1 1.121s
Create Triggers 0 0 0.651s
Install Comments 0 0 0.000s
------------------------- --------- --------- --------- --------------
Total import time ✓ 13 0.8 kB 28.042s
如果你看到这样的输出,则表示你的数据已成功导出到 Prisma Postgres 实例。
¥If you see output like this, it means your data has been successfully exported to your Prisma Postgres instance.
你还可以使用 Prisma 工作室 并验证迁移是否成功:
¥You also can use Prisma Studio and verify whether the migration was successful:
npx prisma studio
4. 为 Prisma Postgres 配置你的 Prisma 项目
¥ Configure your Prisma project for Prisma Postgres
迁移数据后,你需要设置 Prisma 项目以与 Prisma Postgres 配合使用。步骤因你是否已使用 Prisma ORM 而异。
¥After migrating your data, you need to set up your Prisma project to work with Prisma Postgres. The steps differ depending on whether you were already using Prisma ORM.
如果你以前没有使用 Prisma ORM
¥If you were not previously using Prisma ORM
通过在项目目录中运行 npx prisma init
来初始化项目中的 Prisma。这将创建一个 prisma
文件夹,其中包含 schema.prisma
文件和 .env
文件(如果尚不存在)。
¥Initialize Prisma in your project by running npx prisma init
in your project directory. This creates a prisma
folder with a schema.prisma
file and .env
file (if not already present).
在生成的 .env
文件中,更新 DATABASE_URL
以匹配你在 步骤 1 中收到的 Prisma Postgres 连接字符串:
¥In the generated .env
file, update DATABASE_URL
to match your Prisma Postgres connection string that you received in step 1:
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=__API_KEY__"
通过运行以下命令 内省 你新迁移的数据库:
¥Introspect your newly migrated database by running:
npx prisma db pull
此命令使用代表迁移表的模型更新你的 schema.prisma
文件,因此你可以开始使用 Prisma 客户端 查询数据或使用 Prisma 迁移 管理未来的更改。
¥This command updates your schema.prisma
file with models representing your migrated tables, so you can start using Prisma Client to query your data or Prisma Migrate to manage future changes.
恭喜!你已成功将 MySQL 数据库迁移到 Prisma Postgres 并配置了 Prisma 项目。你的迁移教程现已完成。
¥Congratulations! You've successfully migrated your MySQL database to Prisma Postgres and configured your Prisma project. Your migration tutorial is now complete.
有关开始使用 Prisma 和 Prisma Postgres 的综合指南,请参阅 从头开始使用 Prisma 和 Prisma Postgres。
¥For a comprehensive guide on getting started with Prisma and Prisma Postgres, see start from scratch with Prisma and Prisma Postgres.
如果你已经在使用 Prisma ORM
¥If you were already using Prisma ORM
在你的 schema.prisma
文件中,将 datasource
块中的 provider
从 mysql
更改为 postgresql
:
¥In your schema.prisma
file, change the provider
in the datasource
block from mysql
to postgresql
:
datasource db {
provider = "mysql"
provider = "postgres"
url = env("DATABASE_URL")
}
在生成的 .env
文件中,更新 DATABASE_URL
以匹配你在 步骤 1 中收到的新 Prisma Postgres 连接字符串:
¥In the generated .env
file, update DATABASE_URL
to match your new Prisma Postgres connection string that you received in step 1:
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=__API_KEY__"
检查你新迁移的 Prisma Postgres 数据库并生成 Prisma 客户端:
¥Introspect your newly migrated Prisma Postgres database and generate Prisma Client:
npx prisma db pull
此命令根据新的数据库模式刷新你的 Prisma 模型。
¥This command refreshes your Prisma models based on the new database schema.
如果你之前使用过 Prisma 迁移:
¥If you were using Prisma Migrate before:
-
删除
prisma
目录中现有的migrations
文件夹。¥Delete your existing
migrations
folder in theprisma
directory. -
为数据库设定基线 开始创建新的迁移。
¥Baseline your database to begin creating new migrations.
恭喜!你已成功将 MySQL 数据库迁移到 Prisma Postgres 并配置了 Prisma 项目。你的迁移教程现已完成。
¥Congratulations! You've successfully migrated your MySQL database to Prisma Postgres and configured your Prisma project. Your migration tutorial is now complete.
如果你在迁移过程中遇到任何问题,请随时通过 Discord 或 X 与我们联系。
¥If you encounter any issues during the migration, please don't hesitate to reach out to us on Discord or via X.