# MySQL/MariaDB Database

Source: https://docs.quake.ai/resources/iac-templates/mysql-database
Markdown: https://docs.quake.ai/resources/iac-templates/mysql-database.md

---

# MySQL/MariaDB database

This pattern composes Compute, Network, and Block Storage.

## What this template does

Provisions a standalone self-managed MySQL/MariaDB server with private networking, independent
of any application tier:

- Dedicated compute instance sized for database workloads
- Block storage volume formatted and mounted at `/var/lib/mysql` (on `/dev/sdb`) for the database
  data directory
- Cloud-init installs the database engine (`db_engine_package`, MariaDB by default), creates the
  `db_name` database and `db_user` role, and binds the server to all interfaces
- Scheduled `mysqldump --all-databases` backup script on `backup_schedule` (writes to
  `/var/lib/mysql/backups`)
- Private network: no public access to the database port
- Security group allowing MySQL/MariaDB traffic only from `allowed_cidrs`

Set `db_password` to a strong value when you apply; the template ships no default password.

## Parameters

| Parameter | Description | Default |
| --- | --- | --- |
| `key_name` | SSH keypair name (must already exist in your project) | required |
| `db_flavor` | Database instance size | `m2a.large` |
| `volume_size` | Data volume in GB | `50` |
| `db_engine_package` | APT package for the database engine | `mariadb-server` |
| `backup_schedule` | Cron expression for backups | `0 2 * * *` |
| `allowed_cidrs` | CIDRs allowed to connect | No default |
| `db_name` | Application database created on first boot | `appdb` |
| `db_user` | Application database user created on first boot | `appuser` |
| `db_password` | Password for the database user (required, no default) | _none_ |
| `image_name` | Operating system image | `Ubuntu-24.04` |
| `external_network` | External network for router gateway | `PublicStatic` |
| `private_cidr` | Address range for the private subnet | `192.168.50.0/24` |
| `instance_name` | Compute instance display name | `mysql` |

## When to use this pattern

Run MySQL or MariaDB on a single compute instance with a data volume and private network
access, independent of an application tier. Choose this template over
[WordPress + MySQL on Compute](/resources/iac-templates/wordpress-mysql) when the workload needs
a MySQL-compatible database without the bundled WordPress application, and over
[Self-managed PostgreSQL](/resources/iac-templates/self-managed-postgres) when the workload
speaks the MySQL wire protocol rather than PostgreSQL's. Choose [Full-Stack Application](/resources/iac-templates/full-stack-app)
when the database sits behind separate web and app tiers.

## Estimated cost

<PricingCompanion
  components={[
    { kind: "template", slug: "mysql-database", required: true },
  ]}
/>

## Template source

<TemplateSource slug="mysql-database" />

<TemplateVariations base="mysql-database" />

<TemplateResourceMap template="mysql-database" format="opentofu" />

## Customize this pattern

- [Customize a template's image and flavor](/docs/automation/how-to/customize-template-image-flavor)
- [Add a block volume to a template](/docs/automation/how-to/add-volume-to-template)
- [Parameterize a template with a tfvars file](/docs/automation/how-to/parameterize-template-tfvars)

## See also

- [Deploy the MySQL/MariaDB database template with OpenTofu](/resources/deployments/deploy-mysql-database-template)
- [WordPress + MySQL template](/resources/iac-templates/wordpress-mysql)
- [Self-managed PostgreSQL](/resources/iac-templates/self-managed-postgres)
- [Object Storage overview](/docs/object)
