TLDR
- Problem: Oversized log file consumed all remaining space, causing service anomalies
- Cause: MySQL was configured with mysql-general-log enabled, without scheduled cleanup
- Investigation Method: Checked MySQL settings and disk space usage
- Solution: Set up scheduled cleanup using logrotate
Scenario
The MySQL Slave in the production environment regularly generates analysis reports and emails them to relevant personnel.
We received a notification that reports hadn’t been received for some time. Upon logging into the host to attempt manual sending, it failed due to insufficient space.
The database files showed no abnormalities, and the database was located on an external hard drive with ample remaining space.
Investigation
Checking Linux Disk Space
First, we used df -h
to check the disk space status, only to find very little remaining space.
|
|
|
|
Checking Large Directories in Root
We can use the following command to view the top 20 directories consuming the most space in the root directory.
The issue I encountered was with /var/log/mysql/mysql-gen.log
. (The output below is for illustration)
|
|
|
|
Analyzing the Cause
About six months ago, the client requested us to enable MySQL’s general_log and forward it through rsyslog.
A colleague, unfamiliar with general_log and unaware that rsyslog forwards in real-time, didn’t set up scheduled cleanup.
In simple terms, general_log is MySQL’s detailed log that records all queries, thus generating a large volume of logs.
In just six months, it completely filled up the remaining space.
Solution
Step 1: Use logrotate for periodic log file cleanup
Add the following configuration to /etc/logrotate.d/mysql
:
|
|
Step 2: Force execute the new configuration
|
|
Step 3: Clean up existing large log files
Remove the rotated mysql-gen.log.1
file to free up space.
Step 4: Verify results
Use df -h
again to check disk usage and confirm that root directory space has been freed.
Conclusion
- If Linux operates abnormally, check hardware and memory usage.
- Use logrotate for scheduled log cleanup to prevent logs from growing too large and consuming space.
- Before making any configurations or running commands, it’s crucial to understand their purpose and potential consequences.