How to use an init container to check if MySQL is ready for connections?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Kubernetes, application pods often start faster than database dependencies. If MySQL is still initializing, applications may crash or enter noisy retry loops at boot. An init container can enforce startup ordering by blocking the main container until MySQL is truly usable.
Core Sections
1. Why init containers are better than startup sleep hacks
A fixed sleep in app startup scripts is fragile because database startup time changes with load, storage speed, and recovery events. Init containers provide explicit dependency gating:
- they run before app containers
- they must complete successfully
- failures are visible in pod status
This gives a deterministic startup contract. Instead of app logs full of transient connection errors, you get a clear init-stage signal that dependency is unavailable.
2. Build a readiness check that matches real app requirements
Checking only TCP connectivity is often too weak. A port can be open while authentication or schema migration is still incomplete. For many workloads, a query-level check is more accurate.
Example init container with bounded retry and SELECT 1:
This approach fails fast and visibly if dependency never becomes ready.
3. Keep retries bounded and observable
An unbounded until loop can hide real outages and block rollouts indefinitely. Use a finite retry budget and explicit exit code. Also print attempt numbers so logs tell you how long startup waited.
Helpful operational rule:
- short retries for transient cold starts
- clear timeout for persistent failures
- alerting on repeated init failures
This keeps incidents visible instead of silently stuck.
4. Separate dependency checks from app health probes
Init checks and probes solve different problems:
- init container: dependency readiness before startup
- readiness probe: app can serve traffic now
- liveness probe: app should be restarted if unhealthy
Do not drop readiness probes just because init container exists. After startup, app can still fail due to downstream issues, config drift, or memory pressure.
5. Handle credentials and least privilege
Never embed database secrets directly in manifests or shell scripts committed to source control. Reference Kubernetes Secret keys and use a low-privilege MySQL user dedicated to readiness checks.
For a simple query check, user permissions can usually be minimal. Restricting privilege reduces blast radius if credentials leak through logs or misconfiguration.
6. Debugging failed init containers
When a pod stays in Init state, inspect events and init logs first:
Common failure causes:
- wrong service hostname
- wrong credentials
- DNS resolution failures
- NetworkPolicy blocking traffic
Because init logic is isolated, these failures are easier to diagnose than app-level retry noise.
7. Advanced pattern: combine init with migration jobs
If your app also requires schema migrations, run migrations as a separate Job and make app deployment depend on Job success. Keep init container focused on availability checks only. This avoids mixing two concerns in one startup script and prevents long-running schema operations from hiding as readiness checks.
Common Pitfalls
- Using fixed sleep delays instead of real dependency checks.
- Running infinite retry loops that conceal prolonged outages.
- Checking only open port when authenticated query is required.
- Hardcoding database credentials in deployment manifests.
- Assuming init checks replace readiness and liveness probes.
Summary
- Init containers are the right place to gate startup on MySQL availability.
- Use query-level checks when your app needs authenticated DB access.
- Keep retries bounded with explicit failure to improve observability.
- Preserve secret hygiene and least-privilege database users.
- Combine init checks with proper probes for stable runtime behavior.
Related reading
- How to use ConfigMap configuration with Helm NginX Ingress controller - Kubernetes
- How to use helm with token based authentication system on EKS
- How to use imagePullSecrets in Helm 3
- How to use kubectl command instead of sudo kubectl
- How to use bash with an Alpine based docker image?
- How to use Docker Image in ECR with AWS EKS
- How to use async Mysql query with PHP PDO
- How to use auto increment for primary key id in dynamodb

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.