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

# MySQL

> Declare a MySQL 8.4 resource with explicit database credentials, private networking, and recovery settings.

Towbar's `mysql` preset runs the reviewed MySQL 8.4 image on amd64 or arm64. It
listens on port `3306`, stores data in `/var/lib/mysql`, and verifies readiness
with an authenticated `mysqladmin ping`.

## Declare the resource

```yaml title=".towbar/resources/mysql.resource.yml" highlight={3,14-19,21} theme={"system"}
id: mysql
name: MySQL
type: mysql
container:
  network: application
  networkAlias: mysql
  resources:
    cpus: 1
    memory: 1g
access:
  sshTunnel:
    hostPort: 13306
secrets:
  runtime:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_DATABASE
    - MYSQL_USER
    - MYSQL_PASSWORD
environments:
  production:
    server: 192.0.2.10
```

`MYSQL_ROOT_PASSWORD` is required by Towbar. The other three variables ask the
image to create a named application database and non-root user on the first
initialization.

| Variable              | Purpose                                                  |
| --------------------- | -------------------------------------------------------- |
| `MYSQL_ROOT_PASSWORD` | Root credential used by Towbar health and recovery tools |
| `MYSQL_DATABASE`      | Application database created on an empty data volume     |
| `MYSQL_USER`          | Non-root application user                                |
| `MYSQL_PASSWORD`      | Password for the application user                        |

Use different values for the root and application passwords.

## Connect from an app

Containers on the `application` network can reach `mysql:3306`. Store the
application connection URL or individual fields in the app's runtime secrets.
The optional tunnel exposes server-local port `13306` only through SSH.

## Initialization and credential changes

The initialization variables do not reconcile an existing data volume. Create,
rename, or alter users and databases with SQL first. Update the corresponding
Towbar values only after the database accepts the new credentials.

## Back up and restore

Towbar uses `mysqldump` for a logical export and restores it with the MySQL
client into a fresh target volume. Recovery requires MySQL major version 8.
Continue with [Database backups](/docs/backups) and [Database restores](/docs/restores).
