Skip to content

Conversation

@JayShenkar07
Copy link

Fixes issue: #448

This PR fixes the MySQL connection retry logic in the Docker Mautic container startup script.
Currently, the startup script relies on string matching to detect whether MySQL is alive:

Problem with String-Based Matching

  • Unreliable output: mysqladmin ping may print warnings, the script checks for exact string equality, any extra text (like the warning) causes the condition to never evaluate to true.
  • Logical issue: Even if the database is up and reachable, the loop continues indefinitely, printing "MySQL is not ready yet" and hanging the container startup. (This is exact case what I was facing)

Changes Made

  • Replaced string matching with exit-code-based detection using mysqladmin ping.
  • Exit code 0 → MySQL is reachable.
  • Exit code non-zero → retry.
  • Added retry loop with configurable max attempts (MAX_FAILURE_COUNT) and logging.
  • Avoids issues with warnings, extra messages, or output formatting changes.
  • Ensures the container does not hang indefinitely if MySQL is temporarily unavailable.

Why This is Necessary

  • Fixes container startup hangs when MySQL is not immediately available.
  • Improves robustness and reliability of the Mautic Docker setup.
  • Aligns with best practices for checking database availability in scripts.

@cibero42 cibero42 changed the title improved string matching with exit-code-based detection for db connection [BUG] Improved string matching with exit-code-based detection for db connection Oct 7, 2025
@cibero42 cibero42 requested a review from O-Mutt October 7, 2025 18:06
Copy link
Contributor

@cibero42 cibero42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

Thank you for your contribution. Please have a look on my comments

log_debug "MySQL is alive and well."
fi
# Max attempts reached
log_error "MySQL is not responding after ${MAX_FAILURE_COUNT} attempts. Exiting."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log_error "MySQL is not responding after ${MAX_FAILURE_COUNT} attempts. Exiting."
log_startup_error_header
log_error "MySQL is not responding after ${MAX_FAILURE_COUNT} attempts. Exiting."

fi

if [[ "${IS_MYSQL_ALIVE}" =~ "error" ]]; then
log_error "MySQL response contained error: ${IS_MYSQL_ALIVE}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintaining these error logs would be better, as they allow people to diagnose their DB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants