Skip to content

rustmy

A fast MySQL and MariaDB driver for Python, written in Rust. DB-API 2.0 and asyncio, connection pools, SQLAlchemy and Django, and wheels with nothing else to install.

$pip install rustmy

PyPI Python versions Free-threaded Downloads GitHub stars CI License

3.3×faster than mysqlclient reading 1M rows
3.3×faster inserting 1M rows with executemany
1.5×faster than asyncmy inserting 1M rows on asyncio
0system libraries to install

Time to read and insert 1,000,000 rows, and to load 1,000,000 objects through the SQLAlchemy and Django ORMs, with rustmy, mysqlclient, PyMySQL and mysql-connector-python (lower is better) Time to read and insert 1,000,000 rows, and to load 1,000,000 objects through the SQLAlchemy and Django ORMs, with rustmy, mysqlclient, PyMySQL and mysql-connector-python (lower is better)

Write it the way you already do

import rustmy

with rustmy.connect(
    host="localhost", user="app", password="secret", database="shop"
) as conn:
    with conn.cursor() as cursor:
        cursor.execute("SELECT id, name, price FROM products WHERE price < %s", (10,))
        for product_id, name, price in cursor:
            print(product_id, name, price)
    conn.commit()
import asyncio

from rustmy import aio


async def main() -> None:
    async with aio.create_pool(
        host="localhost", user="app", password="secret", database="shop"
    ) as pool:
        async with pool.acquire() as conn, conn.cursor() as cursor:
            await cursor.execute(
                "SELECT id, name FROM products WHERE price < %s", (10,)
            )
            for product_id, name in await cursor.fetchall():
                print(product_id, name)


asyncio.run(main())
from sqlalchemy import create_engine, text

engine = create_engine("mysql+rustmy://app:secret@localhost/shop", pool_pre_ping=True)
with engine.connect() as conn:
    rows = conn.execute(text("SELECT id, name FROM products")).all()

On asyncio, create_async_engine("mysql+rustmy://app:secret@localhost/shop").

settings.py
import rustmy

rustmy.install_as_MySQLdb()

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "shop",
        "USER": "app",
        "PASSWORD": "secret",
        "HOST": "localhost",
    }
}

Why rustmy

  • Fast where it counts


    Rows are read in small batches with the GIL released and converted in Rust while the server sends the next batch. executemany batches inserts and pipelines everything else.

    Performance

  • Nothing to install but the wheel


    No libmysqlclient, no compiler, no pkg-config. TLS is rustls, and the supported authentication methods are implemented in Rust.

    Installation

  • asyncio on the same core


    rustmy.aio runs the same Rust protocol code on the event loop's thread, with the API of aiomysql and asyncmy. No thread per query.

    asyncio

  • Pools for threads and tasks


    rustmy.Pool and rustmy.aio.create_pool serve waiters in order of arrival, ping idle connections and roll back what is given back open.

    Connection pools

  • SQLAlchemy and Django


    mysql+rustmy:// and mariadb+rustmy://, blocking and asyncio, and Django's MySQL backend through install_as_MySQLdb().

    Integrations

  • Correct and tested hard


    The server's definition of every character set, property-based tests of escaping and injection, fuzzing, and nine server versions in CI.

    How it is tested

Supported

Supported
Python CPython 3.10 to 3.15, free-threaded 3.14t and 3.15t
MySQL 8.0, 8.4 LTS, 9.7, 26.x
MariaDB 10.11, 11.4, 11.8, 12.3, 13.x
Frameworks SQLAlchemy 2.0 and 2.1 (blocking and asyncio), Django 5.2 LTS and 6.x
Platforms Linux x86_64 and aarch64 (glibc, musl), macOS x86_64 and arm64, Windows x64 and arm64