Chalk home page
Docs
API
CLI
  1. Integrations
  2. MySQL

Chalk supports MySQL as a SQL source. You can configure the MySQL-specific options using the MySQLSource init args, or configure the source through your dashboard, and reference the source in your code.

Adding MySQL

On the dashboard, you can plug in the configuration for your MySQL database:

Add MySQL

Add a MySQL integration.

MySQL
Environment

Learn more about Chalk's MySQL Integration

Single Integration

If you have only one MySQL connection that you’d like to add to Chalk, you do not need to specify any arguments to construct the source in your code.

from chalk.sql import MySQLSource

mysql = MySQLSource()

@online
def fn(...) -> ...:
    return mysql.query(...).first()

Multiple Integrations

Chalk's injects environment variables to support data integrations. But what happens when you have two data sources of the same kind? When you create a new data source from your dashboard, you have an option to provide a name for the integration. You can then reference this name in the code directly.
from chalk.sql import MySQLSource

risk = MySQLSource(name="RISK")
marketing = MySQLSource(name="MARKETING")

@online
def risk_resolver(...) -> ...:
    return risk.query(...).first()

@online
def marketing_resolver(...) -> ...:
    return marketing.query(...).first()
Named integrations inject environment variables with the standard names prefixed by the integration name. For example, if your integration is called RISK, then the variable MYSQL_HOST will be injected as RISK_MYSQL_HOST. The first integration of a given kind will also create the un-prefixed environment variable (ie. both MYSQL_HOST and RISK_MYSQL_HOST).

Environment Variables

You can also configure the integration directly using environment variables on your local machine or from those added through the generic environment variable support.

import os
from chalk.sql import MySQLSource

mysql = MySQLSource(
    host=os.getenv(...),
    port=os.getenv(...),
    db=os.getenv(...),
    user=os.getenv(...),
    password=os.getenv(...),
)

@online
def fn(...) -> ...:
    return mysql.query(...).first()