MySQL shutdown unexpectedly | Fix the xampp MySQL issue.


What is XAMPP
XAMPP is indeed a popular and convenient tool for PHP developers, bundling Apache, MySQL (MariaDB), PHP, and Perl into an easy-to-install package for local development. However, the "MySQL shutdown unexpectedly" error is a common frustration, particularly due to port conflicts.
The Core Issue: Port Conflicts with MySQL
The error "MySQL shutdown unexpectedly" often points to a conflict with port 3306, which is the default port for MySQL. This happens when another service or application on your computer is already using this port. Common culprits include:
Another MySQL instance: You might have a standalone MySQL server, a different MAMP/WAMP/LAMP stack, or even a previous XAMPP installation running in the background.
Other database software: Tools like PostgreSQL, SQL Server, or even specific applications might be configured to use port 3306 for their internal database.
Skype or other communication applications: Historically, Skype used port 80 and sometimes 3306, though this is less common with newer versions.
Solution
When XAMPP's MySQL tries to start and finds port 3306 already in use, it fails, leading to the error message. While changing the port in my.ini
is a valid troubleshooting step for port conflicts, it might not always resolve the issue if the underlying problem is persistent or if other configurations are also clashing.
Why Docker is a Robust Solution
Your approach of using Docker to run MySQL is an excellent and increasingly popular solution for several reasons:
Isolation: Docker containers provide isolated environments. This means that MySQL running in a Docker container operates independently of other software on your host machine. It has its own network stack within the container, effectively bypassing port conflicts with services outside the container. Even if another application is using port 3306 on your host, you can map a different host port to the container's internal port 3306.
Consistency: Docker ensures that your development environment is consistent across different machines and over time. You define your MySQL setup (version, configuration) in a
Dockerfile
ordocker-compose.yml
file, and it will run the same way everywhere. This eliminates "it works on my machine" issues.Cleanliness: When you're done, you can simply stop and remove the Docker container, leaving no residual files or conflicting services on your system.
Version Control: Easily switch between different MySQL versions for different projects without conflicts.
Scalability & Microservices: While perhaps overkill for a simple local setup, Docker naturally supports more complex architectures, making it a valuable skill for future development.
For a PHP developer, while XAMPP is a quick start, Docker provides a more professional, flexible, and robust local development environment.
How to Set Up MySQL with Docker
We crate a simple docker file and and using docker we can run it very easily. And that working in my case. Yes docker have multiple usecase and some different use case but if it fix our issues so why we can’t use ?
How to setup ?
You've correctly identified the need for Docker Desktop and a docker-compose.yml
file. Here's how to set it up:
Download: Go to the official Docker website (www.docker.com/products/docker-desktop) and download Docker Desktop for your operating system (Windows, macOS, Linux).
Craete docker-compose.yml
version: '3.8' services: mysql: image: mysql:latest container_name: mysql_server restart: always environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_DATABASE: dbname MYSQL_USER: user MYSQL_PASSWORD: userpass ports: - "3306:3306" volumes: - mysql_data:/var/lib/mysql phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin restart: always ports: - "8080:80" environment: PMA_HOST: mysql MYSQL_ROOT_PASSWORD: rootpass depends_on: - mysql volumes: mysql_data:
Now start your Docker Desktop
docker-compose up -d
Now we use the MySQL db :
http://localhost:8080/
Connect to PHP application :
<?php $host = 'localhost'; $user = 'user'; $pass = 'userpass'; $db = 'skart'; $conn = new mysqli($host, $user, $pass, $db); if ($conn->connect_error) { die('DB Connection Failed: ' . $conn->connect_error); } ?>
Conclusion
Using Docker for MySQL effectively sidesteps the common XAMPP port conflict issues by providing an isolated and consistent environment. While it introduces a new tool to your workflow, the benefits in terms of reliability, project consistency, and future scalability make it a worthwhile investment for any PHP developer. You can still use XAMPP's Apache and PHP components, or you can even containerize Apache/PHP as well for a fully Dockerized development environment!
Subscribe to my newsletter
Read articles from Anirban Kundu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Anirban Kundu
Anirban Kundu
A passionate Full-Stack Developer with a strong focus on innovation and continuous learning. Committed to delivering high-quality, scalable products and driving impactful solutions from concept to deployment.