#!/bin/bash set -e echo "==========================================" echo "Dish Planner - Starting Initialization" echo "==========================================" # ============================================ # MySQL Initialization # ============================================ echo "[1/6] Initializing MySQL..." # Check if MySQL data directory is empty (first run) if [ ! -d "/var/lib/mysql/mysql" ]; then echo " → First run detected, initializing MySQL data directory..." mysqld --initialize-insecure --user=mysql --datadir=/var/lib/mysql fi # Start MySQL temporarily in background for setup echo " → Starting MySQL..." mysqld --user=mysql --datadir=/var/lib/mysql & MYSQL_PID=$! # Wait for MySQL to be ready echo " → Waiting for MySQL to be ready..." for i in {1..30}; do if mysqladmin ping -h localhost --silent; then echo " → MySQL is ready!" break fi echo " → Waiting... ($i/30)" sleep 2 done # ============================================ # Database Setup # ============================================ echo "[2/6] Setting up database..." # Set default values for database credentials DB_DATABASE=${DB_DATABASE:-dishplanner} DB_USERNAME=${DB_USERNAME:-dishuser} # Check if this is first run by looking for credentials file CREDS_FILE="/var/www/backend/storage/.db_credentials" if [ -f "$CREDS_FILE" ]; then # Not first run - load existing credentials echo " → Loading existing database credentials..." source "$CREDS_FILE" else # First run - generate new credentials and save them echo " → First run detected, generating credentials..." DB_PASSWORD=${DB_PASSWORD:-$(openssl rand -base64 32)} MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-$(openssl rand -base64 32)} # Save credentials for future restarts cat > "$CREDS_FILE" <