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

# Connect MySQL

> Connect MySQL to QueryComment with performance_schema and a read-only monitoring user.

Connect MySQL by creating a monitoring user, confirming that `performance_schema` captures statement digests, and running the QueryComment collector.

## Prepare MySQL

<Steps>
  <Step title="Create the monitoring user">
    Connect as a MySQL user with grant privileges and run:

    ```sql theme={null}
    CREATE USER 'querycomment'@'%' IDENTIFIED BY 'replace-with-strong-password';
    GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO 'querycomment'@'%';
    FLUSH PRIVILEGES;
    ```

    `PROCESS` lets the collector inspect thread state and InnoDB status. `REPLICATION CLIENT` lets it read replication status when replicas are present.

    <Note>
      Replace `'%'` with the collector host or subnet when you want to restrict where the monitoring user can connect from.
    </Note>
  </Step>

  <Step title="Enable statement digests">
    QueryComment uses `performance_schema.events_statements_summary_by_digest` for normalized query statistics.

    Confirm that `performance_schema` is enabled:

    ```sql theme={null}
    SHOW VARIABLES LIKE 'performance_schema';
    ```

    Confirm that statement digests are being collected:

    ```sql theme={null}
    SELECT NAME, ENABLED
    FROM performance_schema.setup_consumers
    WHERE NAME = 'statements_digest';
    ```

    If the consumer is disabled, enable it as an administrator:

    ```sql theme={null}
    UPDATE performance_schema.setup_consumers
    SET ENABLED = 'YES'
    WHERE NAME = 'statements_digest';
    ```
  </Step>

  <Step title="Verify access">
    Confirm the monitoring user has the required grants and that digest rows exist:

    ```sql theme={null}
    SHOW GRANTS FOR 'querycomment'@'%';

    SELECT COUNT(*)
    FROM performance_schema.events_statements_summary_by_digest;
    ```

    If the digest table is empty, run a representative workload against the database and check again.
  </Step>
</Steps>

## Collector connection values

Use the [QueryComment collector](https://github.com/querycomment/collector/releases/tag/v0.1.2) with the bundled MySQL receiver. The collector is available as the [`ghcr.io/querycomment/collector:0.1.2`](https://github.com/querycomment/collector/pkgs/container/collector) Docker image or as a release package for your operating system.

Set these environment variables before starting the collector:

| Variable                | Description                                                                                            |
| ----------------------- | ------------------------------------------------------------------------------------------------------ |
| `MYSQL_HOST`            | MySQL host name or IP address.                                                                         |
| `MYSQL_PORT`            | MySQL port. Defaults to `3306` if omitted.                                                             |
| `MYSQL_USER`            | Monitoring user name.                                                                                  |
| `MYSQL_PASSWORD`        | Monitoring user password.                                                                              |
| `MYSQL_DATABASE`        | Database to connect to.                                                                                |
| `MYSQL_TLS_INSECURE`    | Optional. Set to `true` only when the connection should skip TLS. Defaults to `false`.                 |
| `MYSQL_TLS_SKIP_VERIFY` | Optional. Set to `true` only when TLS certificate verification should be skipped. Defaults to `false`. |
| `QC_ENDPOINT`           | Optional QueryComment ingest endpoint. Defaults to `ingest.querycomment.com:443`.                      |
| `QC_INGEST_TOKEN`       | QueryComment ingest token from your dashboard.                                                         |
| `QC_DATABASE_NAME`      | Database or cluster name to attach to exported metrics.                                                |
| `OTEL_ENVIRONMENT`      | Deployment environment. Defaults to `production` if omitted.                                           |

Example `.env` file:

```bash theme={null}
MYSQL_HOST=db.example.com
MYSQL_PORT=3306
MYSQL_USER=querycomment
MYSQL_PASSWORD=replace-with-strong-password
MYSQL_DATABASE=app

QC_INGEST_TOKEN=replace-with-querycomment-ingest-token
QC_DATABASE_NAME=production-mysql
OTEL_ENVIRONMENT=production
```

Run the collector with Docker:

```bash theme={null}
docker run --rm \
  --env-file .env \
  ghcr.io/querycomment/collector:0.1.2 \
  --config /etc/querycommentcol/mysql.yaml
```

Or choose the release package for your operating system and architecture. This example uses Linux amd64:

```bash theme={null}
curl -L -o querycommentcol_0.1.2_linux_amd64.tar.gz \
  https://github.com/querycomment/collector/releases/download/v0.1.2/querycommentcol_0.1.2_linux_amd64.tar.gz
tar -xzf querycommentcol_0.1.2_linux_amd64.tar.gz
cd querycommentcol_0.1.2_linux_amd64
set -a
. ../.env
set +a
./querycommentcol --config config/mysql.yaml
```
