MySQL Slow Query Log Filling Disk – Real Fix
Your MySQL slow query log is eating disk space fast. Here's the quick fix, why it works, and how to stop it coming back.
You open your monitoring dashboard, and boom – your MySQL server disk is 99% full.
I've been there. The slow query log is the culprit. It's a text file that grows until your server chokes. Let me show you the fastest way to stop the bleeding and fix the root cause.
The Immediate Fix: Disable and Clean Up
Don't mess around. If your disk is nearly full, you need to stop the log from growing right now. Run these commands in your MySQL client (as root or a user with SUPER privileges):
SET GLOBAL slow_query_log = OFF;
SET GLOBAL long_query_time = 10;
This turns off the slow query log instantly. The long_query_time setting resets to 10 seconds (default). If you had it set too low (like 0.1 seconds), every little query was being logged. So this stops new entries.
Now free up space by truncating the log file. On Linux:
sudo truncate -s 0 /var/log/mysql/mysql-slow.log
On Windows (or if you prefer SQL), you can rename the file and create an empty one. For example:
sudo mv /var/log/mysql/mysql-slow.log /var/log/mysql/mysql-slow.log.bak
sudo touch /var/log/mysql/mysql-slow.log
sudo chown mysql:mysql /var/log/mysql/mysql-slow.log
Then re-enable the log with a safe setting:
SET GLOBAL slow_query_log = ON;
Your disk usage should drop immediately. If the disk was fully full, you might also need to clean old backups or binary logs – but the slow log is the first place to check.
Why This Works
The slow query log writes every query that runs longer than long_query_time. If you set that value too low – like 0.5 seconds – then even a busy e-commerce site with 1000 queries per second will fill a 10 GB disk in hours. Combine that with log_queries_not_using_indexes = ON, and you're logging almost everything. The log file grows without a limit by default – MySQL doesn't rotate it. So it fills the disk until there's no space left.
By turning the log off and truncating, you reclaim disk space. Setting long_query_time back to 10 seconds means only truly slow queries get logged – not normal traffic.
Less Common Variations
Sometimes the slow query log isn't your only problem. But here's what else I've seen:
- The binary log is the real disk hog. Check with
SHOW BINARY LOGS;. If you have lots of old binlogs, they can fill disk faster than the slow log. Delete old ones withPURGE BINARY LOGS BEFORE '2025-01-01 00:00:00'; - General log enabled by mistake. Some folks turn on
general_log = ONfor debugging and forget. That log writes every query. Turn it off withSET GLOBAL general_log = OFF; - Slow log file location is on a small partition. Maybe your
slow_query_log_fileis on/var, which is only 2 GB. Move it to a larger partition or set a different path. - Orchestration tools like Orchestrator or ProxySQL create their own logs. Check those too.
If you're using MySQL 8.0+, the performance schema also stores slow query data. But that's in memory and won't fill disk.
Prevention: Never Let This Happen Again
The real trick is to set up log rotation and limit the log file size. Here's what I do in production:
1. Set long_query_time to a sensible value
On a typical web app, 2 seconds is fine for long_query_time. For high-traffic systems, 5 or 10 seconds works. Test with your workload. Don't set it below 1 second unless you're debugging a specific slow query.
2. Turn off log_queries_not_using_indexes in production
This setting is useful for development, but in production it logs tons of queries that don't use indexes – like small lookup tables. Keep it off unless you're actively optimizing.
3. Add log rotation via logrotate
Create a config file at /etc/logrotate.d/mysql-slow with content like:
/var/log/mysql/mysql-slow.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 640 mysql adm
postrotate
/usr/bin/mysqladmin flush-logs
endscript
}
This rotates the log daily, keeps 7 days of logs, and compresses old ones. The flush-logs command tells MySQL to start a new log file.
4. Monitor log file size
Add a cron job or alert if the slow log exceeds 1 GB. For example:
0 * * * * du -sh /var/log/mysql/mysql-slow.log | awk '$1 ~ /G/ {print "Slow log too big!"}'
Or use a proper monitoring tool like Prometheus with the MySQL exporter.
5. Consider using performance schema instead
In MySQL 8.0, you can query performance_schema.events_statements_summary_by_digest to find slow queries without a file. This doesn't fill disk. But it's a bit more setup – you need to enable the performance schema and the statement digest tables. For most people, the log rotation approach is simpler.
One last thing: if you're on a shared hosting or managed MySQL (like RDS), you can't disable the slow log from the SQL prompt. You'll need to edit the parameter group to set slow_query_log = 0 and then apply it. On RDS, the slow log is written to CloudWatch Logs – you can set a retention period there to avoid infinite growth.
That's it. You've freed disk space, stopped the log from filling it again, and set up prevention. Your server is safe now.
Was this solution helpful?