#!/bin/bash # Define directories MYSQL_DATA_DIR="/var/lib/mysql" MYSQL_RUN_DIR="/var/run/mysqld" echo ">>> Setting up MySQL environment..." # 1. Prepare Runtime Directory (Socket) # This is critical because /var/run is often cleared on restart mkdir -p "$MYSQL_RUN_DIR" chown -R mysql:mysql "$MYSQL_RUN_DIR" chmod 777 "$MYSQL_RUN_DIR" # 2. Check and Initialize Data Directory # If the 'mysql' database doesn't exist, we must initialize it. if [ ! -d "$MYSQL_DATA_DIR/mysql" ]; then echo ">>> MySQL data directory is empty. Initializing..." # Ensure ownership before initialization chown -R mysql:mysql "$MYSQL_DATA_DIR" # Initialize insecurely (empty root password) for practice mysqld --initialize-insecure --user=mysql --datadir="$MYSQL_DATA_DIR" echo ">>> Initialization complete." else echo ">>> MySQL data directory appears populated." fi # Ensure ownership is correct (just in case) chown -R mysql:mysql "$MYSQL_DATA_DIR" # 3. Start MySQL in the Background echo ">>> Starting MySQL Server..." # We use mysqld_safe directly as user 'mysql' runuser -u mysql -- /usr/bin/mysqld_safe --skip-grant-tables --skip-networking=0 & # 4. Wait for MySQL to be ready echo ">>> Waiting for MySQL to start..." # Loop until we can connect or 30 seconds pass for i in {1..30}; do if mysqladmin ping -u root --silent; then echo ">>> MySQL is up and running!" break fi echo "Waiting for MySQL... ($i/30)" sleep 1 done # 5. Start code-server echo ">>> Starting code-server..." exec runuser -u coder -- code-server --bind-addr 0.0.0.0:7860 --auth none /home/coder/projects