目次
- 1 1. Introduction
- 2 2. Main Types of MySQL Logs
- 3 3. How to Configure Each Log
- 4 4. Log File Storage Locations and Management
- 5 5. How to Use Logs
- 6 6. Points to Note and Best Practices
- 7 7. Summary
1. Introduction
The Role and Necessity of MySQL Log Management
MySQL is used by many companies and developers as a database management system. In its operation, log management plays a very important role. A log is a file that records the database’s operational status, errors, query execution history, and so on. These are essential for system administration, troubleshooting, and performance tuning. For example, during a server failure, the error log can sometimes pinpoint the cause, and analyzing the slow query log can enable measures to prevent performance degradation. In this way, properly configuring and managing logs can significantly improve database stability and efficiency.Purpose of the Article and Target Audience
This article explains the basic knowledge and specific configuration methods for MySQL log management. The intended readers are as follows.- Beginners with basic MySQL knowledge
- Intermediate users who want to learn practical log management
- Those aiming to improve database performance and troubleshoot issues
About the Next Steps
In the next installment, we will discuss in detail the various types of logs available in MySQL. This will allow you to accurately understand each log’s role and use.2. Main Types of MySQL Logs
Error Log
Purpose and Use
The error log records MySQL server start-ups, shutdowns, system errors, warnings, and the like. It is used to verify that the server is operating correctly and to pinpoint the cause when problems arise.Use Cases
- Analyzing errors that occur during server startup
- Identifying configuration mistakes or file permission issues
- Diagnosing and recovering from crashes
Error Log Example
2023-12-22T10:30:45.123456Z 0 [ERROR] [MY-013132] [Server] Plugin 'InnoDB' initialization failed.
2023-12-22T10:30:45.123456Z 0 [ERROR] [MY-010119] [Server] Aborting
General Query Log
Purpose and Use
The general query log records all SQL queries sent to the MySQL server. It helps track user activity and verify behavior in test environments.Use Cases
- Monitoring user actions and managing logs
- Analyzing executed SQL statements and confirming their behavior
- Query analysis during debugging
General Query Log Example
2023-12-22T10:35:00.123456Z 10 Query SELECT * FROM users WHERE id=1;
2023-12-22T10:35:01.123456Z 11 Query UPDATE users SET status='active' WHERE id=1;
Slow Query Log
Purpose and Use
The slow query log records queries whose execution time exceeds a configured threshold. It helps identify performance bottlenecks and aids in crafting efficient SQL statements and optimizing indexes.Use Cases
- Identifying queries causing performance degradation
- Analyzing and optimizing query execution times
- Monitoring server load from high‑intensity queries
Slow Query Log Example
# Time: 2023-12-22T10:40:00
# User@Host: root[root] @ localhost []
# Query_time: 12.345 Lock_time: 0.001 Rows_sent: 1000 Rows_examined: 20000
SELECT * FROM orders WHERE status='pending';
Binary Log
Purpose and Use
The binary log records data‑modifying operations (INSERT, UPDATE, DELETE, etc.) within the database. It is a crucial log used for data recovery and replication (database duplication).Use Cases
- Restoring the latest state after a database failure
- Managing data synchronization via replication settings
- Tracking change history and supporting audit requirements
Binary Log Example (output from MySQLbinlog)
# at 12345
#2023-12-22T10:45:00 server id 1 end_log_pos 234
INSERT INTO orders (id, status) VALUES (1, 'pending');
Summary
Each of these logs serves a distinct role and is essential for managing and monitoring MySQL. The next section will detail the concrete steps to configure these logs, illustrating practical usage with configuration examples and sample code.3. How to Configure Each Log
Error Log Configuration
Configuration Steps
The error log is enabled by default in MySQL’s initial setup, but you can customize its location and format.Example Configuration
- Edit MySQL’s configuration file (
my.cnf
ormy.ini
).
[mysqld]
log_error = /var/log/mysql/error.log
- Restart the MySQL service to apply the changes.
sudo systemctl restart mysql
Key Points
- Set the error log destination with the server’s disk capacity in mind.
- Configure log rotation to automatically delete old logs, making management easier.
General Query Log Configuration
Configuration Steps
The general query log is disabled by default. To enable it, add the following settings.Example Configuration
- Edit the configuration file.
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/general.log
- Restart the MySQL service.
sudo systemctl restart mysql
Cautions
- Since the query log records every SQL statement, disk usage can increase rapidly. Disable it when not needed or set up rotation as appropriate.
Slow Query Log Configuration
Configuration Steps
The slow query log is useful for performance tuning, so it should be configured carefully.Example Configuration
- Edit the configuration file.
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
- Restart the MySQL service.
sudo systemctl restart mysql
Explanation
slow_query_log
: Enables the slow query log (1 for on, 0 for off).slow_query_log_file
: Destination for the log file.long_query_time
: Threshold (in seconds) for queries to be logged.
Additional Note
- Using the slow query analysis tool “pt-query-digest” makes log analysis easier.
Binary Log Configuration
Configuration Steps
The binary log is disabled by default, but enable it when using replication or backups.Example Configuration
- Edit the configuration file.
[mysqld]
log_bin = /var/log/mysql/mysql-bin
server_id = 1
binlog_format = ROW
expire_logs_days = 7
- Restart the MySQL service.
sudo systemctl restart mysql
Parameter Explanation
log_bin
: Specifies the binary log storage location.server_id
: Server identifier, required for replication setup.binlog_format
: Binary log recording format.ROW
records row-level changes.expire_logs_days
: Log retention period (in days). Old logs are automatically deleted.
Summary
These settings are crucial for MySQL operations. Configure the error log, general query log, slow query log, and binary log appropriately for their purposes, and use them for monitoring and optimization during operation.
4. Log File Storage Locations and Management
Verifying and Configuring Log File Storage Locations
Default Storage Locations
MySQL log files vary depending on the environment and installation method, but they are typically stored in the following locations.- Error log:
/var/log/mysql/error.log
- General query log:
/var/log/mysql/general.log
- Slow query log:
/var/log/mysql/slow.log
- Binary log:
/var/lib/mysql/mysql-bin.*
Customizing Storage Locations
To change the destination, edit the configuration file (my.cnf
or my.ini
). Example: Changing the Error Log Destination[mysqld]
log_error = /custom/log/mysql_error.log
After making changes, restart MySQL to apply them.sudo systemctl restart mysql
Key Points
- Set appropriate permissions on the destination directory so the MySQL user can write to it.
- Regularly monitor the disk space where log files are stored.
Log File Rotation Configuration
Rotation Configuration Example (using logrotate)
- Create or edit the configuration file.
sudo nano /etc/logrotate.d/mysql
- Add the following content.
/var/log/mysql/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 640 mysql mysql
postrotate
/usr/bin/mysqladmin flush-logs
endscript
}
Applying the Configuration
sudo logrotate -f /etc/logrotate.d/mysql
Best Practices for Disk Space Management
Commands to Check Disk Usage
du -sh /var/log/mysql
Check available space:df -h
Example of an Automatic Cleanup Script
Automatically delete log files older than 30 days.#!/bin/bash
find /var/log/mysql/*.log -mtime +30 -exec rm {} ;
Log File Encryption and Security Management
Permission Settings Example
chmod 640 /var/log/mysql/*.log
chown mysql:mysql /var/log/mysql/*.log
Encryption Example
You can encrypt logs using OpenSSL.openssl enc -aes-256-cbc -salt -in /var/log/mysql/general.log -out /var/log/mysql/general.log.enc
Summary
MySQL log files can be managed safely and efficiently by properly configuring storage locations, rotation settings, and disk space management. In particular, applying encryption and permission controls enhances security.5. How to Use Logs
Troubleshooting
Using Error Logs
Error logs help identify issues related to server startup and operation. Situation: MySQL server won’t start. Checking the error log:sudo tail -n 20 /var/log/mysql/error.log
Example log:2023-12-22T10:30:45.123456Z 0 [ERROR] [MY-013132] [Server] Plugin 'InnoDB' initialization failed.
2023-12-22T10:30:45.123456Z 0 [ERROR] [MY-010119] [Server] Aborting
Solution:- Check the InnoDB settings and, if corrupted, repair using recovery mode.
[mysqld]
innodb_force_recovery = 1
- After configuring, restart MySQL and attempt recovery.
Using the General Query Log
Situation: A specific user may be performing suspicious actions. Checking the query log:sudo tail -n 20 /var/log/mysql/general.log
Example log:2023-12-22T10:35:00.123456Z 10 Query SELECT * FROM users WHERE id=1;
2023-12-22T10:35:01.123456Z 11 Query DELETE FROM users WHERE id=1;
Solution:- Identify the suspicious user based on the operation history and enforce access restrictions.
- Review permission settings as needed.
Performance Tuning
Using the Slow Query Log
Situation: A specific page loads slowly. Checking the slow query log:sudo tail -n 20 /var/log/mysql/slow.log
Example log:# Time: 2023-12-22T10:40:00
# Query_time: 12.345 Lock_time: 0.001 Rows_sent: 1000 Rows_examined: 20000
SELECT * FROM orders WHERE status='pending';
Analysis and Optimization:- Review the query’s WHERE clause to reduce unnecessary row reads.
- Add indexes where appropriate.
ALTER TABLE orders ADD INDEX (status);
Using Additional Tools:- pt-query-digest: a tool for analyzing slow query logs.
pt-query-digest /var/log/mysql/slow.log
Security Auditing
Using Binary Logs
Situation: Data tampering due to unauthorized access is suspected. Checking the binary log:mysqlbinlog /var/lib/mysql/mysql-bin.000001 | grep 'UPDATE users'
Example log:# at 12345
#2023-12-22T10:45:00 server id 1 end_log_pos 234
UPDATE users SET status='active' WHERE id=1;
Countermeasure Examples:- Review the executed queries and trace source IPs and user IDs.
- Reevaluate permission management and password policies.
- Strengthen firewall settings and add IP restrictions.
- MySQL Enterprise Audit: enhances log auditing capabilities.
- Fail2Ban: automates detection and blocking of unauthorized access.
Conclusion
MySQL logs are essential tools for troubleshooting, performance improvement, and security hardening. Master practical uses such as diagnosing with error and query logs, optimizing slow queries, and tracking history with binary logs.
6. Points to Note and Best Practices
Considerations for Server Load Management
Impact of Logging on Load
Logging consumes system resources, so depending on the configuration it can add load to the server. In particular, the following settings should be handled carefully.- Enabling the General Query Log Since it records every query, it can cause performance degradation in high‑load environments. Enable it only when needed, or limit logging to specific queries.
- Setting the Slow Query Log Threshold If the threshold is set too low, a large volume of logs may be generated, so it’s important to configure it appropriately.
Conducting Load Tests
After changing log settings, run a load test to verify the impact on the system.mysqlslap --user=root --password=password --concurrency=50 --iterations=10 --query="SELECT * FROM test_table;" --create-schema=testdb
Key Points for Disk Space Management
Monitoring Capacity
Log files grow over time, so you need to monitor disk space regularly. Check Used Space:du -sh /var/log/mysql/
Check Free Space:df -h
Rotation and Automatic Cleanup
By configuring log rotation to automatically delete old log files, you can streamline space management./var/log/mysql/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 640 mysql mysql
postrotate
/usr/bin/mysqladmin flush-logs
endscript
}
Best Practices for Security Measures
Setting Access Restrictions
Since log files may contain sensitive information, set appropriate access restrictions.chmod 640 /var/log/mysql/*.log
chown mysql:mysql /var/log/mysql/*.log
Protection via Encryption
Encrypting log files reduces the risk of leakage. Encryption Example:openssl enc -aes-256-cbc -salt -in /var/log/mysql/general.log -out /var/log/mysql/general.log.enc
Importance of Backup and Recovery Plans
Perform Regular Backups
Include log files in regular backups so you have a recovery process in place for emergencies.mysqldump -u root -p --all-databases > backup.sql
Conduct Recovery Tests
Periodically test that backups can be restored successfully and document the procedures.Archive Management of Log Data
Need for Long‑Term Retention
Long‑term retention of log files may be required for audit purposes or legal compliance. In such cases, consider compressing them or storing them in cloud storage. Compression Example:tar -czvf mysql-logs.tar.gz /var/log/mysql/
Cloud Storage Transfer Example:aws s3 cp mysql-logs.tar.gz s3://your-bucket-name/
Conclusion
In MySQL log management, managing server load, disk space, security measures, and establishing backup and recovery plans are essential. By adopting these best practices, you can achieve stable operation and enhanced security.7. Summary
Reaffirming the Importance of Log Management
MySQL logs play crucial roles in many areas, as outlined below.- Troubleshooting You can use the error log to pinpoint the causes of server startup errors and configuration mistakes.
- Performance Tuning By leveraging the slow query log, you can identify queries that cause performance degradation and take corrective actions.
- Security Auditing Using the binary log, you can strengthen monitoring for unauthorized access and data tampering.
Practical Log Management Tips
Verify Basic Settings
- Set appropriate locations for error logs and query logs.
- Optimize the log output level to match system requirements.
Log Rotation and Compression Management
- Use automatic rotation settings to manage disk space while archiving older logs.
Security Measures and Backups
- Strengthen log file protection through permission management and encryption.
- Perform regular backups and restore tests to ensure rapid response during incidents.
Post-Configuration Verification Methods
Configuration Checklist
Below is a checklist to verify your configuration.- [ ] Have you enabled and configured the error log, query log, slow query log, and binary log?
- [ ] Is the log file storage location properly set and are access permissions correctly managed?
- [ ] Is the log rotation configured correctly and is disk space being managed?
- [ ] Have you implemented security measures such as encryption and access restrictions?
- [ ] Have backup and recovery procedures been tested in advance and confirmed to work reliably?
Key Points for Future Operations and Improvements
Regular Monitoring and Improvement
- Regularly review log file contents to monitor for anomalies or errors.
- Leverage new tools and security features to enhance the accuracy and efficiency of log management.
Adapting to New Features
- Continuously review new features and configuration options introduced with MySQL upgrades, and update settings as needed.
Summary and Next Steps
MySQL log management is a fundamental yet powerful tool that can be flexibly customized to enhance the reliability of database operations. Use this article as a reference to apply it in your real-world operations. Next Steps- Consider adopting log analysis tools and monitoring systems to further optimize configurations and deepen log analysis.
- Apply log settings in real projects and systems to aid troubleshooting and performance improvements.