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:

airflow/providers/mysql/example_dags/example_mysql.pyView Source


drop_table_mysql_task = MySqlOperator(
    task_id='create_table_mysql', mysql_conn_id='mysql_conn_id', 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.

airflow/providers/mysql/example_dags/example_mysql.pyView Source


mysql_task = MySqlOperator(
    task_id='create_table_mysql_external_file',
    mysql_conn_id='mysql_conn_id',
    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?