> For the complete documentation index, see [llms.txt](https://realisticnode.gitbook.io/rnsetups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://realisticnode.gitbook.io/rnsetups/rnkoth/reference/database.md).

# Database (SQLite / MySQL)

SQLite or MySQL / MariaDB, the options, and what each table keeps.

The database keeps the player stats, the history of events, the rewards of players who were offline and the reward box.

## SQLite (default)

```yaml
storage:
  type: sqlite
```

One file, `plugins/rnKoth/data.db`, nothing to set up. Best for a single server.

## MySQL / MariaDB

To share the stats between several servers:

```yaml
storage:
  type: mysql
  mysql:
    host: localhost
    port: 3306
    database: minecraft
    username: root
    password: ''
    table-prefix: 'rnkoth_'
    pool-size: 4
    parameters: 'useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=utf8'
```

| Option               | Default                                                            | What it does                                                                               |
| -------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| `storage.type`       | `sqlite`                                                           | `sqlite` or `mysql` (`mariadb` works too).                                                 |
| `mysql.host`         | `localhost`                                                        | Address of the database server.                                                            |
| `mysql.port`         | `3306`                                                             | Port of the database server.                                                               |
| `mysql.database`     | `minecraft`                                                        | Name of the database. It must exist.                                                       |
| `mysql.username`     | `root`                                                             | User.                                                                                      |
| `mysql.password`     | `''`                                                               | Password.                                                                                  |
| `mysql.table-prefix` | `rnkoth_`                                                          | Start of every table name. Only letters, numbers and `_`. Also used for the SQLite tables. |
| `mysql.pool-size`    | `4`                                                                | How many connections are kept open (at least 2).                                           |
| `mysql.parameters`   | `useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=utf8` | Extra options of the connection.                                                           |

{% hint style="warning" %}
Changing `storage` needs a **server restart**. Check the console: `Running on Paper with 3 KOTH(s), stats in MySQL.` If the database cannot be opened, the console says `Could not open the MySQL database (...)`, and stats and the reward box do not work until it is fixed.
{% endhint %}

MySQL starts empty: the stats of SQLite are not copied to it.

## Tables

The tables are created by themselves. With the default prefix:

| Table            | What it keeps                                                                                                                                                |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `rnkoth_stats`   | One row per player: name, wins, captures, events played, time holding (seconds), kills, holder kills, and the date of the last win.                          |
| `rnkoth_history` | One row per event that ended: KOTH, winner, winning player, date, length and number of players. Used by `/koth history`.                                     |
| `rnkoth_pending` | Rewards waiting for a player: reward lines for players who were offline, and the items of the reward box (`/koth claim`). A row is removed when it is given. |

## What counts for each stat

| Stat          | Counts                                                                                                                                                                                          |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Wins          | +1 for each player of the winning side who stood in the KOTH.                                                                                                                                   |
| Captures      | +1 per event for a player who was inside when their side took control of the KOTH.                                                                                                              |
| Events played | +1 per event for every player who took part.                                                                                                                                                    |
| Time holding  | Seconds the player's side held the KOTH while the player was inside.                                                                                                                            |
| Kills         | Kills during an event where the killer or the victim was inside the area. Kills of a teammate (same side) do not count. A killer outside who is not playing gets the kill, but no event played. |
| Holder kills  | Kills of a holder of another side (at most once a minute for the same two players).                                                                                                             |

Stats are saved at the end of each event. `/koth resetstats <player>` deletes one player's stats, `/koth resetstats all confirm` deletes everyone's. The history and the reward box are kept.

## Files outside the database

| File               | What it keeps                                                                                                                                                                               |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data/running.yml` | The events that are running, so they continue after a restart, and the area where the next event of each KOTH with several areas starts. Saved every few seconds and when the server stops. |
| `data/players.yml` | Players who turned the locator and sidebar off with `/koth hud`.                                                                                                                            |

These files are per server, also with MySQL.

## Backups

* SQLite: stop the server and copy `data.db` (or copy `data.db`, `data.db-wal` and `data.db-shm` together).
* MySQL: use the backup of your database host.
