Create a dbt profile that authenticates to a MySQL database so you can build, test, and document models from the same analytics workflow.
Connecting MySQL to dbt lets you version-control data transformations, add automated tests, and generate documentation—without leaving your analytics workflow.
Run pip install dbt-mysql
. The adapter provides the MySQL connection, compiles Jinja templates, and pushes generated SQL to the database.
Edit ~/.dbt/profiles.yml
and add a target named dev
. Supply host, port, schema, user, and password. Use threads
to control parallel runs and ssl
to enable encryption.
profiles.yml
entry look like?default:
target: dev
outputs:
dev:
type: mysql
server: analytics-mysql.internal
port: 3306
schema: ecommerce
user: analytics_dbt
password: {{ env_var('MYSQL_DBT_PW') }}
ssl: true
threads: 4
Execute dbt debug --target dev
. A green check confirms credentials and SSL negotiation.
Create .sql
files under models/
. Each file returns a SELECT statement. dbt will materialize the query as a table or view in MySQL.
-- models/customer_ltv.sql
{{ config(materialized='table') }}
SELECT c.id AS customer_id,
SUM(o.total_amount) AS lifetime_spend
FROM {{ ref('Customers') }} c
JOIN {{ ref('Orders') }} o ON o.customer_id = c.id
GROUP BY c.id;
Add unique
and not_null
tests on customer_id
. Use accepted_values
to catch unexpected order statuses.
Run dbt run --target dev
, validate with dbt test
, and publish docs via dbt docs generate && dbt docs serve
.
Keep production credentials in environment variables, pin the adapter version, and limit threads
to avoid saturating the MySQL server.
Support comes via the open-source dbt-mysql
adapter. Install and pin a version to ensure compatibility.
Yes. The adapter supports materialized='incremental'
with unique_key
and is_incremental()
logic.
Set ssl: true
in the profile and point SSL_CERT
, SSL_KEY
, and SSL_CA
environment variables to your certificate files.