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

# MySQL

> Connect MySQL databases to CloudThinker for query analysis, performance monitoring, and database optimization

Connect your MySQL databases to enable [Tony](/guide/agents/tony) (Database Engineer) to analyze queries, optimize performance, and monitor database health.

## Supported platforms

| Platform                     | Support            |
| ---------------------------- | ------------------ |
| **Self-hosted MySQL**        | 5.7, 8.0, 8.1+     |
| **MariaDB**                  | 10.x               |
| **AWS RDS MySQL**            | All versions       |
| **AWS Aurora MySQL**         | All versions       |
| **Google Cloud SQL**         | All MySQL versions |
| **Azure Database for MySQL** | Flexible Server    |

## Prerequisites

* A MySQL instance reachable from CloudThinker over the network.
* Admin or root access to create a dedicated user and grant privileges.
* Performance Schema enabled (default in MySQL 5.7+; verify before connecting).

## Setup

<Steps>
  <Step title="Connect as root">
    Connect to MySQL as root or admin user:

    ```bash theme={null}
    mysql -h your-host -u root -p
    ```
  </Step>

  <Step title="Create a read-only user">
    Create the CloudThinker user:

    ```sql theme={null}
    CREATE USER 'cloudthinker_readonly'@'%' IDENTIFIED BY 'your-secure-password';
    ```
  </Step>

  <Step title="Grant read permissions">
    Grant SELECT and monitoring privileges:

    ```sql theme={null}
    GRANT SELECT ON *.* TO 'cloudthinker_readonly'@'%';
    GRANT SHOW DATABASES ON *.* TO 'cloudthinker_readonly'@'%';
    GRANT PROCESS ON *.* TO 'cloudthinker_readonly'@'%';
    ```
  </Step>

  <Step title="Grant Performance Schema access">
    Required for query analysis:

    ```sql theme={null}
    GRANT SELECT ON performance_schema.* TO 'cloudthinker_readonly'@'%';
    FLUSH PRIVILEGES;
    ```
  </Step>

  <Step title="Verify Performance Schema is enabled">
    ```sql theme={null}
    SHOW VARIABLES LIKE 'performance_schema';
    -- Should return: ON
    ```

    If disabled, add to `my.cnf` and restart:

    ```ini theme={null}
    [mysqld]
    performance_schema = ON
    ```
  </Step>

  <Step title="Configure network access">
    Ensure CloudThinker can reach your database:

    * Add CloudThinker IPs to your security group or firewall
    * For RDS: enable public access or use VPC peering
  </Step>

  <Step title="Add the connection in CloudThinker">
    Navigate to **Connections → MySQL** and enter:

    * **Host**: your database hostname or IP
    * **Port**: database port (default: `3306`)
    * **Database**: database name
    * **Username**: `cloudthinker_readonly`
    * **Password**: the password you set above
    * **SSL**: enabled (recommended)

    Click **Connect**. CloudThinker shows a **Connected** status once it succeeds.
  </Step>
</Steps>

## Connection details

| Field                  | Description                                   | Default |
| ---------------------- | --------------------------------------------- | ------- |
| **Host**               | Database hostname or IP                       | —       |
| **Port**               | MySQL port                                    | `3306`  |
| **Database**           | Database name                                 | —       |
| **Username**           | Dedicated user, e.g. `cloudthinker_readonly`  | —       |
| **Password**           | User password                                 | —       |
| **SSL**                | Enable SSL/TLS encryption                     | Enabled |
| **Connection timeout** | Seconds to wait for connection                | `10`    |
| **Read timeout**       | Max time to wait for query results in seconds | `30`    |

Connection string format:

```
mysql://cloudthinker_readonly:your-secure-password@your-host:3306/database-name
```

## Required permissions

### Minimum

```sql theme={null}
GRANT SELECT ON your_database.* TO 'cloudthinker_readonly'@'%';
GRANT PROCESS ON *.* TO 'cloudthinker_readonly'@'%';
```

### Recommended (full analysis)

```sql theme={null}
-- All of the above, plus:
GRANT SELECT ON performance_schema.* TO 'cloudthinker_readonly'@'%';
GRANT SELECT ON mysql.* TO 'cloudthinker_readonly'@'%';
GRANT REPLICATION CLIENT ON *.* TO 'cloudthinker_readonly'@'%';
```

<Tip>
  Start with the minimum grants. Add `performance_schema` and `REPLICATION CLIENT` to unlock slow-query data and replication monitoring.
</Tip>

## Agent capabilities

Once connected, Tony can:

| Capability                 | Description                                                      |
| -------------------------- | ---------------------------------------------------------------- |
| **Query analysis**         | Identify slow queries from slow query log and Performance Schema |
| **Index recommendations**  | Find missing indexes, identify redundant indexes                 |
| **Performance metrics**    | Monitor connections, buffer pool, and query cache                |
| **Table statistics**       | Analyze table sizes, fragmentation, and engine status            |
| **Replication monitoring** | Check replica status, lag, and errors                            |

### Verify the connection

```text theme={null}
@tony #report check MySQL connection health and show current active sessions
```

### Example prompts

```text theme={null}
@tony #report analyze slow queries on the production MySQL instance
@tony #recommend find missing indexes for high-frequency queries
@tony #dashboard show buffer pool usage and connection statistics
```

## Troubleshooting

<Accordion title="Connection refused">
  * Verify host and port are correct
  * Check your security group or firewall allows CloudThinker IPs
  * For RDS: ensure "Publicly accessible" is enabled or use VPC peering
  * Confirm MySQL is listening on the correct interface (`bind-address` in `my.cnf`)
</Accordion>

<Accordion title="Access denied">
  * Verify username and password are correct
  * Check the user has the correct host specification (`'user'@'%'` vs `'user'@'localhost'`)
  * Ensure `GRANT` statements were followed by `FLUSH PRIVILEGES`
</Accordion>

<Accordion title="Performance Schema disabled">
  * Check with: `SHOW VARIABLES LIKE 'performance_schema';`
  * Enable in `my.cnf` and restart MySQL
  * For RDS: modify the parameter group and reboot the instance
</Accordion>

<Accordion title="Missing slow query data">
  * Enable slow query log: `SET GLOBAL slow_query_log = 'ON';`
  * Set the threshold: `SET GLOBAL long_query_time = 1;`
  * For RDS: modify the parameter group
</Accordion>

## Security

* **Least privilege** — grant only the permissions the agents need for your use case; start read-only and widen later.
* **Read-only by default** — use read-only credentials unless you want agents to make changes through this connection.
* **Rotate credentials** — rotate keys and tokens on your normal schedule; CloudThinker picks up the new value when you update the connection.
* **Revoke on offboarding** — remove the credential at the provider when you delete a connection or a teammate leaves.

- **SSL required** — always enable SSL to encrypt data in transit.
- **Minimal grants** — grant only `SELECT` to the CloudThinker user; never grant write access.

## Related

<CardGroup cols={2}>
  <Card title="Tony Agent" icon="database" href="/guide/agents/tony">
    Database-focused optimization agent
  </Card>

  <Card title="PostgreSQL Connection" icon="https://mintcdn.com/cloudthinker/aLd-ttc-SCW-aFky/images/icons/postgresql.svg?fit=max&auto=format&n=aLd-ttc-SCW-aFky&q=85&s=8bb2ac033d0a2ccbef51154a76e1e819" href="/guide/connections/postgresql" width="24" height="24" data-path="images/icons/postgresql.svg">
    Similar setup for PostgreSQL databases
  </Card>
</CardGroup>
