# Royal Audio MySQL Setup

## 1. Create the database and user

Run these commands in phpMyAdmin, MySQL Workbench, cPanel MySQL, or the MySQL command line. Replace the password before running them.

```sql
CREATE DATABASE royal_audio CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'royal_audio_user'@'localhost' IDENTIFIED BY 'CHANGE_THIS_STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON royal_audio.* TO 'royal_audio_user'@'localhost';
FLUSH PRIVILEGES;
```

Some managed hosts create the database and user through their control panel. In that case, use the names and credentials supplied by the host.

## 2. Create the environment file

Copy `.env.example` to `.env` and enter the real MySQL values.

```bash
cp .env.example .env
```

Required settings:

```text
DATABASE_DRIVER=mysql
DATABASE_REQUIRED=true
DB_HOST=localhost
DB_PORT=3306
DB_USER=royal_audio_user
DB_PASSWORD=your-real-password
DB_NAME=royal_audio
```

## 3. Install and verify

```bash
npm config set registry https://registry.npmjs.org/
npm install
npm run db:check
npm start
```

The application automatically creates the required tables from `database/schema.sql`.

## 4. Existing local data

On the first MySQL connection, the application checks whether the database is empty. If it is empty, it imports the current local users, books, chapters, progress, payments, settings, analytics events, and password-reset records.

After that, MySQL becomes the persistent source loaded during application startup. Local JSON files remain as recovery snapshots for this transition build.

## 5. Backup

Example command:

```bash
mysqldump -u royal_audio_user -p royal_audio > royal_audio_backup.sql
```

Schedule daily database backups with your host before accepting live payments.
