awswrangler.mysql.connect

awswrangler.mysql.connect(connection: str | None = None, secret_id: str | None = None, catalog_id: str | None = None, dbname: str | None = None, boto3_session: Session | None = None, read_timeout: int | None = None, write_timeout: int | None = None, connect_timeout: int = 10, cursorclass: type['Cursor'] | None = None) pymysql.connections.Connection

Return a pymysql connection from a Glue Catalog Connection or Secrets Manager.

https://pymysql.readthedocs.io

Note

You MUST pass a connection OR secret_id. Here is an example of the secret structure in Secrets Manager: { “host”:”mysql-instance-wrangler.dr8vkeyrb9m1.us-east-1.rds.amazonaws.com”, “username”:”test”, “password”:”test”, “engine”:”mysql”, “port”:”3306”, “dbname”: “mydb” # Optional }

Note

It is only possible to configure SSL using Glue Catalog Connection. More at: https://docs.aws.amazon.com/glue/latest/dg/connection-defining.html

Note

Consider using SSCursor cursorclass for queries that return a lot of data. More at: https://pymysql.readthedocs.io/en/latest/modules/cursors.html#pymysql.cursors.SSCursor

Parameters:
  • connection (str | None) – Glue Catalog Connection name.

  • secret_id (str | None) – Specifies the secret containing the connection details that you want to retrieve. You can specify either the Amazon Resource Name (ARN) or the friendly name of the secret.

  • catalog_id (str | None) – The ID of the Data Catalog. If none is provided, the AWS account ID is used by default.

  • dbname (str | None) – Optional database name to overwrite the stored one.

  • boto3_session (Session | None) – The default boto3 session will be used if boto3_session is None.

  • read_timeout (int | None) – The timeout for reading from the connection in seconds (default: None - no timeout). This parameter is forward to pymysql. https://pymysql.readthedocs.io/en/latest/modules/connections.html

  • write_timeout (int | None) – The timeout for writing to the connection in seconds (default: None - no timeout) This parameter is forward to pymysql. https://pymysql.readthedocs.io/en/latest/modules/connections.html

  • connect_timeout (int) – Timeout before throwing an exception when connecting. (default: 10, min: 1, max: 31536000) This parameter is forward to pymysql. https://pymysql.readthedocs.io/en/latest/modules/connections.html

  • cursorclass (type[Cursor] | None) – Cursor class to use, e.g. SSCursor; defaults to pymysql.cursors.Cursor https://pymysql.readthedocs.io/en/latest/modules/cursors.html

Return type:

Connection

Returns:

pymysql connection.

Examples

>>> import awswrangler as wr
>>> with wr.mysql.connect("MY_GLUE_CONNECTION") as con:
...     with con.cursor() as cursor:
...         cursor.execute("SELECT 1")
...         print(cursor.fetchall())