How to Integrate MySQL with dbt in PostgreSQL

Galaxy Glossary

How do I integrate MySQL with dbt?

Connect MySQL to dbt so you can build, test, and document analytics models directly on your MySQL database.

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 use dbt with MySQL?

dbt turns raw MySQL tables into reliable analytics models with version control, testing, and documentation—all without moving data to another warehouse.

What do I need before starting?

Install Python 3.8+, pip install dbt-mysql, and create a MySQL user with SELECT, CREATE, and INSERT privileges on the target database.

How do I configure profiles.yml?

Store connection details under ~/.dbt/profiles.yml. Set type: mysql, host, port, user, password, schema, and threads.

Sample profiles.yml

my_mysql_project:
target: dev
outputs:
dev:
type: mysql
server: 127.0.0.1
port: 3306
user: analytics
password: {{ env_var('MYSQL_PW') }}
schema: ecommerce
threads: 4

How do I run dbt commands?

Use dbt run to materialize models, dbt test for data quality, and dbt build to execute both in one step.

How can I model Customers and Orders?

Create a SQL file in models/ that joins operational tables, then reference it with ref() in downstream models.

models/customers_with_orders.sql

select
c.id as customer_id,
c.name,
c.email,
o.id as order_id,
o.order_date,
o.total_amount
from {{ source('ecommerce', 'Customers') }} c
left join {{ source('ecommerce', 'Orders') }} o
on o.customer_id = c.id

How do I add tests?

In schema.yml, declare unique and not_null tests on customer_id to catch duplicates and blanks.

What are best practices?

  • Use incremental models with unique_key to avoid full reloads.
  • Add indexes in post-hook SQL for faster joins.
  • Pin adapter versions in requirements.txt.

Common mistakes and fixes

Wrong adapter type: Setting type: postgres instead of mysql breaks authentication. Fix by updating the profile.

No unique_key: Incremental models rerun the entire table without it. Add unique_key: id to the model config.

Next steps

Schedule dbt build in CI/CD, publish docs with dbt docs serve, and monitor query performance.

Why How to Integrate MySQL with dbt in PostgreSQL is important

How to Integrate MySQL with dbt in PostgreSQL Example Usage


-- models/top_products.sql

select
  p.id          as product_id,
  p.name,
  sum(oi.quantity) as units_sold,
  sum(oi.quantity * p.price) as revenue
from  p
join  oi on oi.product_id = p.id
group by 1,2
order by revenue desc
limit 10;

How to Integrate MySQL with dbt in PostgreSQL Syntax


# profiles.yml (connection syntax)
my_mysql_project:
  target: dev
  outputs:
    dev:
      type: mysql           # adapter name
      server: 127.0.0.1     # host
      port: 3306            # port
      user: analytics       # MySQL user
      password: 
      schema: ecommerce     # database name
      threads: 4            # parallelism

# CLI commands
$ dbt debug                 # test connection
$ dbt run                   # build models
$ dbt test                  # execute tests
$ dbt docs generate         # create docs site
$ dbt build                 # run, test, and post-hooks

Common Mistakes

Frequently Asked Questions (FAQs)

Can dbt run incremental models on MySQL?

Yes. Add {{ config(materialized='incremental', unique_key='id') }} and filter new rows with where + is_incremental().

Does dbt support MySQL 8 authentication?

dbt-mysql works with MySQL 8. Use native or caching_sha2_password auth; verify with dbt debug.

How do I source raw tables in MySQL?

Define sources: in schema.yml with database and table names, then reference them with source() in models.

Want to learn about other SQL terms?

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