airflow.providers.mysql.hooks.mysql

This module allows to connect to a MySQL database.

Module Contents

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

Bases: airflow.hooks.dbapi.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"}

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(self, conn: Connection, autocommit: bool)[source]

MySql connection sets autocommit in a different way.

get_autocommit(self, conn: Connection)[source]

MySql connection gets autocommit in a different way.

Parameters

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

Returns

connection autocommit setting

Return type

bool

_get_conn_config_mysql_client(self, conn: Connection)[source]
_get_conn_config_mysql_connector_python(self, conn: Connection)[source]
get_conn(self)[source]

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

get_uri(self)[source]
bulk_load(self, table: str, tmp_file: str)[source]

Loads a tab-delimited file into a database table

bulk_dump(self, table: str, tmp_file: str)[source]

Dumps a database table into a tab-delimited file

static _serialize_cell(cell: object, conn: Optional[Connection] = None)[source]

MySQLdb converts an argument to a literal when passing those separately to execute. Hence, this method does nothing.

Parameters
  • cell (object) – The cell to insert into the table

  • conn (connection object) – The database connection

Returns

The same cell

Return type

object

get_iam_token(self, conn: Connection)[source]

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(self, table: str, tmp_file: str, duplicate_key_handling: str = 'IGNORE', extra_options: str = '')[source]

A more configurable way to load local data from a file into the database.

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

Was this entry helpful?