Solved MySQL stopped and it can’t be restart
Solved: MySQL Stopped and Can’t Be Restarted (Full Fix + Prevention Guide)
When your MySQL service fails to restart, it can take your entire website down. This guide walks you through diagnosing the issue, clearing disk space, and setting up MySQL to prevent it from happening again.
🔍 TL;DR – Quick Fix for “MySQL Can’t Be Restarted”
- Symptom:
mysql.service: Unit entered failed state.
- Root Cause: Low disk space due to excessive binary log files
- Fix: Purge binlogs using:
PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;
- Prevention: Set auto-expiry:
SET GLOBAL binlog_expire_logs_seconds = (60*60*24*3);
Symptom: MySQL Fails to Restart
You attempt to restart MySQL using:
sudo service mysql restart
And receive this:
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
Checking logs with:
journalctl -xe
Returns logs like:
mysql.service: Failed with result 'exit-code'.
...
AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/3864/status"
...
InnoDB: Plugin initialization aborted with error Generic error
Plugin 'InnoDB' init function returned error.
Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
Diagnosis: Check Disk Usage
Use:
df -h
This shows /dev/vda1
is 100% full.
Then dig deeper with:
du -h --max-depth=1 /
du -hs /var/*
du -hs /var/lib/*
Findings:
40G /var/lib/mysql
2.7G /var/lib/mysql_bkp
Binary logs (binlog) are consuming disk space:
/var/lib/mysql/binlog.000316 (10GB)
/var/lib/mysql/binlog.000384 (1.1GB)
Fix: Purge MySQL Binary Logs
Log into MySQL:
mysql -u root -p
Then run:
PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;
This deletes old logs and frees disk space immediately.
Check disk usage again:
du -h --max-depth=1 /
Result:
Disk space recovered! You now have 25GB free.
🔢 Prevent This Issue in the Future
Set MySQL binary log auto-expiry:
SET GLOBAL binlog_expire_logs_seconds = (60*60*24*3);
SET PERSIST binlog_expire_logs_seconds = (60*60*24*3);
This automatically removes binlogs older than 3 days.
MySQL 8.0+ Note: Use binlog_expire_logs_seconds
, not expire_logs_days
.
MySQL stores this in:
/var/lib/mysql/mysqld-auto.cnf
More info: MySQL 5 Reference Manual
💡 Pro Tip: Check This First
When MySQL refuses to restart:
- Always run
df -h
to check disk usage - Investigate
/var/lib/mysql
and/var/log/mysql
- Look at logs:
/var/log/mysql/error.log
Commands Recap
sudo service mysql restart
journalctl -xe
df -h
du -hs /var/*
du -hs /var/lib/*
mysql -u root -p
PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;
SET GLOBAL binlog_expire_logs_seconds = (60*60*24*3);
🌐 Official Resources & Credits
Hosting & Optimization Tip
This guide was inspired by real issues on a DigitalOcean droplet. It’s one of the most affordable and developer-friendly platforms for running WordPress and MySQL.
If you found this helpful, leave a comment or share it with someone stuck on the same issue. Need help? Reach out — we offer MySQL support and server tuning services.
If you enjoy this tutorial and you find it helpful don’t forget to share and leave a comment especially those who encounter issues and I will be gladly available to sort your problem you can also hire us