# MySQL

import { Tabs, TabsContent, TabsList, TabsTrigger } from "zudoku/ui/Tabs"

{/* vale off */}
{/* THIS FILE WAS AUTOGENERATED FROM THE PUBLIC EXAMPLE REPOSITORY. DO NOT EDIT THIS FILE DIRECTLY. */}


This guide shows you how to use [MySQL](https://www.mysql.com), one of the most popular open source relational databases.
To run it, follow these steps:

1. Install the CLI.
   Use the [unikraft CLI](/cli/unikraft).
   You need a [BuildKit](https://github.com/moby/buildkit) builder. The easiest way to get one is via [Docker](https://docs.docker.com/engine/install/).
   Alternatively, you can also directly set up and use BuildKit, see the [quick start](https://github.com/moby/buildkit#quick-start).

	:::note
	The unikraft CLI is the current standard, while kraft is the legacy version.
	Choose one of the CLIs below and only run the commands associated with it for the rest of this guide.
	:::

2. Clone the [`examples` repository](https://github.com/unikraft-cloud/examples) and `cd` into the `examples/mysql/` directory:

```bash
git clone https://github.com/unikraft-cloud/examples
cd examples/mysql
```

Make sure to log into Unikraft Cloud and pick a [metro](/platform/metros) close to you.
This guide uses `fra` (Frankfurt, 🇩🇪):

<CodeTabs syncKey="cli">

```bash title="unikraft"
unikraft login
```

```bash title="kraft"
# Set Unikraft Cloud access token
export UKC_TOKEN=token
# Set metro to Frankfurt, DE
export UKC_METRO=fra
```

</CodeTabs>

When done, invoke the following command to deploy this app on Unikraft Cloud:

<CodeTabs syncKey="cli">

```bash title="unikraft"
unikraft build . --output <my-org>/mysql:latest
unikraft run --scale-to-zero policy=idle,cooldown-time=1000,stateful=true --metro fra -p 3306:3306/tls -m 1G -e MYSQL_ROOT_PASSWORD="unikraft" --image <my-org>/mysql:latest
```

```bash title="kraft"
kraft cloud deploy --scale-to-zero idle --scale-to-zero-cooldown 1s --scale-to-zero-stateful --env MYSQL_ROOT_PASSWORD="unikraft" -p 3306:3306/tls -M 1Gi .
```

</CodeTabs>

The output shows the instance address and other details:

```ansi
metro:        fra
name:         mysql-b3mlh
uuid:         19479d80-0995-4ed2-b08e-86b29eda3f3
state:        starting
image:        <my-org>/mysql
runtime:
  env:
    MYSQL_ROOT_PASSWORD: *
resources:
  memory:                1GiB
  vcpus:                 1
service:
  name:                  purple-wood-hqb663gu
  uuid:                  d2bcd1c0-c980-4d5b-9777-2bec2b0d0645
  domains:
  - fqdn:                purple-wood-hqb663gu.fra.unikraft.app
networks:
- uuid:                  3e3a6cd5-ca66-41ec-8c28-5280b049f732
  private-ip:            10.0.14.181
  mac:                   12:b0:0a:00:0e:b5
timestamps:
  created:               just now
scale-to-zero:
  enabled:               true
  policy:                idle
  stateful:              true
  cooldown-time:         1s
```

or

```ansi title="kraft"
[38;5;245m[[0m[92m●[0m[38;5;245m][0m Deployed successfully!
 [38;5;245m│[0m
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mname[0m: mysql-b3mlh
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245muuid[0m: 19479d80-0995-4ed2-b08e-86b29eda3f3
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mmetro[0m: https://api.fra.unikraft.cloud/v1
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mstate[0m: [92mstarting[0m
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mdomain[0m: https://purple-wood-hqb663gu.fra.unikraft.app
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mimage[0m: oci://unikraft.io/<my-org>/mysql@sha256:f51ecc121c9ca34abb88a2bc6a69765501304f7893f7e85af15fbec3dc86e2bd
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mmemory[0m: 1024 MiB
 [38;5;245m├[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mservice[0m: purple-wood-hqb663gu
 [38;5;245m├[0m[38;5;245m─[0m [38;5;245mprivate fqdn[0m: mysql-b3mlh.internal
 [38;5;245m└[0m[38;5;245m─[0m[38;5;245m─[0m[38;5;245m─[0m [38;5;245mprivate ip[0m: 10.0.3.3
```

In this case, the instance name is `mysql-b3mlh` which is different for each run.

To test the deployment, first forward the port with the `socat` command.

```bash
socat TCP-LISTEN:3306,reuseaddr,fork OPENSSL:purple-wood-hqb663gu.fra.unikraft.app:3306,verify=0
```

You can now, on a separate console, use the `mysql` command line tool to test that the set up works:

```bash
mysql -h 127.0.0.1 --ssl-mode=DISABLED -u root -punikraft mysql <<< "select count(*) from user"
```

Or use the `mariadb` client command line tool:

```bash
mariadb -h 127.0.0.1 --ssl=OFF -u root -punikraft mysql <<< "select count(*) from user"
```

You should see output such as:

```ansi
count(*)
6
```

To disconnect, kill the `socat` command using `Ctrl+c`.

:::note
This guide uses `socat` for port forwarding only when a service doesn't support TLS and isn't HTTP-based (TLS/SNI determines the correct instance to send traffic to).
Also note that port forwarding isn't needed when connecting via an instance's private IP/FQDN.
For example, when a MySQL instance serves as a database server to another instance that acts as a frontend and which **does** support TLS.
:::

You can list information about the instance by running:

<CodeTabs syncKey="cli">

```bash title="unikraft"
unikraft instances list
```

```bash title="kraft"
kraft cloud instance list
```

</CodeTabs>

<CodeTabs syncKey="cli">

```ansi title="unikraft"
[1mMETRO[22m  [1mNAME[22m         [1mSTATE[22m    [1mIMAGE[22m           [1mARGS[22m  [1mMEMORY[22m   [1mVCPUS[22m  [1mFQDN[22m                                    [1mCREATED[22m
fra    mysql-b3mlh  [38;2;0;188;125mrunning[0m  <my-org>/mysql        1024MiB  1      purple-wood-hqb663gu.fra.unikraft.app  2 minutes ago
```

```ansi title="kraft"
[0;1;39mNAME[0m         [0;1;39mFQDN[0m                                   [0;1;39mSTATE[0m    [0;1;39mSTATUS[0m         [0;1;39mIMAGE[0m                                        [0;1;39mMEMORY[0m    [0;1;39mVCPUS[0m  [0;1;39mARGS[0m  [0;1;39mBOOT TIME[0m
mysql-b3mlh  purple-wood-hqb663gu.fra.unikraft.app  [0;32mrunning[0m  5 minutes ago  oci://unikraft.io/<my-org>/mysql@sha256:...  1024 MiB  1            11.13 ms
```

</CodeTabs>

When done, you can remove the instance:

<CodeTabs syncKey="cli">

```bash title="unikraft"
unikraft instance remove mysql-b3mlh
```

```bash title="kraft"
kraft cloud instance remove mysql-b3mlh
```

</CodeTabs>

:::tip
This example uses the [`idle` scale-to-zero policy](/api/platform/v1/instances#scaletozero_policy) by default.
:::

## Using volumes

You can use [volumes](/platform/volumes) for data persistence for your MySQL instance.
For that you would first create a volume:

<CodeTabs syncKey="cli">

```bash title="unikraft"
unikraft volume create --set metro=fra --set name=mysql-store --set size=512M
```

```bash title="kraft"
kraft cloud volume create --name mysql-store --size 512Mi
```

</CodeTabs>

Then start the MySQL instance and mount that volume:

```bash
unikraft build . --output <my-org>/mysql:latest
unikraft run --scale-to-zero policy=idle,cooldown-time=1000,stateful=true --metro fra -p 3306:3306/tls -m 1G -e MYSQL_ROOT_PASSWORD="unikraft" --volume mysql-store:/var/lib --image <my-org>/mysql:latest
```

or

```bash title="kraft"
kraft cloud deploy --scale-to-zero idle --scale-to-zero-stateful --scale-to-zero-cooldown 1s -M 1Gi -p 3306:3306/tls --env MYSQL_ROOT_PASSWORD="unikraft" --volume mysql-store:/var/lib .
```


## Learn more

Use the `--help` option for detailed information on using Unikraft Cloud:

<CodeTabs syncKey="cli">

```bash title="unikraft"
unikraft --help
```

```bash title="kraft"
kraft cloud --help
```

</CodeTabs>

Or visit the [CLI Reference](/cli/unikraft) or the [legacy CLI Reference](/cli/kraft/overview).
{/* vale on */}
