mindboost-infrastructure/apps/backend/database/healthcheck.sh

34 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
# Credentials from environment variables
MYSQL_USER="${MARIADB_USER:-default}"
MYSQL_PASSWORD="${MARIADB_PASSWORD:-default}"
MYSQL_HOST="127.0.0.1"
ROOT_PASSWORD=$(cat /run/secrets/mariadb_root)
echo "🔑 READ ROOT PASSWORD FROM SECRETS"
# Check if MariaDB is running
if ! mariadb -h "$MYSQL_HOST" -u root -p"$ROOT_PASSWORD" -e "SELECT 1;" &>/dev/null; then
echo "❌ MariaDB is not responding"
exit 1
fi
# Check if a specific user exists
USER_EXISTS=$(mariadb -h "$MYSQL_HOST" -u root -p"$ROOT_PASSWORD" -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${MYSQL_USER}');" | tail -n 1)
if [ "$USER_EXISTS" -ne 1 ]; then
echo "❌ User '${MYSQL_USER}' does not exist"
exit 1
fi
# Check if the user can log in with the provided password
if ! mariadb -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -e "SELECT 1;" &>/dev/null; then
echo "❌ User '${MYSQL_USER}' exists, but authentication failed with the provided password."
exit 1
fi
echo "✅ MariaDB is healthy"
exit 0