MySQL Database Python Docs | dltHub

Build a MySQL-to-database pipeline in Python using dlt with AI harness support for Claude Code, Cursor, and Codex.

Last updated:

MySQL is an open-source relational database management system. dlt's sql_database source connects to it through SQLAlchemy with the mysql+pymysql driver from the PyMySQL package, reads the table schemas from the database, and loads the tables you choose into any destination. See the dlt SQL database source docs for the full reference.


How do I choose what to load from MySQL?

The sql_database source reads the schema of each table you name and turns it into a resource, so the pipeline works with whatever tables your database holds. These options narrow a load down to what you need:

To loadUse
A single tablesql_table(table="orders")
A specific databasesql_database(schema="my_database")
Views as well as tablessql_database(include_views=True)
Only some columnssql_table(table="orders", included_columns=["id", "status"])
Only some rowssql_database(query_adapter_callback=...)

Always pass table_names=[...] to sql_database(): without it, dlt reflects every table in the database before loading anything.


How do I connect to the MySQL database?

dlt needs a database user with read access to the tables you want to load. Add its connection details to .dlt/secrets.toml:

[sources.sql_database.credentials] drivername = "mysql+pymysql" host = "localhost" port = 3306 username = "my_user" password = "my_password" database = "my_database"

Or as a single connection string:

[sources.sql_database] credentials = "mysql+pymysql://my_user:my_password@localhost:3306/my_database"

Use SQLAlchemy’s built-in MySQL dialect with a DBAPI driver such as mysqlclient (the default for the mysql URL) or PyMySQL via mysql+pymysql. For reliable reflection, declare table names in lowercase: MySQL’s identifier case behavior depends on the operating system and lower_case_table_names, and SQLAlchemy warns that reflected foreign-key table names are received in lowercase.

Every field of the connection string is documented in the sql_database configuration reference. dlt reads this automatically at runtime — never hardcode credentials in your pipeline script. For production environments, see setting up credentials with dlt for environment variable and vault-based options.


How do I set up and run the pipeline?

Set up a virtual environment and install dlt:

uv init uv add "dlt[hub,sql_database,duckdb]" pyarrow pandas PyMySQL

1. Install the dlt AI harness:

uv run dlthub ai init --agent <your-agent> # <agent>: claude | cursor | codex

This installs project rules, a secrets management skill, appropriate ignore files, and configures the dlt MCP server for your agent. Learn more →

2. Install the sql-database-pipeline toolkit:

uv run dlthub ai toolkit install sql-database-pipeline

This loads the skills and context about dlt the agent uses to connect to the database, choose tables, and build the pipeline safely. The agent uses MCP tools to inspect credentials — it never needs to read your secrets.toml directly. Learn more →

3. Start LLM-assisted coding:

Use /find-source to load tables from my MySQL database into DuckDB.

The sql-database-pipeline toolkit takes over from here — it checks whether a specialised source fits better, lists the schemas and tables in your database, asks which tables to load and whether you need normalization, then scaffolds a one-table pipeline and runs it with a load limit before scaling up.

4. Run the pipeline:

uv run python sql_database_pipeline.py

If everything is configured correctly, you'll see output like this:

Pipeline mysql_pipeline load step completed in 0.26 seconds 1 load package(s) were loaded to destination duckdb and into dataset mysql_data The duckdb destination used duckdb:/mysql_pipeline.duckdb location to store data Load package 1749667187.541553 is LOADED and contains no failed jobs

Inspect your pipeline and data:

uv run dlthub show

This opens the Pipeline Dashboard where you can verify pipeline state, load metrics, schema (tables, columns, types), and query the loaded data directly.


Python pipeline example

This example loads the customers and orders tables from the my_database database into DuckDB. Replace the table names with your own:

import dlt from dlt.sources.sql_database import sql_database def load_tables() -> None: source = sql_database( schema="my_database", table_names=["customers", "orders"], ) pipeline = dlt.pipeline( pipeline_name="mysql_pipeline", destination="duckdb", dataset_name="mysql_data", ) load_info = pipeline.run(source, write_disposition="replace") print(load_info) if __name__ == "__main__": load_tables()

Credentials are not passed in the code: sql_database() reads them from [sources.sql_database] in .dlt/secrets.toml.

Load only new and changed rows:

Reloading a large table on every run is rarely what you want. Give each table a cursor column and a primary key, and dlt loads only rows changed since the last run and merges them into the destination:

source.orders.apply_hints( incremental=dlt.sources.incremental("updated_at"), write_disposition="merge", primary_key="id", )

Use a timestamp such as updated_at for tables whose rows change, and an increasing id for append-only tables. The toolkit's /adjust-table skill sets this up for you.


Which backend should I use?

The backend decides how rows are read from MySQL. The default, sqlalchemy, works everywhere but is the slowest; switch once the pipeline works:

Data sizeBackendWhy
Under 100k rowssqlalchemy (default)Works with every destination, no extra dependencies
100k – 10M rowspyarrow20–30× faster, keeps decimal and date types exact; needs numpy
Over 10M rowsconnectorx or pyarrowFastest bulk reads
source = sql_database( schema="my_database", table_names=["customers", "orders"], backend="pyarrow", chunk_size=50000, )

pyarrow, pandas and connectorx skip dlt's row-by-row normalization. The toolkit's /optimize-sql-performance skill measures where time goes and picks a backend, chunk size and parallelism for you. See configuring the backend.


How do I query the loaded data?

After a run, pipeline.dataset() holds the loaded tables. A Relation is a query on that dataset, either a whole table or a SQL string, and reading it gives you the data as a Pandas DataFrame, a PyArrow table or Python tuples. Nothing is read until you ask for it.

import dlt pipeline = dlt.pipeline("mysql_pipeline") # Step 1: Get the dataset from the pipeline dataset = pipeline.dataset() # Step 2: Get a Relation, either a whole table... orders = dataset.table("orders") # ...or any SQL query against DuckDB recent = dataset("SELECT * FROM orders ORDER BY updated_at DESC LIMIT 100") # Step 3: Read the data df = orders.df() # or orders.df(chunk_size=50) for a large table arrow_table = recent.arrow() # a PyArrow Table rows = recent.fetchall() # a list of Python tuples

The columns are the ones of your MySQL tables; dataset.row_counts().df() lists every table with its row count. Your agent can run the same queries through the MCP server's execute_sql_query tool, and uv run dlthub show opens the Pipeline Dashboard with the schema, load metrics and a query box. See access loaded data in Python and exploring it in marimo notebooks.


What destinations can I load MySQL data to?

dlt supports loading into any of these destinations — only the destination parameter changes:

DestinationExample value
DuckDB (local, default)"duckdb"
PostgreSQL"postgres"
BigQuery"bigquery"
Snowflake"snowflake"
Redshift"redshift"
Databricks"databricks"
Filesystem (S3, GCS, Azure)"filesystem"

Change the destination in dlt.pipeline(destination="snowflake") and add credentials in .dlt/secrets.toml. See the full destinations list.


Next steps

Continue your data engineering journey with the other toolkits of the dltHub AI harness:

  • data-exploration — Build custom notebooks, charts, and dashboards for deeper analysis with marimo notebooks.
  • dlthub-platform — Deploy, schedule, and monitor your pipeline in production.
uv run dlthub ai toolkit install data-exploration uv run dlthub ai toolkit install dlthub-platform

Was this page helpful?

Community Hub

Need more dlt context for MySQL?

Request dlt skills, commands, AGENT.md files, and AI-native context.

Available Pipelines