airflow.providers.mysql.hooks.mysql

This module allows to connect to a MySQL database.

Module Contents

Classes

MySqlHook

Interact with MySQL.

Attributes

logger

MySQLConnectionTypes

airflow.providers.mysql.hooks.mysql.logger[source]
airflow.providers.mysql.hooks.mysql.MySQLConnectionTypes[source]
class airflow.providers.mysql.hooks.mysql.MySqlHook(*args, **kwargs)[source]

Bases: airflow.providers.common.sql.hooks.sql.DbApiHook

Interact with MySQL.

You can specify charset in the extra field of your connection as {"charset": "utf8"}. Also you can choose cursor as {"cursor": "SSCursor"}. Refer to the MySQLdb.cursors for more details.

Note: For AWS IAM authentication, use iam in the extra connection parameters and set it to true. Leave the password field empty. This will use the “aws_default” connection to get the temporary token unless you override in extras. extras example: {"iam":true, "aws_conn_id":"my_aws_conn"}

You can also add “local_infile” parameter to determine whether local_infile feature of MySQL client is going to be enabled (it is disabled by default).

Parameters
  • schema – The MySQL database schema to connect to.

  • connection – The MySQL connection id used for MySQL credentials.

  • local_infile – Boolean flag determining if local_infile should be used

  • init_command – Initial command to issue to MySQL server upon connection

conn_name_attr = 'mysql_conn_id'[source]
default_conn_name = 'mysql_default'[source]
conn_type = 'mysql'[source]
hook_name = 'MySQL'[source]
supports_autocommit = True[source]
set_autocommit(conn, autocommit)[source]

Set autocommit.

mysqlclient uses an autocommit method rather than an autocommit property, so we need to override this to support it.

Parameters
  • conn (MySQLConnectionTypes) – connection to set autocommit setting

  • autocommit (bool) – autocommit setting

get_autocommit(conn)[source]

Whether autocommit is active.

mysqlclient uses an get_autocommit method rather than an autocommit property, so we need to override this to support it.

Parameters

conn (MySQLConnectionTypes) – connection to get autocommit setting from.

Returns

connection autocommit setting

Return type

bool

get_conn()[source]

Get connection to a MySQL database.

Establishes a connection to a mysql database by extracting the connection configuration from the Airflow connection.

Note

By default it connects to the database via the mysqlclient library. But you can also choose the mysql-connector-python library which lets you connect through ssl without any further ssl parameters required.

Returns

a mysql connection object

Return type

MySQLConnectionTypes

bulk_load(table, tmp_file)[source]

Load a tab-delimited file into a database table.

bulk_dump(table, tmp_file)[source]

Dump a database table into a tab-delimited file.

get_iam_token(conn)[source]

Retrieve a temporary password to connect to MySQL.

Uses AWSHook to retrieve a temporary password to connect to MySQL Port is required. If none is provided, default 3306 is used

bulk_load_custom(table, tmp_file, duplicate_key_handling='IGNORE', extra_options='')[source]

Load local data from a file into the database in a more configurable way.

Warning

According to the mysql docs using this function is a security risk. If you want to use it anyway you can do so by setting a client-side + server-side option. This depends on the mysql client library used.

Parameters
get_openlineage_database_info(connection)[source]

Return MySQL specific information for OpenLineage.

get_openlineage_database_dialect(_)[source]

Return database dialect.

get_openlineage_default_schema()[source]

MySQL has no concept of schema.

Was this entry helpful?