How-to Guide for Mysql using SQLExecuteQueryOperator

Use the SQLExecuteQueryOperator to execute SQL commands in a MySql database.

Previously, MySqlOperator was used to perform this kind of operation. But after deprecation it was removed. Please use SQLExecuteQueryOperator instead.

Using the Operator

Use the conn_id argument to connect to your MySql instance where the connection metadata is structured as follows:

MySql Airflow Connection Metadata

Parameter

Input

Host: string

MySql hostname

Schema: string

Set schema to execute Sql operations on by default

Login: string

MySql user

Password: string

MySql user password

Port: int

MySql port

An example usage of the SQLExecuteQueryOperator is as follows:

tests/system/mysql/example_mysql.py[source]


    drop_table_mysql_task = SQLExecuteQueryOperator(
        task_id="drop_table_mysql", sql=r"""DROP TABLE table_name;""", dag=dag
    )

You can also use an external file to execute the SQL commands. Script folder must be at the same level as DAG.py file.

tests/system/mysql/example_mysql.py[source]


    mysql_task = SQLExecuteQueryOperator(
        task_id="drop_table_mysql_external_file",
        sql="/scripts/drop_table.sql",
        dag=dag,
    )

Note

Parameters that can be passed onto the operator will be given priority over the parameters already given in the Airflow connection metadata (such as schema, login, password and so forth).

Was this entry helpful?