> For the complete documentation index, see [llms.txt](https://docs.wproofreader.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wproofreader.com/v6.18.1/deployment/installation/kubernetes/prepare-mysql.md).

# Prepare MySQL

Prepare the MySQL 8.4 server and the Admin-panel database for the WProofreader stack.

The stack keeps its data in two databases on a MySQL 8.4 server:

* `admin_panel_db` for Admin-panel. You create this database on this page.
* `cloud_service` for WProofreader Server. db-manager creates this database during the WProofreader Server installation.

db-manager connects with a MySQL administrator account. This account must have permission to create databases and users.

Before you start, load the settings in your terminal:

```bash
cd ~/wproofreader-k8s
source wsc-stack.env
```

### Select a MySQL option

Select one option. For production, use a MySQL server that your team operates, for example a managed database service.

{% tabs %}
{% tab title="Existing MySQL server" %}
Use this option for a MySQL 8.4 server that you already operate. It can be a managed service, for example Amazon RDS, Azure Database for MySQL, or Cloud SQL. The server can be outside the cluster. The Pods in the `wsc` namespace must be able to connect to it on port 3306.

**1. Add the server to the settings file**

Open `wsc-stack.env` in a text editor. Make these changes:

* Set `MYSQL_ADMIN_USER` to the name of your MySQL administrator account.
* Set `MYSQL_ADMIN_PASSWORD` to the password of this account.
* Add this line and replace `<mysql_host>` with the host name of your MySQL server:

```bash
export MYSQL_HOST='<mysql_host>'
```

Save the file. Then load it again:

```bash
source wsc-stack.env
echo "$MYSQL_HOST"
```

The command shows the MySQL host name that you entered.

**2. Create the Admin-panel database and user**

This command starts a temporary MySQL client Pod. The Pod creates the `admin_panel_db` database and the `admin_panel` user, and then stops.

```bash
kubectl -n wsc run mysql-setup \
  --rm --attach --restart=Never --image=mysql:8.4 \
  --env="MYSQL_PWD=${MYSQL_ADMIN_PASSWORD}" \
  -- mysql --host="${MYSQL_HOST}" --user="${MYSQL_ADMIN_USER}" --execute="
CREATE DATABASE IF NOT EXISTS admin_panel_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS 'admin_panel'@'%' IDENTIFIED BY '${ADMIN_PANEL_DB_PASSWORD}';
ALTER USER 'admin_panel'@'%' IDENTIFIED BY '${ADMIN_PANEL_DB_PASSWORD}';
GRANT ALL PRIVILEGES ON admin_panel_db.* TO 'admin_panel'@'%';"
```

When the command is complete, the last line starts with `pod "mysql-setup" deleted`.
{% endtab %}

{% tab title="mysql-server-helm" %}
Use this option to run MySQL in the cluster with the WebSpellChecker [mysql-server-helm](https://github.com/WebSpellChecker/mysql-server-helm) chart. The chart creates the MySQL server, the `admin_panel_db` database, and the `admin_panel` user.

**1. Create the MySQL Secret**

```bash
kubectl -n wsc create secret generic mysql-credentials \
  --from-literal=mysql-root-password="${MYSQL_ADMIN_PASSWORD}" \
  --from-literal=mysql-password="${ADMIN_PANEL_DB_PASSWORD}"
```

**2. Install MySQL**

```bash
helm upgrade --install mysql ./mysql-server-helm/mysql \
  --namespace wsc \
  --set image.tag=8.4 \
  --set auth.existingSecret=mysql-credentials \
  --set auth.database=admin_panel_db \
  --set auth.username=admin_panel \
  --wait \
  --timeout 10m
```

The command shows `STATUS: deployed` when MySQL is ready.

**3. Add the server to the settings file**

```bash
echo "export MYSQL_HOST='mysql-primary.wsc.svc.cluster.local'" >> wsc-stack.env
source wsc-stack.env
echo "$MYSQL_HOST"
```

The last command shows `mysql-primary.wsc.svc.cluster.local`.

{% hint style="info" %}
The chart keeps the data on a persistent volume. The default size is 8 GiB. To change the size, add `--set primary.persistence.size=<size>` to the install command, for example `20Gi`.
{% endhint %}
{% endtab %}

{% tab title="MySQL Operator" %}
Use this option if your team runs MySQL with the [MySQL Operator for Kubernetes](https://dev.mysql.com/doc/mysql-operator/en/). To install the operator, you must have permission to add cluster-wide resources.

1. Install the operator and create an InnoDB Cluster with MySQL 8.4. Follow the [MySQL Operator documentation](https://dev.mysql.com/doc/mysql-operator/en/).
2. Use the host name of the InnoDB Cluster Service as the MySQL host.
3. Do the steps in the **Existing MySQL server** tab.
   {% endtab %}
   {% endtabs %}

### Check the connection

This command connects to MySQL as the `admin_panel` user:

```bash
kubectl -n wsc run mysql-check \
  --rm --attach --restart=Never --image=mysql:8.4 \
  --env="MYSQL_PWD=${ADMIN_PANEL_DB_PASSWORD}" \
  -- mysql --host="${MYSQL_HOST}" --user=admin_panel \
  --execute='SELECT DATABASE(), CURRENT_USER(), VERSION();' admin_panel_db
```

The output shows `admin_panel_db`, `admin_panel@%`, and a MySQL version that starts with `8.4`.

{% hint style="info" %}
Before the output, `kubectl` can show messages such as `If you don't see a command prompt, try pressing enter`. You can ignore them.
{% endhint %}

If the output shows `Access denied`, make sure that the password in `wsc-stack.env` is the password of the `admin_panel` user.

### Next step

[Install WProofreader Server](/v6.18.1/deployment/installation/kubernetes/install-wproofreader-server.md)
