mysql2

Last updated Apr 21, 2026 The mysql2 ↗︎ package is a modern MySQL driver for Node.js with better performance and built-in Promise support. This example demonstrates how to use it with Cloudflare Workers and Hyperdrive.

Install the mysql2 ↗︎ driver:

npmyarnpnpmbun

npm i mysql2@>3.13.0
yarn add mysql2@>3.13.0
pnpm add mysql2@>3.13.0
bun add mysql2@>3.13.0

Note

mysql2 v3.13.0 or later is required

Add the required Node.js compatibility flags and Hyperdrive binding to your wrangler.jsonc file:

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

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

Create a new connection instance and pass the Hyperdrive parameters:

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

Note

The minimum version of mysql2 required for Hyperdrive is 3.13.0.

Was this helpful?

YesNo

On this page

mysql2 - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql2/#page","headline":"mysql2","description":"Use the mysql2 driver with Hyperdrive to query MySQL databases from Cloudflare Workers.","url":"https://developers.cloudflare.com/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql2/","inLanguage":"en","image":"https://developers.cloudflare.com/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql2/og.png?v=12726197a0c2ada1","dateModified":"2026-04-21","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Use Hyperdrive to connect to PostgreSQL and PostgreSQL-compatible databases from Cloudflare Workers. title: Connect to PostgreSQL image: https://developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/og.png?v=b46b2c0c6274c318


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/hyperdrive/llms.txt
Use this file to discover all available pages before exploring further.