Skip to content

Recovering a cPanel MySQL host when /var/lib/mysql hits 100% — without losing data

A full data directory locks MySQL into a read-only state and risks crash on next restart. The recovery is staged — free space first, then resume writes — and the first move matters.

/var/lib/mysql at 100% is a cPanel host’s least graceful failure. MySQL stops accepting writes — INSERTs return errors, schema changes fail, the binlog can’t roll — but reads sometimes continue depending on caching. The site shows random errors. The host monitoring alerts fire. The instinct is rm the biggest file in /var/lib/mysql, which on cPanel is usually a binlog or a slow log, and the consequences range from “lost an hour of audit history” to “broke replication and the dbmap.” There’s a right order to do this in, and the first thing is not to delete anything.

What’s happening

MySQL writes to multiple files in /var/lib/mysql:

  • *.ibd — InnoDB data files. One per table (with innodb_file_per_table = ON, the default). Don’t touch these from the filesystem.
  • ibdata1 — the InnoDB system tablespace. Holds the data dictionary on older versions. Don’t touch.
  • ib_logfile* or undo_* — InnoDB redo and undo logs. Don’t touch.
  • mysql-bin.* — binary log files. These are safe to purge through SQL.
  • <hostname>-slow.log — slow query log. Safe to truncate.
  • mysql.log, error log — general logs. Safe to truncate.

The dangerous mistake is rm /var/lib/mysql/mysql-bin.000001 to free space — the index file still lists it as active, and the next FLUSH LOGS errors out, breaking automation. Free space the safe way: through MySQL’s own commands.

How to verify

df -h /var/lib/mysql
du -sh /var/lib/mysql/* | sort -h | tail -20
mysql -e "SHOW BINARY LOGS" | head
mysql -e "SHOW VARIABLES LIKE 'expire_logs_days'"
mysql -e "SHOW VARIABLES LIKE 'slow_query_log'"
ls -lh /var/lib/mysql/*.log
mysql -e "SELECT TABLE_SCHEMA, ROUND(SUM(DATA_LENGTH+INDEX_LENGTH)/1024/1024) AS mb FROM information_schema.TABLES GROUP BY TABLE_SCHEMA ORDER BY mb DESC LIMIT 10"

The biggest space consumers are usually binlogs, slow log, and one customer’s bloated InnoDB tables. The breakdown tells you where to recover space safely.

The fix

  1. Confirm MySQL is still alive. If mysql -e "SELECT 1" works, you have time to do this right. If MySQL has crashed, skip to “after restart” steps below.
  2. Purge expired binlogs first — usually frees the most space, no risk:
    mysql -e "PURGE BINARY LOGS BEFORE NOW() - INTERVAL 24 HOUR"
    Frees binlogs older than 24 hours. If you have replicas, check SHOW SLAVE HOSTS first and don’t purge what they haven’t read.
  3. Truncate the slow query log (it can grow to GBs):
    mysql -e "SET GLOBAL slow_query_log = 0"
    truncate -s 0 /var/lib/mysql/*-slow.log
    mysql -e "SET GLOBAL slow_query_log = 1"
    Disabling before truncate, re-enabling after, ensures the file handle is reopened.
  4. Truncate the error log if it’s grown:
    truncate -s 0 /var/log/mysql/error.log
    mysqladmin flush-logs
  5. Find the biggest tables with the query above, and if any are clearly bloated (DATA_FREE substantial), schedule an OPTIMIZE outside this incident.
  6. Move some data off the volume. If you have a separate volume mounted somewhere, you can move a single non-critical schema to it via mysqldump + drop + restore. Heavy and slow; only do this if other freed space isn’t enough.
  7. Once disk is below 80%, MySQL resumes accepting writes normally. Verify by inserting a row into a test database. Confirm df -h and SHOW STATUS LIKE 'Innodb_buffer_pool_pages_dirty' are recovering.
  8. Add or fix monitoring so this doesn’t recur:
    • Alert on /var/lib/mysql > 75% capacity.
    • Alert on the largest table size growing by more than 20% in a day.
    • Schedule binlog purge through expire_logs_days so manual cleanup isn’t needed.

If MySQL has already crashed (disk hit 100% during a write):

  1. Stop trying to start it. The disk is full; it won’t start.
  2. Free space via filesystem first: truncate -s 0 on the slow log and error log. Do NOT rm .ibd or mysql-bin.* files.
  3. Once 1 GB free, start MySQL: systemctl start mariadb. It may need to replay the redo log, which takes a few minutes.
  4. After it’s up, do the SQL-level purges (binlogs) as above.

For environments where the disk-full incident keeps recurring across cPanel hosts, Stack Harbor’s environment management team correlates growth patterns against backup and binlog retention, sets quotas, and shifts logs off the data volume.