Skip to content

Commit

Permalink
refactor: Optimize MySQL directory setup and permission handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pcfreak30 committed Dec 4, 2024
1 parent 5a6fc1c commit 3b50bb0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
12 changes: 11 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ source "${LIB_PATH}/core/cron.sh"
if [ "$(id -u)" = "0" ]; then
# Ensure correct permissions on key directories
# Create and set permissions on required directories
mkdir -p $DATA_DIR $RUN_DIR $LOG_DIR $CONFIG_DIR
mkdir -p $DATA_DIR $RUN_DIR $LOG_DIR $CONFIG_DIR /var/lib/mysql-files

# Set proper permissions for MySQL directories
chown -R mysql:mysql $DATA_DIR $RUN_DIR $LOG_DIR $CONFIG_DIR /var/lib/mysql-files
chmod 750 $DATA_DIR /var/lib/mysql-files
chmod 755 $RUN_DIR $LOG_DIR $CONFIG_DIR

# Ensure directory is clean for initialization
if [ ! -d "$DATA_DIR/mysql" ]; then
rm -rf "$DATA_DIR"/*
fi

# Create base MySQL configuration
cat > "/etc/my.cnf" << EOF
Expand Down
24 changes: 14 additions & 10 deletions lib/mysql-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ init_mysql() {
rm -f /var/run/mysqld/mysqld.pid
rm -f /var/run/mysqld/mysqld.sock*

# Initialize MySQL with explicit paths (directories should be created by root)
# Verify directory permissions before initialization
if [ ! -w "$DATA_DIR" ]; then
log_error "Data directory $DATA_DIR is not writable"
return 1
fi

# Initialize MySQL with explicit paths
mysqld --initialize-insecure --user=mysql \
--datadir="$DATA_DIR" \
--basedir=/usr \
Expand All @@ -58,7 +64,13 @@ init_mysql() {
if [ -n "$MYSQL_ROOT_PASSWORD" ]; then
log_info "Starting MySQL with skip-grant-tables to set root password..."

# Remove any stale files (directories should exist and be owned by mysql)
# Verify socket directory is writable
if [ ! -w "$RUN_DIR" ]; then
log_error "Socket directory $RUN_DIR is not writable"
return 1
fi

# Remove any stale files
rm -f /var/run/mysqld/mysqld.sock*
rm -f /var/run/mysqld/mysqld.pid

Expand Down Expand Up @@ -166,14 +178,6 @@ start_mysql() {
return 1
fi

# Ensure required directories exist
mkdir -p "$RUN_DIR"
chown mysql:mysql "$RUN_DIR"

# Ensure data directory exists and has correct permissions
mkdir -p /var/lib/mysql
chown -R mysql:mysql "$DATA_DIR"

# Check for forced master recovery
FORCE_MASTER_RECOVERY=${FORCE_MASTER_RECOVERY:-0}

Expand Down

0 comments on commit 3b50bb0

Please sign in to comment.