Git Product home page Git Product logo

worker-mysql's Introduction

Cloudflare Workers + MySQL

This repo contains example code and a MySQL driver that can be used in any Workers project. If you are interested in using the driver outside of this template, copy the driver/mysql module into your project's node_modules or directly alongside your source.

Usage

Before you start, please refer to the official tutorial.

const mysql = new Client()
const mysqlClient = await mysql.connect({
    username: '<DATABASE_USER>',
    db: '<DATABASE_NAME>',
    // hostname is the full URL to your pre-created Cloudflare Tunnel, see documentation here:
    // https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/create-tunnel
    hostname: env.TUNNEL_HOST || 'https://dev.example.com',
    password: env.DATABASE_PASSWORD, // use a secret to store passwords
})

Please Note:

  • you must use this config object format vs. a database connection string
  • the hostname property must be the URL to your Cloudflare Tunnel, NOT your database host
    • your Tunnel will be configured to connect to your database host

Running the MySQL Demo

mysql/docker-compose.yml

This docker-compose composition will get you up and running with a local instance of mysql and a copy of cloudflared to enable your applications to securely connect through an encrypted tunnel. Unlike the PostgreSQL example, this does not contain any server-side connection pool, but you can configure one behind cloudflared should it be necessary.

Usage

from within scripts/mysql, run:

  1. Create credentials file (first time only)
docker run -v ~/.cloudflared:/etc/cloudflared cloudflare/cloudflared:2021.10.5 login
  1. Start a local dev stack (cloudflared/mysql)
TUNNEL_HOSTNAME=dev.example.com docker-compose up

worker-mysql's People

Contributors

nilslice avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.