# MySQL Setup Instructions

Follow these steps to configure MySQL for the Loyal Aid System:

## Step 1: Update .env File

Open your `.env` file and update the database configuration section:

```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=loyal_aid
DB_USERNAME=root
DB_PASSWORD=your_mysql_password_here
```

**Important:** Replace `your_mysql_password_here` with your actual MySQL root password.

## Step 2: Create MySQL Database

### Option A: Using MySQL Command Line

```bash
mysql -u root -p
```

Then run:
```sql
CREATE DATABASE loyal_aid CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
EXIT;
```

### Option B: Using phpMyAdmin

1. Open phpMyAdmin in your browser
2. Click on "New" to create a new database
3. Database name: `loyal_aid`
4. Collation: `utf8mb4_unicode_ci`
5. Click "Create"

### Option C: Using MySQL Workbench

1. Open MySQL Workbench
2. Connect to your MySQL server
3. Right-click in the Schemas panel → Create Schema
4. Name: `loyal_aid`
5. Default Collation: `utf8mb4 - utf8mb4_unicode_ci`
6. Click "Apply"

## Step 3: Test Database Connection

Run this command to test if Laravel can connect to MySQL:

```bash
php artisan db:show
```

If successful, you'll see database information. If not, check your `.env` credentials.

## Step 4: Run Migrations

After confirming the connection, run migrations to create all tables:

```bash
php artisan migrate:fresh --seed
```

**Warning:** This will drop all existing tables and recreate them with seed data.

## Step 5: Clear Configuration Cache

Clear Laravel's configuration cache to ensure it uses the new database settings:

```bash
php artisan config:clear
php artisan cache:clear
```

## Step 6: Verify Setup

Check that tables were created:

```bash
php artisan db:table
```

You should see tables like: `users`, `members`, `roles`, `permissions`, `subscription_plans`, etc.

## Troubleshooting

### Error: "Access denied for user"
- Check your MySQL username and password in `.env`
- Ensure MySQL user has proper permissions

### Error: "Unknown database 'loyal_aid'"
- Make sure you created the database (Step 2)
- Verify the database name in `.env` matches exactly

### Error: "PDOException: could not find driver"
- Install PHP MySQL extension:
  - Windows: Uncomment `extension=pdo_mysql` in `php.ini`
  - Linux: `sudo apt-get install php-mysql` or `sudo yum install php-mysql`
  - macOS: `brew install php-mysql`

### Still Using SQLite?
- Make sure `DB_CONNECTION=mysql` in `.env`
- Run `php artisan config:clear`
- Check that MySQL service is running

## Default Admin Credentials

After seeding, you can login with:
- **Email:** admin@loyalaid.com
- **Password:** password

**Important:** Change this password immediately after first login!

