跳转到内容
搜索文档

PlanetScale

将 Hyperdrive 连接到 PlanetScale MySQL 数据库。

最后更新 查看 MarkdownAgent 设置

本示例展示如何将 Hyperdrive 连接到 PlanetScale ↗ MySQL 数据库。

1. 允许 Hyperdrive 访问

通过创建新用户并获取数据库连接字符串,可将 Hyperdrive 连接到任何现有 PlanetScale MySQL 兼容数据库。

PlanetScale 仪表板

  1. 前往 PlanetScale 仪表板 ↗ 并选择要连接的数据库。
  2. 点击 Connect(连接)。输入 hyperdrive-user 作为密码名称(或你偏好的名称)并按需配置权限。选择 Create password(创建密码)。记下用户名和密码,它们不会再次显示。
  3. 选择 Other(其他) 作为语言或框架。记下数据库主机、数据库名称、数据库用户名和密码。创建 Hyperdrive 数据库配置时需要这些信息。

有了主机、数据库名称、用户名和密码后,即可创建 Hyperdrive 数据库配置。

2. 创建数据库配置

配置 Hyperdrive 需要:

  • 数据库的 IP 地址(或主机名)和端口。
  • 上一步配置的数据库用户名(例如 hyperdrive-demo)。
  • 该用户名对应的密码。
  • 希望 Hyperdrive 连接的数据库名称,例如 mysql。

Hyperdrive 接受数据库驱动常用的连接字符串格式组合上述参数:

mysql://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name

大多数数据库提供商会提供可直接复制到 Hyperdrive 的连接字符串。

使用 Wrangler CLI 创建 Hyperdrive 配置时,打开终端并运行以下命令。

  • 将 <NAME_OF_HYPERDRIVE_CONFIG> 替换为 Hyperdrive 配置名称并粘贴数据库主机提供的连接字符串,或
  • 将 user、password、HOSTNAME_OR_IP_ADDRESS、port 和 database_name 占位符替换为你的数据库信息:
npx wrangler hyperdrive create <NAME_OF_HYPERDRIVE_CONFIG> --connection-string="mysql://user:password@HOSTNAME_OR_IP_ADDRESS:PORT/database_name"

该命令会输出 Wrangler 配置文件 的绑定:

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "hyperdrive-example",
	"main": "src/index.ts",
	// Set this to today's date
	"compatibility_date": "2026-08-17",
	"compatibility_flags": [
		"nodejs_compat"
	],
	// Pasted from the output of `wrangler hyperdrive create <NAME_OF_HYPERDRIVE_CONFIG> --connection-string=[...]` above.
	"hyperdrive": [
		{
			"binding": "HYPERDRIVE",
			"id": "<ID OF THE CREATED HYPERDRIVE CONFIGURATION>"
		}
	]
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "hyperdrive-example"
main = "src/index.ts"
# Set this to today's date
compatibility_date = "2026-08-17"
compatibility_flags = [ "nodejs_compat" ]

[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<ID OF THE CREATED HYPERDRIVE CONFIGURATION>"

3. 从 Worker 使用 Hyperdrive

安装 mysql2 ↗ 驱动:

npm i mysql2@>3.13.0

在 wrangler.jsonc 中添加所需的 Node.js 兼容性标志和 Hyperdrive 绑定:

在 wrangler.jsonc 中添加 Node.js 兼容性标志和 Hyperdrive 绑定(binding):

{
	// required for database drivers to function
	"compatibility_flags": [
		"nodejs_compat"
	],
	// Set this to today's date
	"compatibility_date": "2026-08-17",
	"hyperdrive": [
		{
			"binding": "HYPERDRIVE",
			"id": "<your-hyperdrive-id-here>"
		}
	]
}
compatibility_flags = [ "nodejs_compat" ]
# Set this to today's date
compatibility_date = "2026-08-17"

[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<your-hyperdrive-id-here>"

创建新的 connection 实例并传入 Hyperdrive 参数:

// mysql2 v3.13.0 or later is required
import { createConnection } from "mysql2/promise";

export default {
	async fetch(request, env, ctx): Promise<Response> {
		// Create a new connection on each request. Hyperdrive maintains the underlying
		// database connection pool, so creating a new connection is fast.
		const connection = await createConnection({
			host: env.HYPERDRIVE.host,
			user: env.HYPERDRIVE.user,
			password: env.HYPERDRIVE.password,
			database: env.HYPERDRIVE.database,
			port: env.HYPERDRIVE.port,

			// Required to enable mysql2 compatibility for Workers
			disableEval: true,
		});

		try {
			// Sample query
			const [results, fields] = await connection.query("SHOW tables;");

			// Return result rows as JSON
			return Response.json({ results, fields });
		} catch (e) {
			console.error(e);
			return Response.json(
				{ error: e instanceof Error ? e.message : e },
				{ status: 500 },
			);
		}
	},
} satisfies ExportedHandler<Env>;

后续步骤

这篇文档对您有帮助吗?