How to Connect MySQL to dbt in PostgreSQL

Galaxy Glossary

How do I connect a MySQL database to dbt?

Create a dbt profile that authenticates to a MySQL database so you can build, test, and document models from the same analytics workflow.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why link MySQL to dbt?

Connecting MySQL to dbt lets you version-control data transformations, add automated tests, and generate documentation—without leaving your analytics workflow.

Which adapter package do I install?

Run pip install dbt-mysql. The adapter provides the MySQL connection, compiles Jinja templates, and pushes generated SQL to the database.

How do I create a dbt profile for MySQL?

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.

What does a complete 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

How can I test the connection?

Execute dbt debug --target dev. A green check confirms credentials and SSL negotiation.

How do I model MySQL data in dbt?

Create .sql files under models/. Each file returns a SELECT statement. dbt will materialize the query as a table or view in MySQL.

Example: customer lifetime spend

-- 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;

What tests should I add?

Add unique and not_null tests on customer_id. Use accepted_values to catch unexpected order statuses.

How do I run and document the project?

Run dbt run --target dev, validate with dbt test, and publish docs via dbt docs generate && dbt docs serve.

Best practices to remember

Keep production credentials in environment variables, pin the adapter version, and limit threads to avoid saturating the MySQL server.

Why How to Connect MySQL to dbt in PostgreSQL is important

How to Connect MySQL to dbt in PostgreSQL Example Usage


-- Build a revenue per product view inside MySQL via dbt

SELECT p.id   AS product_id,
       p.name AS product_name,
       SUM(oi.quantity * p.price) AS gross_revenue
FROM  p
JOIN  oi ON oi.product_id = p.id
GROUP BY p.id, p.name;

How to Connect MySQL to dbt in PostgreSQL Syntax


profiles.yml syntax for MySQL in dbt

default:
  target: dev            # name of the target environment
  outputs:
    dev:
      type: mysql        # adapter package
      server: <host>     # MySQL endpoint
      port: 3306         # default port, override if needed
      schema: ecommerce  # database/schema to build models in
      user: <username>   # database user with CREATE and SELECT rights
      password: <pw>     # plaintext or 
      ssl: true|false    # enable TLS
      threads: <int>     # concurrent threads (1–8 recommended)

Optional keys:
  connect_timeout: 10    # seconds before timing out
  keepalives: 1          # maintain TCP keep-alive packets

Common Mistakes

Frequently Asked Questions (FAQs)

Does dbt officially support MySQL?

Support comes via the open-source dbt-mysql adapter. Install and pin a version to ensure compatibility.

Can I use incremental models?

Yes. The adapter supports materialized='incremental' with unique_key and is_incremental() logic.

How do I handle SSL certificates?

Set ssl: true in the profile and point SSL_CERT, SSL_KEY, and SSL_CA environment variables to your certificate files.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.