MySQL driver for asyncio.
Project Links
Meta
Author: Nikolay Novik
Requires Python: >=3.9
Classifiers
Intended Audience
- Developers
Programming Language
- Python :: 3
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
Operating System
- POSIX
Environment
- Web Environment
Development Status
- 3 - Alpha
Topic
- Database
- Database :: Front-Ends
Framework
- AsyncIO
aiomysql is a “driver” for accessing a MySQL database from the asyncio (PEP-3156/tulip) framework. It depends on and reuses most parts of PyMySQL . aiomysql tries to be like awesome aiopg library and preserve same api, look and feel.
Internally aiomysql is copy of PyMySQL, underlying io calls switched to async, basically yield from and asyncio.coroutine added in proper places)). sqlalchemy support ported from aiopg.
Documentation
Basic Example
aiomysql based on PyMySQL , and provides same api, you just need to use await conn.f() or yield from conn.f() instead of calling conn.f() for every method.
Properties are unchanged, so conn.prop is correct as well as conn.prop = val.
import asyncio
import aiomysql
async def test_example():
async with aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='mysql') as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 42;")
print(cur.description)
(r,) = await cur.fetchone()
assert r == 42
asyncio.run(test_example())
Example of SQLAlchemy optional integration
Sqlalchemy support has been ported from aiopg so api should be very familiar for aiopg user.:
import asyncio
import sqlalchemy as sa
from aiomysql.sa import create_engine
metadata = sa.MetaData()
tbl = sa.Table('tbl', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('val', sa.String(255)))
async def go():
engine = await create_engine(user='root', db='test_pymysql',
host='127.0.0.1', password='')
async with engine.acquire() as conn:
await conn.execute(tbl.insert().values(val='abc'))
await conn.execute(tbl.insert().values(val='xyz'))
async for row in conn.execute(tbl.select()):
print(row.id, row.val)
engine.close()
await engine.wait_closed()
asyncio.run(go())
Requirements
0.3.2
Oct 22, 2025
0.3.1
Oct 21, 2025
0.3.0
Oct 21, 2025
0.2.0
Jun 11, 2023
0.1.1
May 08, 2022
0.1.0
Apr 11, 2022
0.1.0rc2
Apr 11, 2022
0.1.0rc1
Apr 11, 2022
0.0.22
Nov 14, 2021
0.0.21
Nov 26, 2020
0.0.20
Dec 19, 2018
0.0.19
Jul 11, 2018
0.0.17
Jul 06, 2018
0.0.16
Jun 03, 2018
0.0.15
May 20, 2018
0.0.14
Apr 22, 2018
0.0.13
Apr 19, 2018
0.0.12
Jan 18, 2018
0.0.11
Dec 06, 2017
0.0.10
Dec 06, 2017
0.0.9
Sep 14, 2016
0.0.8
Aug 24, 2016
0.0.7
Jan 27, 2016
0.0.6
Dec 10, 2015
0.0.5
Oct 27, 2015
0.0.4
May 23, 2015
0.0.3
Mar 10, 2015
0.0.2
Feb 17, 2015
0.0.1
Feb 17, 2015