File size: 1,641 Bytes
ef25800
 
46e92bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97e88d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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