> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MySQL Conversation Store

> Direct MySQL conversation persistence with connection pooling

MySQL conversation store persists agent sessions and messages with automatic schema creation and connection pooling.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent, db

agent = Agent(
    name="MySQL Agent",
    instructions="Store conversations in MySQL",
    db=db(database_url="mysql://user:pass@localhost:3306/praisonai"),
    session_id="mysql-session",
)
agent.start("Hello — save this conversation")
```

The user continues a thread; MySQL stores messages with connection pooling and auto schema.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    Agent[🤖 Agent] --> Store[🗃️ MySQL Store]
    Store --> Pool[🔗 Connection Pool]
    Pool --> DB[(🐬 MySQL)]

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef store fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef database fill:#10B981,stroke:#7C90A0,color:#fff

    class Agent agent
    class Store,Pool store
    class DB database
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    pip install mysql-connector-python praisonai
    ```

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent, db

    agent = Agent(
        name="MySQL Agent",
        db=db(database_url="mysql://user:pass@localhost:3306/praisonai"),
        session_id="session-1",
    )
    agent.start("Hello!")
    ```
  </Step>

  <Step title="With Configuration">
    Use `MySQLConversationStore` directly when you need table prefixes or pool tuning:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonai.persistence.conversation.mysql import MySQLConversationStore
    from praisonai.persistence import create_conversation_store

    store = MySQLConversationStore(
        host="localhost",
        port=3306,
        database="praisonai",
        user="root",
        password="secret",
        table_prefix="praison_",
        auto_create_tables=True,
        pool_size=5,
    )

    # Or via the registry
    store = create_conversation_store("mysql", url="mysql://user:pass@localhost/praisonai")
    ```
  </Step>
</Steps>

***

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant Agent
    participant Store as MySQLConversationStore
    participant MySQL

    Agent->>Store: save session / message
    Store->>MySQL: CREATE TABLE IF NOT EXISTS
    Store->>MySQL: INSERT (pooled connection)
    MySQL-->>Agent: persisted
```

| Feature                | Description                                                            |
| ---------------------- | ---------------------------------------------------------------------- |
| **Schema management**  | Auto-creates sessions and messages tables (`SCHEMA_VERSION = "1.0.0"`) |
| **Connection pooling** | Configurable `pool_size` for concurrent agents                         |
| **URL parsing**        | `mysql://user:pass@host:port/database` format supported                |

***

## Configuration Options

| Option               | Type   | Default       | Description                                   |
| -------------------- | ------ | ------------- | --------------------------------------------- |
| `url`                | `str`  | `None`        | Full MySQL URL (overrides individual options) |
| `host`               | `str`  | `"localhost"` | MySQL server hostname                         |
| `port`               | `int`  | `3306`        | MySQL server port                             |
| `database`           | `str`  | `"praisonai"` | Database name                                 |
| `user`               | `str`  | `"root"`      | Database username                             |
| `password`           | `str`  | `""`          | Database password                             |
| `table_prefix`       | `str`  | `"praison_"`  | Prefix for table names                        |
| `auto_create_tables` | `bool` | `True`        | Create tables automatically                   |
| `pool_size`          | `int`  | `5`           | Connection pool size                          |

For async workloads, use `create_conversation_store("async_mysql", ...)` with `aiomysql`.

***

## Best Practices

<AccordionGroup>
  <Accordion title="Use db() for most agents">
    `Agent(db=db(database_url="mysql://..."))` is the simplest path — no manual store wiring needed.
  </Accordion>

  <Accordion title="Set table_prefix for multi-tenancy">
    Isolate apps on one database with different prefixes: `table_prefix="prod_"` vs `table_prefix="staging_"`.
  </Accordion>

  <Accordion title="Tune pool_size for concurrency">
    Increase `pool_size` for high-traffic deployments; use smaller pools for serverless MySQL hosts.
  </Accordion>

  <Accordion title="Use SSL in production">
    Append `?ssl_mode=REQUIRED` to the connection URL for encrypted connections.
  </Accordion>
</AccordionGroup>

***

## Related

<CardGroup cols={2}>
  <Card title="MySQL Persistence" icon="database" href="/docs/features/persistence-mysql">
    MySQL overview with SSL and production patterns
  </Card>

  <Card title="Persistence Backend Plugins" icon="puzzle-piece" href="/docs/features/persistence-backend-plugins">
    Extend SQL backends via the registry
  </Card>
</CardGroup>
