Hyperdrive makes connecting to your regional SQL database from Cloudflare Workers fast by:
- Pooling database connections globally 🌎
- Eliminating roundtrips with edge connection setup 🔗
- Caching query results for speed and scale (optional) ⚡️
Check out the demo to see how Hyperdrive can provide up to 4x faster queries. Learn more about how Hyperdrive works to speed up your database access.
This project demonstrates a Worker connecting to a MySQL database using Hyperdrive. Upon loading your Worker, your will see an administrative dashboard that showcases simple create, read, update, delete commands to your MySQL database with Hyperdrive.
Important
When creating a Hyperdrive configuration as part of this template, disable caching from your Hyperdrive configuration to ensure your administrative shows updated values. Learn more about Hyperdrive's built-in query caching and when to use it.
When using C3 to create this project, select "no" when it asks if you want to deploy. You need to follow this project's setup steps before deploying.
Outside of this repo, you can start a new project with this template using C3 (the create-cloudflare CLI):
npm create cloudflare@latest -- --template=cloudflare/templates/mysql-hyperdrive-template
A live public deployment of this template is available at https://mysql-hyperdrive-template.templates.workers.dev
-
Install the project dependencies with a package manager of your choice:
npm install
-
Create a Hyperdrive configuration with the name "hyperdrive-configuration":
npx wrangler hyperdrive create hyperdrive-configuration --connection-string="mysql://<DB_USER>:<DB_PASSWORD>@<DB_HOSTNAME_OR_IP_ADDRESS>:3306/<DATABASE_NAME>" --caching-disabled...and update the
hyperdriveidfield inwrangler.jsonwith the new Hyperdrive ID. You can also specify a connection string for a local MySQL database used for development using thehyperdrivelocalConnectionStringfield. -
Deploy the project!
npx wrangler deploy
-
(Optional) To run your project locally while connecting to your remote database, you must use
wrangler dev --remotewhich will run your Worker in Cloudflare's environment so that you can access your remote database. Run the following:npx wrangler dev --remote