#!/bin/bash echo "🔄 Running MariaDB initialization script..." # Wait until MariaDB is ready until mysqladmin ping -h localhost --silent; do sleep 2 done echo "✅ MariaDB is ready. Checking root credentials..." # Try logging in with the root password if ! mysql -u root -p"$MARIADB_ROOT_PASSWORD" -e "SELECT 1;" &>/dev/null; then echo "❌ ERROR: Root password in .env does not match the database!" echo "🔄 Attempting to reset the root password..." # Stop MariaDB safely echo "⚠️ Stopping MariaDB..." service mysql stop || pkill mysqld sleep 5 # Start MariaDB in recovery mode echo "🚀 Starting MariaDB in recovery mode..." mysqld_safe --skip-grant-tables --skip-networking & sleep 5 # Reset root password echo "🔐 Resetting root password..." mysql -u root < /dev/null; echo "$?") if [ "$DB_EXISTS" -ne 0 ]; then echo "⚠️ Database '${MARIADB_DATABASE}' does not exist. Creating it now..." mysql -u root -p"$MARIADB_ROOT_PASSWORD" -e "CREATE DATABASE ${MARIADB_DATABASE};" echo "✅ Database '${MARIADB_DATABASE}' created!" else echo "✅ Database '${MARIADB_DATABASE}' already exists." fi # Ensure the database user exists and has the correct password USER_EXISTS=$(mysql -u root -p"$MARIADB_ROOT_PASSWORD" -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${MARIADB_USER}');" | tail -n 1) if [ "$USER_EXISTS" -eq 0 ]; then echo "⚠️ User '${MARIADB_USER}' does not exist. Creating it now..." mysql -u root -p"$MARIADB_ROOT_PASSWORD" <