/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 (withinnodb_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*orundo_*— 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
- 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. - Purge expired binlogs first — usually frees the most space, no risk:
Frees binlogs older than 24 hours. If you have replicas, checkmysql -e "PURGE BINARY LOGS BEFORE NOW() - INTERVAL 24 HOUR"SHOW SLAVE HOSTSfirst and don’t purge what they haven’t read. - Truncate the slow query log (it can grow to GBs):
Disabling before truncate, re-enabling after, ensures the file handle is reopened.mysql -e "SET GLOBAL slow_query_log = 0" truncate -s 0 /var/lib/mysql/*-slow.log mysql -e "SET GLOBAL slow_query_log = 1" - Truncate the error log if it’s grown:
truncate -s 0 /var/log/mysql/error.log mysqladmin flush-logs - Find the biggest tables with the query above, and if any are clearly bloated (
DATA_FREEsubstantial), schedule an OPTIMIZE outside this incident. - 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. - Once disk is below 80%, MySQL resumes accepting writes normally. Verify by inserting a row into a test database. Confirm
df -handSHOW STATUS LIKE 'Innodb_buffer_pool_pages_dirty'are recovering. - 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_daysso manual cleanup isn’t needed.
- Alert on
If MySQL has already crashed (disk hit 100% during a write):
- Stop trying to start it. The disk is full; it won’t start.
- Free space via filesystem first:
truncate -s 0on the slow log and error log. Do NOTrm.ibdormysql-bin.*files. - Once 1 GB free, start MySQL:
systemctl start mariadb. It may need to replay the redo log, which takes a few minutes. - 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.