Deploying a Laravel application to Kubernetes often requires preparing its MySQL database. Normally, Laravel handles database changes using:
php artisan migrate --force
However, when migrating an existing application or creating a development environment, you may already have a complete .sql database dump.
In this case, Kubernetes can import the SQL file automatically before Laravel starts.
This beginner-friendly guide explains how to use a Kubernetes init container to import an existing MySQL database instead of running Laravel migrations.
The Deployment Scenario
Assume we have:
- a Laravel application;
- a Docker image;
- a MySQL database;
- a database dump called
database.sql; - a Kubernetes or K3s cluster.
Instead of:
php artisan migrate --force
we want to execute:
mysql -h database-host -u database-user -p database-name < database.sql
The deployment flow becomes:
Kubernetes Pod starts
|
v
Wait for MySQL
|
v
Import database.sql
|
v
Start Laravel
Why Use a Kubernetes Init Container?
Init containers run before the main application container.
For example:
wait-db
|
v
import-db
|
v
Laravel application
Kubernetes will only start Laravel after both initialization steps complete successfully.
This makes init containers useful for database initialization and dependency checks.
Add the MySQL Client to the Docker Image
The PHP pdo_mysql extension allows Laravel to communicate with MySQL, but it does not necessarily provide the mysql command-line program.
For a Debian-based PHP image, install it with:
RUN apt-get update && apt-get install -y \
default-mysql-client \
&& rm -rf /var/lib/apt/lists/*
It can simply be added to the existing application dependencies:
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
git \
curl \
nginx \
default-mysql-client \
libpng-dev \
libonig-dev \
libxml2-dev \
libzip-dev \
zip \
unzip \
&& rm -rf /var/lib/apt/lists/*
Include the SQL File
If the SQL dump is part of the deployment package, copy it into the Docker image:
WORKDIR /var/www/html
COPY . /var/www/html
COPY database.sql /var/www/html/database.sql
The file will then be available inside the container at:
/var/www/html/database.sql
Make sure .dockerignore does not contain something like:
*.sql
Otherwise Docker may exclude the database dump from the build.
Store Database Settings in a Kubernetes Secret
Database configuration can be provided through a Kubernetes Secret:
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
namespace: application-dev
type: Opaque
stringData:
DB_CONNECTION: "mysql"
DB_HOST: "mysql.database.svc.cluster.local"
DB_PORT: "3306"
DB_DATABASE: "laravel"
DB_USERNAME: "laravel"
DB_PASSWORD: "change-me"
The application and initialization container can load these values with:
envFrom:
- secretRef:
name: app-secrets
For real environments, avoid storing production credentials directly in source control.
Wait for MySQL Before Importing
The first init container can wait until MySQL becomes reachable:
initContainers:
- name: wait-db
image: busybox:1.36
command:
- sh
- -c
- |
echo "Waiting for database..."
until nc -z mysql.database.svc.cluster.local 3306
do
echo "Database not ready..."
sleep 2
done
echo "Database ready."
This prevents the import from starting before the database service is available.
Import the SQL Dump
Instead of a Laravel migration container:
- name: migrate
command:
- sh
- -c
- php artisan migrate --force
we can create an SQL import container:
- name: import-db
image: ${IMAGE_TAG}
imagePullPolicy: Always
command:
- sh
- -c
- |
echo "Importing database..."
mysql \
-h"$DB_HOST" \
-P"$DB_PORT" \
-u"$DB_USERNAME" \
-p"$DB_PASSWORD" \
"$DB_DATABASE" < /var/www/html/database.sql
echo "Database import completed."
envFrom:
- secretRef:
name: app-secrets
The application container starts only after this command completes successfully.
Be Careful: The Import Runs Again When the Pod Is Recreated
This is the most important consideration.
Kubernetes init containers run whenever a new Pod is created.
Therefore:
First deployment
→ SQL imported
New deployment
→ SQL imported again
Pod recreated
→ SQL imported again
Depending on the SQL dump, repeated imports can cause duplicate records, SQL errors or even overwrite existing information.
A safer solution is to check whether the database already contains tables.
For example:
TABLE_COUNT=$(mysql \
-h"$DB_HOST" \
-P"$DB_PORT" \
-u"$DB_USERNAME" \
-p"$DB_PASSWORD" \
-Nse \
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$DB_DATABASE';")
Then import only when the database is empty:
if [ "$TABLE_COUNT" -eq 0 ]; then
echo "Database is empty. Importing..."
mysql \
-h"$DB_HOST" \
-P"$DB_PORT" \
-u"$DB_USERNAME" \
-p"$DB_PASSWORD" \
"$DB_DATABASE" < /var/www/html/database.sql
else
echo "Database already initialized. Skipping import."
fi
This is significantly safer for environments where Pods are frequently recreated.
Troubleshooting
If you receive:
mysql: not found
install default-mysql-client in the Docker image.
If you receive:
database.sql: No such file or directory
verify the file inside the container:
ls -lah /var/www/html/database.sql
If the Pod remains in an initialization state, check:
kubectl get pods -n <namespace>
Then inspect the import logs:
kubectl logs <pod-name> -n <namespace> -c import-db
You can also inspect the complete Pod:
kubectl describe pod <pod-name> -n <namespace>
These commands usually reveal database connectivity, authentication, SQL import or missing-file problems.
SQL Import vs Laravel Migrations
SQL imports and Laravel migrations serve different purposes.
An SQL dump is useful when:
- migrating an existing application;
- cloning an existing database;
- restoring an environment;
- initializing development or staging;
- working with legacy databases.
Laravel migrations are generally better for ongoing application development because schema changes remain version controlled.
A common approach is therefore:
Initial environment
↓
Import database.sql
↓
Future releases
↓
Laravel migrations
Security Considerations
Avoid putting sensitive production database dumps inside widely accessible Docker images.
SQL backups may contain:
- customer information;
- email addresses;
- password hashes;
- API credentials;
- internal application data.
Use sanitized database dumps for development whenever possible and always create a backup before importing data into an existing database.
Conclusion
Importing an existing MySQL database during a Laravel Kubernetes deployment is straightforward with init containers.
The basic process is:
- install the MySQL client in the Docker image;
- include or mount the SQL dump;
- wait for MySQL to become available;
- import the database from an init container;
- start Laravel after the import succeeds;
- prevent the SQL file from being imported repeatedly.
This approach is particularly useful for Laravel migrations to Kubernetes, K3s development environments, legacy applications and database restoration scenarios.
For production systems, consider using a dedicated Kubernetes Job or CI/CD database initialization stage instead of automatically importing a database every time an application Pod is created.
SEO Information
SEO Title: Import a MySQL Database During Laravel Kubernetes Deployment
Focus Keyphrase: Laravel Kubernetes MySQL import
Meta Description: Learn how to import an existing MySQL SQL dump during a Laravel deployment on Kubernetes or K3s using Docker and Kubernetes init containers.
URL Slug: laravel-kubernetes-import-mysql-database
Keywords: Laravel Kubernetes deployment, Kubernetes MySQL import, Laravel K3s, Docker MySQL, Kubernetes init container, SQL dump Kubernetes, Laravel Docker deployment, MySQL database migration
Tags: Laravel, Kubernetes, K3s, Docker, MySQL, PHP, DevOps, Database Migration, CI/CD


