|
| 1 | +# Using MySQL2 on Cloudflare Workers |
| 2 | + |
| 3 | +This document is a step-by-step tutorial on how to use `node-mysql2` on Cloudflare Workers. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before you try the steps in this document, you need to prepare the following things: |
| 8 | + |
| 9 | +- A [Cloudflare Workers](https://dash.cloudflare.com/login) account. |
| 10 | +- [Wrangler](https://developers.cloudflare.com/workers/wrangler/) installed. |
| 11 | + |
| 12 | +If you haven't created a Cloudflare Worker project yet, please refer to [Get Started Guide](https://developers.cloudflare.com/workers/get-started/guide/) to create one. |
| 13 | + |
| 14 | + |
| 15 | +## Step 1: Configure `wrangler.jsonc` file |
| 16 | + |
| 17 | +Because of the differences between the Cloudflare Workers runtime and the Node.js runtime, you can't use MySQL2 directly on the worker, but you can enable compatibility mode through configuration: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "$schema": "node_modules/wrangler/config-schema.json", |
| 22 | + "name": "mysql2-cloudflare", |
| 23 | + "main": "src/index.ts", |
| 24 | + // highlight-start |
| 25 | + "compatibility_date": "2025-02-24", |
| 26 | + // highlight-end |
| 27 | + "observability": { |
| 28 | + "enabled": true |
| 29 | + }, |
| 30 | + // highlight-start |
| 31 | + "compatibility_flags": [ |
| 32 | + "nodejs_compat" |
| 33 | + ] |
| 34 | + // highlight-end |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Step 2: Disable code generation-based parser |
| 39 | + |
| 40 | +MySQL2 uses code generation based parser for performance by default, but this is restricted on Cloudflare Workers. You may encounter the errors like: |
| 41 | + |
| 42 | +``` |
| 43 | +Code generation from strings disallowed for this context |
| 44 | +``` |
| 45 | + |
| 46 | +To make it work, you can disable the code generation-based parser by pass the `disableEval=true` parameter when creating a connection. Here are an example: |
| 47 | + |
| 48 | +```ts |
| 49 | +import { createConnection } from 'mysql2/promise'; |
| 50 | + |
| 51 | +export default { |
| 52 | + async fetch(request, env, ctx): Promise<Response> { |
| 53 | + const conn = await createConnection({ |
| 54 | + host: "<ENTER_HOST>", |
| 55 | + port: 3306, |
| 56 | + user: "<ENTER_USERNAME>", |
| 57 | + password: "<ENTER_PASSWORD>", |
| 58 | + // highlight-start |
| 59 | + disableEval: true, |
| 60 | + // highlight-end |
| 61 | + database: "<ENTER_DB_NAME>" |
| 62 | + }) |
| 63 | + |
| 64 | + const [results] = await conn.query("SHOW TABLES;"); |
| 65 | + |
| 66 | + return new Response(JSON.stringify(results)); |
| 67 | + }, |
| 68 | +} satisfies ExportedHandler<Env>; |
| 69 | +``` |
| 70 | + |
| 71 | +:::tip |
| 72 | +For some managed MySQL / MySQL-compatible cloud databases (e.g., TiDB Serverless, Planetscale), connections are usually required to use SSL. It is recommended to use the Cloudflare managed connection pool [Hyperdrive](https://developers.cloudflare.com/hyperdrive/) to connect. |
| 73 | +::: |
| 74 | + |
| 75 | +## Step 3: Test locally |
| 76 | + |
| 77 | +``` |
| 78 | +wrangler dev |
| 79 | +``` |
0 commit comments