Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

PlanetScale

Hyperdrive を PlanetScale の MySQL データベースに接続します。

最終更新 Markdown で表示Agent セットアップ

この例では、Hyperdrive を PlanetScale の MySQL データベースに接続する方法を示します。

1. Hyperdrive へのアクセスを許可する

新しいユーザーを作成して接続文字列を取得すれば、既存の PlanetScale MySQL 互換データベースに Hyperdrive を接続できます。

PlanetScale Dashboard

  1. PlanetScale dashboard を開き、接続するデータベースを選びます。
  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 設定の名前に置き換え、データベースホストから提供された接続文字列を貼り付けるか、
  • userpasswordHOSTNAME_OR_IP_ADDRESSportdatabase_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-09-20",
	"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-09-20"
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 バインディングを追加します。

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

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

Hyperdrive のパラメーターを渡して、新しい connection インスタンスを作成します。

// 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>;

次のステップ

役に立ちましたか?