MySqlOperator

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

Using the Operator

Use the mysql_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 MySqlOperator is as follows:

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


    drop_table_mysql_task = MySqlOperator(
        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/providers/mysql/example_mysql.py[source]


    mysql_task = MySqlOperator(
        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?