When /var/lib/mysql is creeping toward full — or one account’s database usage is climbing in cPanel — the first question is always the same: which database, which table, and is it real data or bloat? Disk pressure from MySQL is rarely evenly spread; it is almost always concentrated in a handful of tables (a wp_options stuffed with autoloaded transients, a logging table no one prunes, a WooCommerce actionscheduler_actions that grew without bound) or in things that are not user data at all — binary logs, the InnoDB undo/redo files, or temp tables that spilled to disk. Ranking the consumers by size, and distinguishing genuine data from reclaimable overhead, is the whole task, because the fix for “a 40 GB logging table” is nothing like the fix for “30 GB of binlogs.”
What’s happening
InnoDB stores each table’s data and indexes in its tablespace files; with innodb_file_per_table on (the default on modern cPanel), each table has its own .ibd whose size on disk reflects its data plus index plus internal free space. MyISAM tables store data and index separately. On top of the per-table files, the datadir also holds the shared InnoDB system tablespace, the redo log, undo logs, and — the frequent surprise — binary logs (binlog.*), which accumulate for replication and point-in-time recovery and can quietly become the largest thing in the directory.
So “MySQL is using a lot of disk” decomposes into several distinct sources. Real table data is the legitimate kind, and within it the growth is usually one or two tables, not the whole database. Then there is reclaimable overhead: binary logs that are never purged, an InnoDB tablespace bloated with free space after large deletes (deleting rows does not shrink the .ibd), and temp/undo files. Telling these apart requires looking both at MySQL’s own size accounting (information_schema.tables) and at the filesystem (du), because the two can disagree — a table that “has” 2 GB of data can occupy 6 GB on disk if it was never rebuilt after a big purge. The diagnosis is to rank by both views.
How to verify
Rank databases and tables by size using MySQL’s own accounting:
# Databases by total size, largest first
mysql -e "SELECT table_schema AS db,
ROUND(SUM(data_length+index_length)/1024/1024,1) AS mb
FROM information_schema.tables GROUP BY table_schema ORDER BY mb DESC;"
# Largest individual tables across the whole server
mysql -e "SELECT table_schema, table_name,
ROUND((data_length+index_length)/1024/1024,1) AS mb,
ROUND(data_free/1024/1024,1) AS free_mb, table_rows
FROM information_schema.tables ORDER BY (data_length+index_length) DESC LIMIT 25;"
Compare against the filesystem — the datadir often holds non-table consumers:
# What is actually big on disk in the datadir
du -h --max-depth=1 /var/lib/mysql/ | sort -rh | head -20
# The frequent surprise: binary logs
ls -lhS /var/lib/mysql/ | grep -iE 'binlog|bin\.|relay' | head
du -ch /var/lib/mysql/binlog.* 2>/dev/null | tail -1
Spot the classic WordPress bloat sources directly:
# Autoloaded options bloat (a common silent grower)
mysql cpuser_db -e "SELECT ROUND(SUM(LENGTH(option_value))/1024/1024,1) AS autoload_mb
FROM wp_options WHERE autoload='yes';"
# A table with lots of free space = bloat reclaimable by rebuild
mysql -e "SELECT table_name, ROUND(data_free/1024/1024,1) AS free_mb
FROM information_schema.tables WHERE table_schema='cpuser_db' AND data_free>50000000;"
The fix
- Rank the consumers before deleting anything — the two queries above (by database, by table) plus
duon the datadir tell you exactly where the space went. Disk pressure is almost always concentrated; find the one or two tables or the binlog pile responsible rather than guessing. The fix depends entirely on which source dominates. - Purge binary logs if they are the bulk — if
binlog.*files dominate the datadir, they are accumulating faster than they expire. Set a sanebinlog_expire_logs_seconds(e.g. 7 days) in/etc/my.cnfso they auto-purge, and reclaim the backlog now withPURGE BINARY LOGS BEFORE NOW() - INTERVAL 7 DAY;. Do notrmbinlog files by hand — that desyncs the index; always purge through MySQL. Keep enough history for your point-in-time recovery window. - Trim a runaway WordPress table at the application level — if the big table is
wp_options(autoloaded transients),actionscheduler_actions/actionscheduler_logs,wp_postmeta, or a plugin’s log table, the fix is to prune it through the application: clear expired transients, let Action Scheduler purge completed actions, or use a maintenance plugin/wp-cli. Deleting rows directly is fine too, but match the application’s expectations so it does not just regrow. - Reclaim InnoDB free space with a rebuild after large deletes — deleting rows marks space free inside the
.ibdbut does not return it to the filesystem, so a table can show largedata_free. Rebuild it to shrink the file:OPTIMIZE TABLE cpuser_db.bigtable;(which for InnoDB does a table rebuild). Do this off-peak — it locks/rewrites the table — and confirmdata_freedrops and the.ibdshrinks afterward. - Stop temp tables from spilling and accumulating — if the datadir grows with temp files during heavy reporting queries, the queries are spilling to disk. That points at a query needing an index or a
tmp_table_size/max_heap_table_sizetoo small for the workload — fix the query or raise the in-memory temp size so it stops writing giant temp files. Confirmtmpdirhas space and is not the thing filling. - Set per-account database awareness so it does not recur — on a shared host, one tenant’s database bloat eats everyone’s disk. Watch the per-database ranking over time (a scheduled run of the size query), and on CloudLinux consider the inode/disk quotas. Catching a table that is doubling weekly before it fills the datadir is far cheaper than a 100%-full recovery.
- Verify the space actually came back — after purging logs or rebuilding tables, confirm with
df -h /var/lib/mysqland re-run the size queries. MySQL’sdata_freeshould drop and the filesystem should show reclaimed space. A table you “cleaned” that still occupies the same.ibdwas deleted-from but not rebuilt (step 4); the space is not back until the file shrinks.
Database disk growth is almost never the whole database — it is one or two tables, a binlog pile, or post-delete bloat, and the entire job is ranking the consumers to find which. Then the fix follows the source: purge logs through MySQL, prune tables through the app, rebuild to reclaim free space. Stack Harbor diagnoses and tames MySQL disk growth under cPanel server management — start a conversation when the data directory keeps filling.