awswrangler.mysql.connect

awswrangler.mysql.connect(connection: ~typing.Optional[str] = None, secret_id: ~typing.Optional[str] = None, catalog_id: ~typing.Optional[str] = None, dbname: ~typing.Optional[str] = None, boto3_session: ~typing.Optional[~boto3.session.Session] = None, read_timeout: ~typing.Optional[int] = None, write_timeout: ~typing.Optional[int] = None, connect_timeout: int = 10, cursorclass: ~typing.Type[~pymysql.cursors.Cursor] = <class 'pymysql.cursors.Cursor'>) pymysql.connections.Connection[Any]

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) – Glue Catalog Connection name.

  • secret_id (Optional[str]:) – 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, optional) – The ID of the Data Catalog. If none is provided, the AWS account ID is used by default.

  • dbname (Optional[str]) – Optional database name to overwrite the stored one.

  • boto3_session (boto3.Session(), optional) – Boto3 Session. The default boto3 session will be used if boto3_session receive None.

  • read_timeout (Optional[int]) – 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 (Optional[int]) – 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 (Cursor) – Cursor class to use, e.g. SSCursor; defaults to pymysql.cursors.Cursor https://pymysql.readthedocs.io/en/latest/modules/cursors.html

Returns

pymysql connection.

Return type

pymysql.connections.Connection

Examples

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