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

# MySQL

> Run MySQL on an Archil disk for elastic, S3-backed storage

[MySQL](https://www.mysql.com/) is an open-source relational database management system (RDBMS) that's
popular for its performance, ease of use, and extensive ecosystem.

This guide walks you through running MySQL on an Archil disk. MySQL doesn't need to know
anything about Archil or S3 -- it just reads and writes files to its data directory as usual. By pointing
that data directory at an Archil mount, you get:

* **Elastic storage**: no need to provision or resize disks as your database grows
* **S3 durability**: all data is automatically synchronized to S3 in the background
* **Pay for what you use**: you're only billed for active data, not provisioned capacity
* **Portability**: unmount from one server and mount on another to move your database

## Create an Archil disk

First, follow the Archil [Getting Started Guide](/getting-started/quickstart) to create an Archil disk.

## Install MySQL

The exact instructions for installing MySQL will vary depending on your operating system. For example, on Amazon Linux 2023,
you can install MySQL with the following commands:

```bash theme={null}
# Download the MySQL community Yum repository
sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
sudo dnf install -y mysql80-community-release-el9-1.noarch.rpm

# Import the MySQL GPG key
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023

# Install MySQL
sudo dnf install -y mysql-community-server mysql-community-client
```

Mount your Archil disk to an easy to access directory.

```bash theme={null}
# Mount Archil disk
sudo mkdir -p /mnt/archil
export ARCHIL_MOUNT_TOKEN="<token>"
sudo --preserve-env=ARCHIL_MOUNT_TOKEN archil mount <disk-name> /mnt/archil --region aws-us-east-1

# Create the directory for MySQL data
sudo mkdir -p /mnt/archil/mysql
sudo chown -R mysql:mysql /mnt/archil/mysql
```

Set the MySQL data directory to the Archil disk. Open the file `/etc/my.cnf` in your favorite editor.
Under the `[mysqld]` section, identify the line that starts with `datadir=` and edit the path to look
like the following:

```bash theme={null}
datadir=/mnt/archil/mysql
```

Initialize the MySQL database in our data directory, and start the MySQL service.

```bash theme={null}
sudo mysqld --initialize --user=mysql --datadir=/mnt/archil/mysql
sudo systemctl start mysqld
```

Note the temporary password that was generated for the root user by looking through the initialization log.

```bash theme={null}
sudo grep 'temporary password' /var/log/mysqld.log
```

Change your root password to something more secure.

```bash theme={null}
mysql --connect-expired-password -u root -p -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOUR_NEW_PASSWORD';"
```

<Note>
  By default, the MySQL password policy requires that you set a password of at least 8 characters that contains at least
  one uppercase letter, one lowercase letter, one number, and one special character.
</Note>

Validate that MySQL is running on Archil by executing the following command, logging in with your new password.

```bash theme={null}
mysql -u root -p -e "SELECT @@datadir;"
```

This should output the data directory that we previously set up, `/mnt/archil/mysql`.

## (Optional) Run a benchmark to validate performance

First, we will download and compile the `sysbench` tool.

```bash theme={null}
# Install development dependencies for sysbench
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y mysql-devel openssl-devel glibc-static libstdc++-static

# Download and compile sysbench
git clone https://github.com/akopytov/sysbench.git
cd sysbench
./autogen.sh
./configure
make -j
sudo make install
```

Let's create a database and write test data to it.

```bash theme={null}
mysql -u root -p -e "CREATE DATABASE sbtest;"

sysbench /usr/local/share/sysbench/oltp_read_write.lua \
  --mysql-host=127.0.0.1 \
  --mysql-user=root \
  --mysql-password='YourPassword' \
  --mysql-db=sbtest \
  --tables=4 --table-size=100000 prepare
```

Finally, run the test:

```bash theme={null}
# Run the sysbench test
sysbench /usr/local/share/sysbench/oltp_read_write.lua \
  --mysql-host=127.0.0.1 \
  --mysql-user=root \
  --mysql-password='YourPassword' \
  --mysql-db=sbtest \
  --threads=4 --time=30 run

# Cleanup the sysbench test
sysbench /usr/local/share/sysbench/oltp_read_write.lua \
  --mysql-host=127.0.0.1 \
  --mysql-user=root \
  --mysql-password='YourPassword' \
  --mysql-db=sbtest cleanup
```
