#!/bin/bash set -e echo "=== Starting MariaDB ===" # Start MariaDB in background mysqld --user=mysql & MYSQL_PID=$! # Wait for MariaDB to be ready echo "Waiting for MariaDB to be ready..." for i in {1..30}; do if mysqladmin ping -h localhost --silent 2>/dev/null; then echo "MariaDB is ready!" break fi sleep 1 done # Create database if it doesn't exist echo "Creating database if not exists..." mysql -h localhost -u root -e "CREATE DATABASE IF NOT EXISTS \`${DB_NAME:-hospital}\`;" # Create user and grant permissions if DB_PASSWORD is set if [ -n "$DB_PASSWORD" ] && [ "$DB_PASSWORD" != "" ]; then echo "Creating user and setting permissions..." # Check if user exists USER_EXISTS=$(mysql -h localhost -u root -N -e "SELECT COUNT(*) FROM mysql.user WHERE User='${DB_USER:-root}' AND Host='%';") if [ "$USER_EXISTS" = "0" ]; then mysql -h localhost -u root -e "CREATE USER '${DB_USER:-root}'@'%' IDENTIFIED BY '${DB_PASSWORD}';" mysql -h localhost -u root -e "GRANT ALL PRIVILEGES ON ${DB_NAME:-hospital}.* TO '${DB_USER:-root}'@'%';" mysql -h localhost -u root -e "FLUSH PRIVILEGES;" else echo "User already exists, updating password..." mysql -h localhost -u root -e "ALTER USER '${DB_USER:-root}'@'%' IDENTIFIED BY '${DB_PASSWORD}';" mysql -h localhost -u root -e "GRANT ALL PRIVILEGES ON ${DB_NAME:-hospital}.* TO '${DB_USER:-root}'@'%';" mysql -h localhost -u root -e "FLUSH PRIVILEGES;" fi fi echo "=== MariaDB initialization complete ===" # If called with nginx.sh, start nginx if [ "$1" = "nginx.sh" ]; then echo "Starting nginx..." nginx exec "$@" fi # Otherwise wait for the process wait