When your CentOS server screams "No space left on device", it's typically /var that's the culprit. Let me walk through a real-world solution approach that's saved me countless times.
# First, verify the actual disk usage
df -h
du -sh /var/* | sort -h
Before MySQL can restart, we need immediate breathing room:
# Clear system logs (preserving current logs)
sudo journalctl --vacuum-size=100M
sudo truncate -s 0 /var/log/*.log
# Clean package cache
sudo yum clean all
sudo package-cleanup --oldkernels --count=1
For persistent solutions, implement log rotation and scheduled cleanups:
# Create a cleanup script /usr/local/bin/clean_logs
#!/bin/bash
find /var/log -type f -name "*.log" -mtime +30 -exec rm -f {} \;
find /var/log -type f -name "*.gz" -mtime +90 -exec rm -f {} \;
Make it executable and add to cron:
chmod +x /usr/local/bin/clean_logs
(crontab -l ; echo "0 3 * * * /usr/local/bin/clean_logs") | crontab -
For MySQL-related space issues:
# Clean binary logs if replication isn't needed
PURGE BINARY LOGS BEFORE DATE_SUB(NOW(), INTERVAL 7 DAY);
# Optimize InnoDB tablespace
mysqlcheck -o --all-databases
Implement these configuration changes:
# /etc/logrotate.conf
/var/log/mysql.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
}
# /etc/my.cnf
[mysqld]
innodb_file_per_table=1
expire_logs_days=3
For /var/www overflow, consider symlinking to /home:
sudo systemctl stop httpd
sudo mv /var/www /home/
sudo ln -s /home/www /var/www
sudo systemctl start httpd
When standard cleanup isn't enough:
# Find large files
find /var -type f -size +100M -exec ls -lh {} \;
# Monitor disk space in real-time
watch -n 60 'df -h; du -sh /var/* | sort -h'
First, let's identify the largest space consumers in your /var partition:
# Find largest directories in /var
sudo du -sh /var/* | sort -rh | head -n 10
From your output, we can see /var/www (2.6G) and /var/lib (1.5G) are the biggest consumers.
For log files (/var/log at 221M), use these commands:
# View largest log files
sudo find /var/log -type f -exec du -sh {} + | sort -rh | head -n 10
# Rotate and clean old logs
sudo logrotate -f /etc/logrotate.conf
sudo journalctl --vacuum-size=100M
# Clear specific application logs
sudo truncate -s 0 /var/log/mysqld.log
sudo truncate -s 0 /var/log/httpd/access_log
sudo truncate -s 0 /var/log/httpd/error_log
The /var/lib/mysql directory often grows large. Try these optimizations:
# Clean up MySQL binary logs (if replication isn't used)
PURGE BINARY LOGS BEFORE NOW();
# Optimize tables to reclaim space (run in MySQL client)
mysqlcheck --optimize --all-databases
# Temporarily move ibdata files if needed (stop MySQL first)
sudo systemctl stop mysqld
sudo mv /var/lib/mysql/ibdata1 /tmp/
sudo systemctl start mysqld
For /var/www (2.6G):
# Find largest files in web directory
sudo find /var/www -type f -exec du -sh {} + | sort -rh | head -n 20
# Clean up temporary uploads and caches
sudo find /var/www -type f -name "*.tmp" -delete
sudo find /var/www -type f -name "*.cache" -delete
# Consider moving static content to /home partition
sudo mv /var/www/static /home/www-static
sudo ln -s /home/www-static /var/www/static
Create a cleanup script (/usr/local/bin/cleanup_disk.sh):
#!/bin/bash
# Rotate logs
/usr/sbin/logrotate -f /etc/logrotate.conf
# Clear package manager cache
dnf clean all || yum clean all
# Remove old kernel versions (keep last 2)
package-cleanup --oldkernels --count=2
# Remove temp files older than 30 days
find /tmp -type f -mtime +30 -delete
find /var/tmp -type f -mtime +30 -delete
# Clean yarn and npm caches
if [ -x "$(command -v yarn)" ]; then
yarn cache clean
fi
if [ -x "$(command -v npm)" ]; then
npm cache clean --force
fi
Make it executable and set a cron job:
sudo chmod +x /usr/local/bin/cleanup_disk.sh
sudo crontab -e
# Add this line to run weekly:
0 3 * * 0 /usr/local/bin/cleanup_disk.sh >/dev/null 2>&1
1. Set up monitoring with a simple disk check script:
#!/bin/bash
THRESHOLD=90
CURRENT=$(df /var --output=pcent | tail -1 | tr -d '% ')
if [ "$CURRENT" -gt "$THRESHOLD" ]; then
echo "WARNING: /var partition at ${CURRENT}% usage" | mail -s "Disk Space Alert" admin@example.com
/usr/local/bin/cleanup_disk.sh
fi
2. Consider resizing partitions or moving directories to less utilized partitions:
# Example moving MySQL data directory
sudo systemctl stop mysqld
sudo mv /var/lib/mysql /home/mysql
sudo ln -s /home/mysql /var/lib/mysql
# Update /etc/my.cnf with new datadir
sudo systemctl start mysqld