MariaDB-Airflow integration lets DAG tasks read and write MariaDB tables through Airflow’s MySqlHook/MySqlOperator.
It schedules, orchestrates, and monitors SQL jobs against MariaDB, replacing ad-hoc scripts with version-controlled, retry-aware DAGs.
Use the Airflow UI, CLI, or environment variables. Provide host, port (3306), user, password, and schema. Choose the “mysql” conn_type—MariaDB is wire-compatible.
airflow connections add mariadb_ecom \
--conn-uri 'mariadb://analytics:SECRETPW@db.prod.local:3306/ecommerce'
MySqlOperator(sql="…", mysql_conn_id="mariadb_ecom", parameters=None, autocommit=False, database=None, **kwargs) executes SQL strings or files. Use Jinja in sql
for templating.
add_daily_orders = MySqlOperator(
task_id="agg_daily_orders",
sql="sql/agg_daily_orders.sql",
mysql_conn_id="mariadb_ecom",
parameters={"ds": '{{ ds }}'},
)
MySqlHook
offers get_records()
, get_pandas_df()
, and run()
. It returns lists or DataFrames you can process further.
def stale_customers(**ctx):
hook = MySqlHook(mysql_conn_id="mariadb_ecom")
rows = hook.get_records("""
SELECT id, email
FROM Customers
WHERE created_at < DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
""")
# send email or write to log
Enable autocommit=True
for DDL or large ETL inserts to avoid implicit transaction overhead. Keep it False
when you need rollback safety.
{{ ds }}, {{ ds_nodash }}
).retries
and retry_delay
.Omitting the database name forces you to fully qualify every table. Add /ecommerce
in the URI to default the schema.
Running bulk INSERT … SELECT
without autocommit=True
can fill transaction logs and lock tables. Enable autocommit or split the job.
Launch a Docker MariaDB container, set an Airflow .env
with the same connection URI, and trigger the DAG with airflow dags test
.
with DAG(
dag_id="daily_ecommerce_metrics",
schedule_interval="0 2 * * *",
start_date=datetime(2023, 1, 1),
catchup=False,
) as dag:
agg_orders = MySqlOperator(
task_id="agg_daily_orders",
sql="sql/agg_daily_orders.sql",
mysql_conn_id="mariadb_ecom",
autocommit=True,
)
mark_stale = PythonOperator(
task_id="flag_stale_customers",
python_callable=stale_customers,
)
agg_orders >> mark_stale
Galaxy’s AI copilot completes SQL, optimizes queries, and shares them across your team—perfect alongside Airflow.
Yes. The mysql-client Python driver supports MariaDB ≥10.3. Ensure Airflow image has mysqlclient
>=2.1 installed.
MySqlOperator runs inside one transaction unless autocommit=True
. Raise an exception to roll back.
Replace the conn_id, import path, and adjust SQL syntax (e.g., use ?
for parameter placeholders in MySqlHook).