Skip to content

Commit b8fb616

Browse files
committed
update examples for mysql
1 parent 3d36513 commit b8fb616

31 files changed

+673
-181
lines changed

src/content/docs/hyperdrive/configuration/connect-to-private-database.mdx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ To test your Hyperdrive configuration to the database using Cloudflare Tunnel an
190190

191191
<Render file="create-hyperdrive-binding" product="hyperdrive" />
192192

193-
### Query your database using Postgres.js
193+
### Query your database
194+
195+
Validate that you can connect to your database from Workers and make queries.
196+
197+
<Tabs>
198+
<TabItem label="PostgreSQL">
194199

195200
Use Postgres.js to send a test query to validate that the connection has been successful.
196201

@@ -204,6 +209,25 @@ npx wrangler deploy
204209

205210
If you successfully receive the list of `pg_tables` from your database when you access your deployed Worker, your Hyperdrive has now been configured to securely connect to a private database using [Cloudflare Tunnel](/cloudflare-one/connections/connect-networks/) and [Cloudflare Access](/cloudflare-one/policies/access/).
206211

212+
</TabItem>
213+
<TabItem label="MySQL">
214+
215+
Use [mysql2](https://github.com/sidorares/node-mysql2) to send a test query to validate that the connection has been successful.
216+
217+
<Render file="use-mysql2-to-make-query" product="hyperdrive" />
218+
219+
Now, deploy your Worker:
220+
221+
```bash
222+
npx wrangler deploy
223+
```
224+
225+
If you successfully receive the list of tables from your database when you access your deployed Worker, your Hyperdrive has now been configured to securely connect to a private database using [Cloudflare Tunnel](/cloudflare-one/connections/connect-networks/) and [Cloudflare Access](/cloudflare-one/policies/access/).
226+
227+
228+
</TabItem>
229+
</Tabs>
230+
207231
## Troubleshooting
208232

209233
If you encounter issues when setting up your Hyperdrive configuration with tunnels to a private database, consider these common solutions, in addition to [general troubleshooting steps](/hyperdrive/observability/troubleshooting/) for Hyperdrive:

src/content/docs/hyperdrive/configuration/rotate-credentials.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You can change the connection information and credentials of your Hyperdrive con
1616

1717
Creating a new Hyperdrive configuration to update your database credentials allows you to keep your existing Hyperdrive configuration unchanged, gradually migrate your Worker to the new Hyperdrive configuration, and easily roll back to the previous configuration if needed.
1818

19-
To create a Hyperdrive configuration that connects to an existing PostgreSQL database, use the [Wrangler](/workers/wrangler/install-and-update/) CLI or the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/hyperdrive).
19+
To create a Hyperdrive configuration that connects to an existing PostgreSQL or MySQL database, use the [Wrangler](/workers/wrangler/install-and-update/) CLI or the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/hyperdrive).
2020

2121
```sh
2222
# wrangler v3.11 and above required

src/content/docs/hyperdrive/examples/aws-rds-aurora.mdx renamed to src/content/docs/hyperdrive/examples/connect-to-mysql/aws-rds-aurora.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
type: example
33
summary: Connect Hyperdrive to an AWS RDS or Aurora database instance.
44
pcx_content_type: example
5-
title: Connect to AWS RDS and Aurora
5+
title: AWS RDS and Aurora
66
sidebar:
77
order: 14
88
description: Connect Hyperdrive to an AWS RDS or Aurora database instance.
@@ -23,7 +23,7 @@ To allow Hyperdrive to connect to your database, you will need to ensure that Hy
2323
When creating or modifying an instance in the AWS console:
2424

2525
1. Configure a **DB cluster identifier** and other settings you wish to customize.
26-
2. Under **Settings** > **Credential settings**, note down the **Master username** and **Master password**.
26+
2. Under **Settings** > **Credential settings**, note down the **Master username** and **Master password** (Aurora only).
2727
3. Under the **Connectivity** header, ensure **Public access** is set to **Yes**.
2828
4. Select an **Existing VPC security group** that allows public Internet access from `0.0.0.0/0` to the port your database instance is configured to listen on (default: `5432` for PostgreSQL instances).
2929
5. Select **Create database**.
@@ -90,4 +90,4 @@ With a database user, password, database endpoint (hostname and port) and databa
9090

9191
## 3. Create a database configuration
9292

93-
<Render file="create-hyperdrive-config" />
93+
<Render file="create-hyperdrive-config-mysql" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
type: example
3+
summary: Connect Hyperdrive to an Azure Database for PostgreSQL instance.
4+
pcx_content_type: example
5+
title: Azure Database
6+
sidebar:
7+
order: 12
8+
description: Connect Hyperdrive to a Azure Database for PostgreSQL instance.
9+
---
10+
11+
import { Render } from "~/components";
12+
13+
This example shows you how to connect Hyperdrive to an Azure Database for PostgreSQL instance.
14+
15+
## 1. Allow Hyperdrive access
16+
17+
To allow Hyperdrive to connect to your database, you will need to ensure that Hyperdrive has valid credentials and network access.
18+
19+
<Render file="public-connectivity" />
20+
21+
### Azure Portal
22+
23+
#### Public access networking
24+
25+
To connect to your Azure Database for MySQL instance using public Internet connectivity:
26+
27+
1. In the [Azure Portal](https://portal.azure.com/), select the instance you want Hyperdrive to connect to.
28+
2. Expand **Settings** > **Networking** > ensure **Public access** is enabled > in **Firewall rules** add `0.0.0.0` as **Start IP address** and `255.255.255.255` as **End IP address**.
29+
3. Select **Save** to persist your changes.
30+
4. Select **Overview** from the sidebar and note down the **Server name** of your instance.
31+
32+
With the username, password, server name, and database name (default: `mysql`), you can now create a Hyperdrive database configuration.
33+
34+
#### Private access networking
35+
36+
To connect to a private Azure Database for MySQL instance, refer to [Connect to a private database using Tunnel](/hyperdrive/configuration/connect-to-private-database/).
37+
38+
## 2. Create a database configuration
39+
40+
<Render file="create-hyperdrive-config-mysql" />
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
type: example
3+
summary: Connect Hyperdrive to a Google Cloud SQL database instance.
4+
pcx_content_type: example
5+
title: Google Cloud SQL
6+
sidebar:
7+
order: 12
8+
description: Connect Hyperdrive to a Google Cloud SQL database instance.
9+
---
10+
11+
import { Render } from "~/components";
12+
13+
This example shows you how to connect Hyperdrive to a Google Cloud SQL MySQL database instance.
14+
15+
## 1. Allow Hyperdrive access
16+
17+
To allow Hyperdrive to connect to your database, you will need to ensure that Hyperdrive has valid user credentials and network access.
18+
19+
<Render file="public-connectivity" />
20+
21+
### Cloud Console
22+
23+
When creating the instance or when editing an existing instance in the [Google Cloud Console](https://console.cloud.google.com/sql/instances):
24+
25+
To allow Hyperdrive to reach your instance:
26+
27+
1. In the [Cloud Console](https://console.cloud.google.com/sql/instances), select the instance you want Hyperdrive to connect to.
28+
2. Expand **Connections** > **Networking** > ensure **Public IP** is enabled > **Add a Network** and input `0.0.0.0/0`.
29+
3. Select **Done** > **Save** to persist your changes.
30+
4. Select **Overview** from the sidebar and note down the **Public IP address** of your instance.
31+
32+
To create a user for Hyperdrive to connect as:
33+
34+
1. Select **Users** in the sidebar.
35+
2. Select **Add User Account** > select **Built-in authentication**.
36+
3. Provide a name (for example, `hyperdrive-user`) > select **Generate** to generate a password.
37+
4. Copy this password to your clipboard before selecting **Add** to create the user.
38+
39+
With the username, password, public IP address and (optional) database name (default: `mysql`), you can now create a Hyperdrive database configuration.
40+
41+
## 2. Create a database configuration
42+
43+
<Render file="create-hyperdrive-config-mysql" />
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
type: overview
3+
pcx_content_type: content
4+
title: Connect to MySQL
5+
hideChildren: false
6+
sidebar:
7+
order: 2
8+
group:
9+
hideIndex: false
10+
---
11+
12+
import { TabItem, Tabs, Render, WranglerConfig } from "~/components";
13+
14+
Hyperdrive supports MySQL and MySQL-compatible databases, [popular drivers](#supported-drivers) and Object Relational Mapper (ORM) libraries that use those drivers.
15+
16+
## Create a Hyperdrive
17+
18+
:::note
19+
20+
New to Hyperdrive? Refer to the [Get started guide](/hyperdrive/get-started/) to learn how to set up your first Hyperdrive.
21+
22+
:::
23+
24+
To create a Hyperdrive that connects to an existing MySQL database, use the [wrangler](/workers/wrangler/install-and-update/) CLI or the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/hyperdrive).
25+
26+
When using wrangler, replace the placeholder value provided to `--connection-string` with the connection string for your database:
27+
28+
```sh
29+
# wrangler v3.11 and above required
30+
npx wrangler hyperdrive create my-first-hyperdrive --connection-string="mysql://user:[email protected]:3306/databasenamehere"
31+
```
32+
33+
The command above will output the ID of your Hyperdrive, which you will need to set in the [Wrangler configuration file](/workers/wrangler/configuration/) for your Workers project:
34+
35+
<WranglerConfig>
36+
37+
```toml
38+
# required for database drivers to function
39+
compatibility_flags = ["nodejs_compat"]
40+
compatibility_date = "2024-09-23"
41+
42+
[[hyperdrive]]
43+
binding = "HYPERDRIVE"
44+
id = "<your-hyperdrive-id-here>"
45+
```
46+
47+
</WranglerConfig>
48+
49+
This will allow Hyperdrive to generate a dynamic connection string within your Worker that you can pass to your existing database driver. Refer to [Driver examples](#driver-examples) to learn how to set up a database driver with Hyperdrive.
50+
51+
Refer to the [Examples documentation](/hyperdrive/examples/) for step-by-step guides on how to set up Hyperdrive with several popular database providers.
52+
53+
## Supported drivers
54+
55+
Hyperdrive uses Workers [TCP socket support](/workers/runtime-apis/tcp-sockets/#connect) to support TCP connections to databases. The following table lists the supported database drivers and the minimum version that works with Hyperdrive:
56+
57+
| Driver | Documentation | Minimum Version Required | Notes |
58+
| ------------------------ | ---------------------------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
59+
| mysql2 (**recommended**) | [mysql2 documentation](https://github.com/sidorares/node-mysql2) | `[email protected]` | Supported in both Workers & Pages. Using the Promise API is recommended. |
60+
| mysql | [mysql documentation](https://github.com/mysqljs/mysql) | `[email protected]` | Requires `compatibility_flags = ["nodejs_compat"]` and `compatibility_date = "2024-09-23"` - refer to [Node.js compatibility](/workers/runtime-apis/nodejs). Requires wrangler `3.78.7` or later. |
61+
| Drizzle | [Drizzle documentation](https://orm.drizzle.team/) | Requires `[email protected]` | |
62+
| Kysely | [Kysely documentation](https://kysely.dev/) | Requires `[email protected]` | |
63+
| Prisma | [Prisma documentation](https://www.prisma.io/) | Requires `[email protected]` | |
64+
| TypeORM | [Kysely documentation](https://typeorm.io/) | Requires `[email protected]` | |
65+
66+
^ _The marked libraries can use either mysql or mysql2 as a dependency._
67+
68+
Other drivers and ORMs not listed may also be supported: this list is not exhaustive.
69+
70+
### Database drivers and Node.js compatibility
71+
72+
[Node.js compatibility](/workers/runtime-apis/nodejs/) is required for database drivers, including mysql and mysql2, and needs to be configured for your Workers project.
73+
74+
<Render file="nodejs_compat" product="workers" />
75+
76+
## Supported TLS (SSL) modes
77+
78+
Hyperdrive supports the following MySQL TLS/SSL connection modes when connecting to your origin database:
79+
80+
| Mode | Supported | Details |
81+
| ----------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
82+
| `DISABLED` | No | Hyperdrive does not support insecure plain text connections. |
83+
| `PREFERRED` | No (use `required`) | Hyperdrive will always use TLS. |
84+
| `REQUIRED` | Yes (default) | TLS is required, and server certificates are validated (based on WebPKI). |
85+
| `VERIFY_CA` | Not currently supported in beta | Verifies the server's TLS certificate is signed by a root CA on the client. |
86+
| `VERIFY_IDENTITY` | Not currently supported in beta | Identical to `VERIFY_CA`, but also requires that the database hostname matches the certificate's Common Name (CN). |
87+
88+
:::note
89+
90+
Hyperdrive does not currently support `VERIFY_CA` or `VERIFY_IDENTITY` for MySQL (beta).
91+
92+
:::
93+
94+
## Driver examples
95+
96+
The following examples show you how to:
97+
98+
1. Create a database client with a database driver.
99+
2. Pass the Hyperdrive connection string and connect to the database.
100+
3. Query your database via Hyperdrive.
101+
102+
### `mysql2`
103+
104+
The following Workers code shows you how to use [mysql2](https://github.com/sidorares/node-mysql2) with Hyperdrive using the Promise API.
105+
106+
<Render file="use-mysql2-to-make-query" product="hyperdrive" />
107+
108+
### `mysql`
109+
110+
Install the `mysql` driver:
111+
112+
```sh
113+
npm install mysql
114+
```
115+
116+
**Ensure you have `compatibility_flags` and `compatibility_date` set in your [Wrangler configuration file](/workers/wrangler/configuration/)** as shown below:
117+
118+
<Render file="nodejs-compat-wrangler-toml" product="workers" />
119+
120+
Create a new connection and pass the Hyperdrive parameters:
121+
122+
```ts
123+
import { createConnection } from "mysql";
124+
125+
export interface Env {
126+
HYPERDRIVE: Hyperdrive;
127+
}
128+
129+
export default {
130+
async fetch(request, env, ctx): Promise<Response> {
131+
const result = await new Promise<any>((resolve) => {
132+
const connection = createConnection({
133+
host: env.HYPERDRIVE.host,
134+
user: env.HYPERDRIVE.user,
135+
password: env.HYPERDRIVE.password,
136+
database: env.HYPERDRIVE.database,
137+
port: env.HYPERDRIVE.port,
138+
});
139+
140+
connection.connect((error: { message: string }) => {
141+
if (error) {
142+
throw new Error(error.message);
143+
}
144+
145+
connection.query("SHOW tables;", [], (error, rows, fields) => {
146+
connection.end();
147+
148+
resolve({ fields, rows });
149+
});
150+
});
151+
});
152+
153+
return new Response(JSON.stringify(result), {
154+
headers: {
155+
"Content-Type": "application/json",
156+
},
157+
});
158+
},
159+
} satisfies ExportedHandler<Env>;
160+
```
161+
162+
## Next steps
163+
164+
- Refer to the list of [supported database integrations](/workers/databases/connecting-to-databases/) to understand other ways to connect to existing databases.
165+
- Learn more about how to use the [Socket API](/workers/runtime-apis/tcp-sockets) in a Worker.
166+
- Understand the [protocols supported by Workers](/workers/reference/protocols/).
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
type: example
3+
summary: Connect Hyperdrive to a Planetscale Postgres database.
4+
pcx_content_type: example
5+
title: Planetscale
6+
sidebar:
7+
order: 4
8+
description: Connect Hyperdrive to a Planetscale Postgres database.
9+
---
10+
11+
import { Render } from "~/components";
12+
13+
This example shows you how to connect Hyperdrive to a [Planetscale](https://Planetscale.tech/) Postgres database.
14+
15+
## 1. Allow Hyperdrive access
16+
17+
You can connect Hyperdrive to any existing Planetscale database by creating a new user and fetching your database connection string.
18+
19+
### Planetscale Dashboard
20+
21+
1. Go to the [**Planetscale dashboard**](https://app.planetscale.com/) and select the database you wish to connect to.
22+
2. Click **Connect**. Enter `hyperdrive-user` as the password name (or your preferred name) and configure the permissions as desired. Select **Create password**. Note the username and password as they will not be displayed again.
23+
3. Select **Other** as your language or framework. Note down the database host, database name, database username, and password. You will need these to create a database configuration in Hyperdrive.
24+
25+
With the host, database name, username and password, you can now create a Hyperdrive database configuration.
26+
27+
## 2. Create a database configuration
28+
29+
<Render file="create-hyperdrive-config-mysql" />

0 commit comments

Comments
 (0)