MySQL

If your infrastructure already runs MySQL, you can use it directly with Memori without setting up a separate database.

TiDB and TiDB Cloud use the same connection pattern. If you're using TiDB, see the dedicated TiDB page for the recommended setup and examples.

Install

Install
pip install memori pymysql

Quick Start

MySQL Connection
from memori import Memori
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

engine = create_engine(
    "mysql+pymysql://user:password@localhost:3306/memori_db",
    pool_pre_ping=True
)
SessionLocal = sessionmaker(bind=engine)

mem = Memori(conn=SessionLocal)
mem.config.storage.build()

Connection Strings (Python)

DriverConnection String
PyMySQLmysql+pymysql://user:pass@host:3306/database
mysqlclientmysql+mysqldb://user:pass@host:3306/database
With charsetmysql+pymysql://user:pass@host:3306/database?charset=utf8mb4
With SSLmysql+pymysql://user:pass@host:3306/database?ssl_ca=/path/to/ca.pem

Complete Example

Complete Example
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from memori import Memori
from openai import OpenAI

engine = create_engine(
    "mysql+pymysql://user:password@localhost:3306/memori_db"
    "?charset=utf8mb4",
    pool_pre_ping=True,
    pool_size=5,
    max_overflow=10,
    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 at Acme Corp as a designer."}]
)
print(response.choices[0].message.content)

mem.augmentation.wait()
facts = mem.recall("workplace")
print(facts)

Notes (TypeScript)

  • Import from mysql2/promise, not mysql2 — Memori expects the modern, promise-based interface.
  • Pass a factory function: conn: () => pool. Memori never closes the pool — you own its lifecycle and call pool.end() when you're done.
  • Use mysql.createPool(), not mysql.createConnection() — a pool safely handles the concurrent reads, writes, and background augmentation that Memori performs.
  • mysql2 ships with built-in TypeScript types — no separate @types/mysql2 package is needed.
  • Set DATABASE_CONNECTION_STRING in your .env file.