Skip to main content

Menu

Webinar

Built for the Agentic Era: Introducing MariaDB Enterprise Platform 2027

Register now

Product

MariaDB Python Connector 2.0.0rc2

By Diego Dupin

12 min read
Share

Key takeaways

  • This major update to the MariaDB Python Connector is a total ground-up rewrite that makes it much easier to install and significantly faster to run. Here are the key takeaways from the version 2.0 release:
  • Simplified installation eliminates the old requirement for pre-installed C libraries, offering a pure Python version that works anywhere and a "plug-and-play" binary option for maximum speed.
  • Native async/await support and full type hinting allow the connector to fit perfectly into modern Python frameworks like FastAPI and Starlette without extra workarounds.
  • Massive performance gains make this the fastest MariaDB driver available, with speeds up to 64 times faster than competitors when handling complex data and large-scale queries.
  • Modern connection features like standard URI strings (mariadb://...) and intelligent statement caching simplify your code while automatically reducing database overhead.
pip install mariadb[binary,pool]
pip install --pre mariadb # Pure Python -- CPython, PyPy, any platform 
pip install --pre mariadb[c] # C extension -- maximum performance
pip install --pre mariadb[binary] # Pre-compiled wheel -- performance, no setup
pip install --pre mariadb[pool] 
pip install --pre mariadb[c,pool]
pip install --pre mariadb[binary,pool]
pip install mariadb # pooling included automatically
pip install mariadb[pool] # explicit opt-in for pooling
import mariadb

pool = mariadb.create_pool(
host="localhost",
port=3306,
user="user",
password="password",
database="mydb",
min_size=5,
max_size=10,
ping_threshold=0.25
)

with pool.acquire() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT COUNT(*) FROM users")
count = cursor.fetchone()[0]
print(f"Total users: {count}")
import asyncio
import mariadb

async def main():
pool = await mariadb.create_async_pool(
host="localhost",
user="user",
password="password",
database="mydb",
min_size=5,
max_size=10
)

async with await pool.acquire() as conn:
async with conn.cursor() as cursor:
await cursor.execute("SELECT * FROM users WHERE id = ?", (1,))
row = await cursor.fetchone()
print(f"User: {row}")

await pool.close()

asyncio.run(main())
import asyncio
import mariadb

async def main():
# Single connection
async with await mariadb.asyncConnect("mariadb://user:pass@host/db") as conn:
async with conn.cursor() as cursor:
await cursor.execute("SELECT * FROM users WHERE id = ?", (1,))
row = await cursor.fetchone()
print(row)

# With connection pool (requires mariadb[pool])
pool = await mariadb.create_async_pool(
"mariadb://user:pass@host/db", min_size=1, max_size=20
)
async with await pool.acquire() as conn:
async with conn.cursor() as cursor:
await cursor.execute("SELECT 1")
result = await cursor.fetchone()
await pool.close()

asyncio.run(main())
# 1.1 -- verbose
conn = mariadb.connect(host="localhost", user="root", password="secret", database="mydb")
# 2.0 -- concise
conn = mariadb.connect("mariadb://root:secret@localhost:3306/mydb")
conn: mariadb.Connection = mariadb.connect(
host="localhost",
port=3306,
user="root",
password="secret",
database="mydb"
)

cursor: mariadb.Cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE id = ?", (1,))
row: tuple | None = cursor.fetchone()
rows: list[tuple] = cursor.fetchall()
cursor.close()
conn.close()
# Text protocol (default)
cursor.execute("SELECT ?", (b'\xde\xad',))
# sends: SELECT _binary'\xde\xad'

# Binary protocol (explicit)
cursor = conn.cursor(binary=True)
cursor.execute("SELECT ?", (b'\xde\xad',))
# uses COM_STMT_PREPARE + COM_STMT_EXECUTE

# Default: caching enabled with up to 100 statements
conn = mariadb.connect("mariadb://localhost/mydb")

# Larger cache for applications with many distinct queries
conn = mariadb.connect("mariadb://localhost/mydb?prep_stmt_cache_size=500")

# Disable caching entirely (1.1-like behavior)
conn = mariadb.connect("mariadb://localhost/mydb?cache_prep_stmts=false")
conn = mariadb.connect("mariadb://localhost/mydb?binary=true")
cur = conn.cursor()
cur.execute("SELECT * FROM users WHERE id = ?", (user_id,)) # binary

conn = mariadb.connect("mariadb://localhost/mydb")

# Hot path -- binary with caching
cur = conn.cursor(binary=True)
cur.execute("SELECT * FROM users WHERE id = ?", (user_id,))

# Ad-hoc query -- text protocol, no PREPARE overhead
cur2 = conn.cursor()
cur2.execute("SHOW PROCESSLIST")
# 2.0 -- all cursors use binary protocol by default
conn = mariadb.connect("mariadb://root:secret@localhost/mydb?binary=true")