MySQL support in Hyperdrive is now generally available
Hyperdrive now provides general availability support for MySQL databases, allowing you to connect to any MySQL database from Cloudflare Workers with optimized connection pooling, caching, and no code changes required for existing drivers and ORMs.
Support for MySQL in Hyperdrive is now generally available. You can connect to any MySQL database from your Workers using Hyperdrive.
Hyperdrive makes your regional, MySQL databases fast when connecting from Cloudflare Workers. It eliminates unnecessary network roundtrips during connection setup, pools database connections globally, and can cache query results to provide the fastest possible response times.
You can connect using your existing drivers, ORMs, and query builders with Hyperdrive's secure credentials, with no code changes required. MySQL support is available at the same pricing as Postgres.
import { createConnection } from "mysql2/promise";
export default {
async fetch(request, env, ctx) {
const connection = await createConnection({
host: env.HYPERDRIVE.host,
user: env.HYPERDRIVE.user,
password: env.HYPERDRIVE.password,
database: env.HYPERDRIVE.database,
port: env.HYPERDRIVE.port,
disableEval: true, // Required for Workers compatibility
});
const [results, fields] = await connection.query("SHOW tables;");
ctx.waitUntil(connection.end());
return new Response(JSON.stringify({ results, fields }), {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
},
};import { createConnection } from "mysql2/promise";
export interface Env {
HYPERDRIVE: Hyperdrive;
}
export default {
async fetch(request, env, ctx): Promise<Response> {
const connection = await createConnection({
host: env.HYPERDRIVE.host,
user: env.HYPERDRIVE.user,
password: env.HYPERDRIVE.password,
database: env.HYPERDRIVE.database,
port: env.HYPERDRIVE.port,
disableEval: true, // Required for Workers compatibility
});
const [results, fields] = await connection.query("SHOW tables;");
ctx.waitUntil(connection.end());
return new Response(JSON.stringify({ results, fields }), {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
},
} satisfies ExportedHandler<Env>;
Learn more about how Hyperdrive works and get started building Workers that connect to MySQL with Hyperdrive.
Source: original entry ↗