Registry / database / aiomysql

aiomysql

JSON →
library0.3.2pypypi✓ verified 25d ago

aiomysql is a MySQL driver for asyncio, enabling asynchronous interaction with MySQL databases using Python's `async`/`await` syntax. It provides a familiar DB-API 2.0-like interface adapted for asyncio. The current version is 0.3.2. Releases are infrequent, typically driven by critical bug fixes, `PyMySQL` updates, or community contributions.

pip install aiomysql
INSTALL
IMPORT
SIG · AIOMYSQL
A
aiomysql
databasepythonv0.3.2
Install
1.7s avg
Import
251ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.268s · 18.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.234s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

connect
from aiomysql import connect
import aiomysql
create_pool
from aiomysql import create_pool
import aiomysql
Connection
from aiomysql import Connection
import aiomysql

This quickstart demonstrates how to connect to a MySQL database, create a table, insert data, and fetch results using `aiomysql`. It uses `async with` statements for robust connection and cursor management, ensuring resources are properly closed. Database credentials are retrieved from environment variables for security.

import asyncio import os import aiomysql async def main(): # Get credentials from environment variables for security db_host = os.environ.get('MYSQL_HOST', '127.0.0.1') db_user = os.environ.get('MYSQL_USER', 'root') db_password = os.environ.get('MYSQL_PASSWORD', 'password') db_name = os.environ.get('MYSQL_DB', 'test_db') try: # Establish an asynchronous connection async with await aiomysql.connect( host=db_host, user=db_user, password=db_password, db=db_name, autocommit=True # Or manage transactions manually ) as conn: print(f"Connected to MySQL on {db_host}") # Create a cursor object async with conn.cursor() as cursor: # Execute a query await cursor.execute("SELECT VERSION();") # Fetch one result version = await cursor.fetchone() print(f"MySQL Version: {version[0]}") # Execute another query (e.g., create a table) await cursor.execute( "CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255));" ) print("Table 'users' ensured to exist.") # Insert data await cursor.execute("INSERT INTO users (name) VALUES (%s);", ("Alice",)) await cursor.execute("INSERT INTO users (name) VALUES (%s);", ("Bob",)) print("Inserted Alice and Bob.") # Select data await cursor.execute("SELECT id, name FROM users;") users = await cursor.fetchall() print("Users:") for user_id, name in users: print(f" ID: {user_id}, Name: {name}") # The connection is automatically closed when exiting the 'async with' block except Exception as e: print(f"An error occurred: {e}") if __name__ == '__main__': asyncio.run(main())
Debug
Known issues
breakingThe `aiomysql.connect` function became an `async def` function in version 0.2.0. Previously, it returned a `Future`.
fix
Ensure you `await` the `aiomysql.connect()` call directly, e.g., `conn = await aiomysql.connect(...)`. For older versions, you might need `conn = await asyncio.ensure_future(aiomysql.connect(...))` or similar.
affects: <0.2.0
gotchaForgetting to `await` asynchronous operations will lead to `RuntimeWarning: coroutine '...' was never awaited` or unexpected behavior.
fix
Always prepend `await` to calls like `aiomysql.connect()`, `cursor.execute()`, `cursor.fetchone()`, `pool.acquire()`, and `pool.release()`.
affects: All
gotchaNot using connection pooling for high-concurrency applications can lead to performance bottlenecks and resource exhaustion.
fix
For applications with many concurrent database operations, use `await aiomysql.create_pool(...)` to manage a pool of connections. Acquire a connection from the pool with `await pool.acquire()` and release it with `pool.release(conn)` (or use `async with pool.acquire() as conn:`).
affects: All
gotchaImproperly closing connections and cursors can lead to resource leaks.
fix
Always use `async with await aiomysql.connect(...) as conn:` and `async with conn.cursor() as cursor:` to ensure connections and cursors are properly closed, even if errors occur.
affects: All
gotchaFailed to connect to the MySQL server. This can be caused by the server not running, incorrect host/port, firewall issues, or network configuration.
fix
Ensure your MySQL server is running and accessible from where your application is running. Verify the host, port, username, and password in your `aiomysql.connect()` parameters. Check firewall rules and network connectivity between your application and the database server.
affects: All
gotchaThe application failed to connect to the MySQL server. This error typically indicates that the database server is not running, is not accessible from the specified host and port, or network/firewall rules are preventing the connection.
fix
Ensure the MySQL server is running, configured to accept connections from the application's host and port, and that no network or firewall rules are blocking the connection.
affects: All
Upgrade
Version history
0.3.2latest on PyPI · released Oct 22, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
60 hits · last 30 days
node
50
OpenAI (training)
2
Resources
aiomysql — pip install aiomysql · libregistry