add healthcheck and init user script for database

rapp/pick-what-you-like
Robert Rapp 2025-02-28 18:08:23 +01:00
parent 02f20a277c
commit 1d04638be8
2 changed files with 107 additions and 0 deletions

33
apps/database/healthcheck.sh Executable file
View File

@ -0,0 +1,33 @@
#!/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

View File

@ -0,0 +1,74 @@
#!/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 <<EOSQL
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MARIADB_ROOT_PASSWORD}';
ALTER USER 'root'@'%' IDENTIFIED BY '${MARIADB_ROOT_PASSWORD}';
FLUSH PRIVILEGES;
EOSQL
echo "✅ Root password reset successfully!"
# Restart MariaDB in normal mode
echo "🔄 Restarting MariaDB in production mode..."
service mysql stop || pkill mysqld
sleep 3
mysqld_safe &
sleep 5
else
echo "✅ Root password is correct."
fi
# Check if the database exists
DB_EXISTS=$(mysql -u root -p"$MARIADB_ROOT_PASSWORD" -e "SHOW DATABASES LIKE '${MARIADB_DATABASE}';" | grep "${MARIADB_DATABASE}" > /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" <<EOSQL
CREATE USER '${MARIADB_USER}'@'%' IDENTIFIED BY '${MARIADB_PASSWORD}';
GRANT ALL PRIVILEGES ON ${MARIADB_DATABASE}.* TO '${MARIADB_USER}'@'%';
FLUSH PRIVILEGES;
EOSQL
echo "✅ User '${MARIADB_USER}' created and granted access to '${MARIADB_DATABASE}'!"
else
echo "✅ User '${MARIADB_USER}' already exists. Ensuring correct password."
mysql -u root -p"$MARIADB_ROOT_PASSWORD" -e "ALTER USER '${MARIADB_USER}'@'%' IDENTIFIED BY '${MARIADB_PASSWORD}'; FLUSH PRIVILEGES;"
echo "✅ Password for '${MARIADB_USER}' updated!"
fi
echo "🎉 MariaDB initialization complete!"