---
title: "aiomysql"
description: "Learn about importing the aiomysql integration and how it captures queries from aiomysql as spans."
url: https://docs.sentry.io/platforms/python/integrations/aiomysql/
---

# aiomysql for Python

The aiomysql integration captures queries from [aiomysql](https://github.com/aio-libs/aiomysql/) as spans.

If you're using stream mode, this page's references to "transaction" should be applied to service spans instead. See [Streamed Spans](https://docs.sentry.io/platforms/python/tracing/streamed-spans.md) for more information.

## [Install](https://docs.sentry.io/platforms/python/integrations/aiomysql.md#install)

Install `sentry-sdk` from PyPI:

```bash
pip install sentry-sdk
```

*Other available variations of the above snippet: uv*

## [Configure](https://docs.sentry.io/platforms/python/integrations/aiomysql.md#configure)

Add `AioMySQLIntegration()` to your `integrations` list:

```python
import sentry_sdk
from sentry_sdk.integrations.aiomysql import AioMySQLIntegration

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
    data_collection={
        "database_query_data": True,
    },
    integrations=[
        AioMySQLIntegration(),
    ],
)
```

## [Verify](https://docs.sentry.io/platforms/python/integrations/aiomysql.md#verify)

```python
import sentry_sdk
from sentry_sdk.integrations.aiomysql import AioMySQLIntegration
import aiomysql
import asyncio

sentry_sdk.init(
    dsn="https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    traces_sample_rate=1.0,
    # Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
    data_collection={
        "database_query_data": True,
    },
    integrations=[
        AioMySQLIntegration(),
    ],
)

async def main():
    conn = await aiomysql.connect(
        host="localhost",
        port=3306,
        user="root",
        password="root",
        db="test",
    )
    try:
        # or sentry_sdk.traces.start_span(name="testing_sentry", parent_span=None) in stream mode
        with sentry_sdk.start_transaction(name="testing_sentry"):
            async with conn.cursor() as cur:
                await cur.execute("SELECT 'Hello World'")
                result = await cur.fetchone()
    finally:
        conn.close()

asyncio.run(main())
```

This will create a transaction called `testing_sentry` in the Performance section of [sentry.io](https://sentry.io) and will create a span for the `SELECT` statement.

It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io).

## [Supported Versions](https://docs.sentry.io/platforms/python/integrations/aiomysql.md#supported-versions)

* aiomysql: 0.3+

If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK. The minimum versions for each major version are listed above.
