# Managed MySQL

Run MySQL Community Server on dedicated CPUs with local NVMe, without a server to look after. We take care of provisioning, failover and patch updates, and every connection is encrypted with TLS.

## Create a MySQL database

1. Go to **Databases** and click **Create database**.
2. Select **MySQL**.
3. Enter a **Name**. It must be unique in your organization.
4. Select the **Data center** your servers run in. See [Regions](https://betterstack.com/docs/cloud/servers/regions/#choose-a-region).
5. Select a **Plan**.
6. Under **Nodes**, select **Primary + 1 replica**, so a replica takes over if the primary fails.
7. Turn on **Allow public access** if clients outside your private networks connect to the database.
8. Click **Create database**.

The database is ready when its status reads **Running**.

![Create database form with MySQL selected](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/2fe458f5-4b15-493e-8292-2ec227230800/md2x =2180x2156)

## Plans

| Plan | vCPU and RAM | Storage | Connections |
|------|--------------|---------|-------------|
| MY1 | 4 vCPU, 4 GB | 64 GB | 400 |
| MY2 | 8 vCPU, 8 GB | 128 GB | 800 |
| MY3 | 8 vCPU, 32 GB | 192 GB | 3,200 |
| MY4 | 16 vCPU, 64 GB | 256 GB | 6,400 |
| MY5 | 32 vCPU, 120 GB | 512 GB | 12,000 |
| MY6 | 48 vCPU, 160 GB | 768 GB | 16,000 |
| MY7 | 64 vCPU, 192 GB | 1,024 GB | 19,200 |
| MY8 | 128 vCPU, 480 GB | 1,536 GB | 48,000 |

Every node gets the full plan and bills at the plan's price, so a primary with one replica costs twice the plan. Storage is what your data and binary logs can use. We keep extra disk for temporary tables and online schema changes. Traffic over private networks, replication included, is free. See [Pricing philosophy](https://betterstack.com/docs/cloud/pricing/#network-and-ip-address-prices) for internet egress.

## Connect to your database

Open the database and find the hostname, the `defaultdb` database, the `admin` user and its password under **Connection**. **Copy database URL** gives you a ready-made connection string for your app. To connect from a terminal, set the hostname as `DB_HOST`:

```bash
[label Connect with the mysql client]
mysql --host "$DB_HOST" --user admin --password --ssl-mode=REQUIRED defaultdb
```

The database listens on port **3306**, and every connection must use TLS. A new database is reachable only from the [private networks](https://betterstack.com/docs/cloud/networks/private-networks/) attached to it. The hostname always points at the current primary, so clients follow a failover by reconnecting.

**Allow public access** opens port 3306 on the primary's public hostname. Get it with `betterstack mysql credentials <name> --availability-zone us-west-2a`, in `public_uri`. Replicas stay private. Restrict who can connect with a [firewall](https://betterstack.com/docs/cloud/networks/firewalls/). A firewall filters only the public address, not traffic from private networks.

[info]
#### Connection hangs from outside Cloud?
The hostname resolves to a private address, so it works only from your private networks. From the internet, turn on public access and connect to the public hostname.
[/info]

## Rotate credentials

To replace the `admin` password:

1. Click **Rotate credentials** on the database page.
2. Click **Rotate credentials** to confirm.

The previous password keeps working for 24 hours, and open connections stay up, so roll your clients within that window.

## Users and databases

`admin` owns your data. Create databases and users, and grant them access, with plain SQL:

```sql
[label Create a database and a user]
CREATE DATABASE app;
CREATE USER 'app'@'%' IDENTIFIED BY 'use-a-strong-password';
GRANT ALL PRIVILEGES ON app.* TO 'app'@'%';
```

Triggers, views, stored procedures and online schema changes all work. The server itself stays with us, so a few things work differently:

- `SET GLOBAL` and `SET PERSIST` aren't allowed.
- `LOAD DATA INFILE` and `LOAD DATA LOCAL INFILE` are off. Import with `mysql < dump.sql`, and export with `mysqldump --single-transaction`.
- `SHOW GRANTS` works only for the account you're connected as. To check another user's grants, connect as that user.
- User names starting with `betterstack_` belong to the managed service.

Tables use InnoDB. MyISAM and other storage engines that can't replicate safely are turned off. Give every table a primary key. Tables without one stall replicas once they grow.

## Replicas and failover

Replicas run in the same data center and copy the primary asynchronously. They serve read-only traffic.

With at least one replica, failover is automatic. When the primary is unreachable for about 90 seconds, the replica with the most applied transactions becomes the primary and the hostname moves to it, usually within 3 minutes.

Because replication is asynchronous, writes that hadn't reached that replica yet are lost. A database without replicas has no failover and is down until its node recovers.

To switch on purpose, for a disaster recovery drill or a planned move:

1. Click **Fail over** on the database page.
2. Click **Fail over** to confirm.

No committed write is lost, but writes are refused from the moment the primary freezes until clients reconnect to the new one. If the replica doesn't catch up within 60 seconds, the switch is called off and the original primary takes writes again.

[warning]
#### Seeing error 1290 after a failover?
The old primary stays reachable and rejects writes with error 1290, `--super-read-only`. Treat it as a signal to reconnect and re-resolve the hostname, cap the lifetime of pooled connections, and don't cache DNS beyond its 60-second TTL.
[/warning]

## Snapshots

Click **Take a snapshot** on the database page. The database keeps serving while the snapshot is taken, and a restore comes up transactionally consistent. Snapshots outlive the database. To restore one, see [Database snapshots](https://betterstack.com/docs/cloud/databases/snapshots/#roll-a-database-back).

## Rescale and disk usage

To change the plan:

1. Click **Rescale** on the database page.
2. Select a **Plan** and click **Rescale database**.

With replicas, nodes resize one at a time and clients see a single reconnect. A database without replicas is unavailable for a few minutes. You can move to a smaller plan only if your data fits within 80% of its storage.

When storage fills up, the database protects itself:

| Storage used | What happens |
|--------------|--------------|
| 80% and 90% | We send a disk usage warning to your [webhooks](https://betterstack.com/docs/cloud/integrations/webhooks/). |
| 97% | The database turns **Degraded** and read-only. |

Act on the warnings. Once the database is read-only, `DELETE`, `TRUNCATE` and `DROP TABLE` fail too, and rescaling to a bigger plan is the only way out. Writes resume on their own once usage is below 90% of the new plan's storage.

Binary logs count toward storage and are kept for 3 days. A bulk load uses about twice the size of the rows it adds, and deleting the rows afterwards frees only the table's share until the logs expire. Leave room for that when you pick a plan.

## Updates

We apply MySQL patch releases automatically. Replicas restart first, then the primary hands over to a replica, so clients see a single reconnect. A database without replicas restarts briefly.

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
