TiDB
TiDB speaks the MySQL wire protocol, so Memori integrates with it through the
same SQLAlchemy / PyMySQL path as MySQL. Memori auto-detects TiDB from
SELECT VERSION() and routes it through a dedicated TiDB integration path
that reuses the MySQL storage family.
Install
Install TiDB Driver
pip install memori pymysql sqlalchemy certifi
Quick Start
TiDB Connection
from memori import Memori
import certifi
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine(
"mysql+pymysql://user:password@host:4000/memori_db?charset=utf8mb4",
connect_args={"ssl": {"ca": certifi.where()}},
pool_pre_ping=True,
pool_recycle=1800,
)
SessionLocal = sessionmaker(bind=engine)
mem = Memori(conn=SessionLocal)
mem.config.storage.build()
Connection Strings
| Environment | Connection String |
|---|---|
| Local / Self-hosted | mysql+pymysql://user:pass@host:4000/memori_db?charset=utf8mb4 |
| TiDB Cloud | mysql+pymysql://user:pass@gateway.example.tidbcloud.com:4000/memori_db?charset=utf8mb4 |
| With TLS params | mysql+pymysql://user:pass@host:4000/memori_db?charset=utf8mb4&ssl_verify_cert=true |
Use the connection string provided by your TiDB deployment or TiDB Cloud
instance. For TiDB Cloud Serverless with SQLAlchemy + PyMySQL, pass TLS through
connect_args={"ssl": {"ca": certifi.where()}} so the driver uses a trusted
CA bundle. Memori does not require a separate adapter.
Complete Example
import os
import certifi
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from memori import Memori
from openai import OpenAI
engine = create_engine(
os.getenv("DATABASE_CONNECTION_STRING"),
connect_args={"ssl": {"ca": certifi.where()}} if os.getenv("DATABASE_USE_TLS") else {},
pool_pre_ping=True,
pool_recycle=1800,
)
SessionLocal = sessionmaker(bind=engine)
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
mem = Memori(conn=SessionLocal).llm.register(client)
mem.attribution(entity_id="user_123", process_id="my_agent")
mem.config.storage.build()
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "I work on distributed systems."}]
)
print(response.choices[0].message.content)
mem.augmentation.wait()
facts = mem.recall("distributed systems")
print(facts)
Why TiDB?
- MySQL compatibility: Works with the same client stack most Python apps already use.
- Distributed SQL: Good match for multi-tenant memory services that need horizontal growth.
- Operational simplicity: Easy path from local prototyping to TiDB Cloud without changing Memori code.
Notes
- TiDB currently follows the same Memori storage family as MySQL. This keeps the integration low-risk and smooth for other MySQL-compatible systems.
- TiDB Cloud Serverless requires secure transport. With SQLAlchemy + PyMySQL, the simplest working setup is
certifiplusconnect_args={"ssl": {"ca": certifi.where()}}. - If you later want TiDB-specific optimizations, such as native vector support, those can be layered in separately without changing the basic BYODB path.