MySQL Logging
Logging is a critical aspect of MySQL administration that allows you to monitor database activities, troubleshoot issues, investigate security breaches, and maintain an audit trail of operations. In this guide, we'll explore the various types of MySQL logs, how to configure them, and how to use them effectively for database management.
Why is MySQL Logging Important?
Proper logging practices help you:
- Identify and solve performance issues
- Track unauthorized access attempts
- Debug application errors
- Comply with audit requirements
- Recover from disasters using binary logs
- Monitor database operations
Types of MySQL Logs
MySQL provides several types of logs, each serving different purposes:
- Error Log - Records problems encountered during server startup, operation, and shutdown
- General Query Log - Records all client connections and SQL statements received
- Binary Log - Records data-modifying statements for replication and recovery
- Slow Query Log - Records queries that exceed a specified execution time
- Relay Log - Used in replication by replica servers
- DDL Log - Records metadata operations
Let's explore each of these in detail.
Error Log
The error log contains information about server startup and shutdown processes, critical errors, and warnings. It's crucial for troubleshooting MySQL server issues.
Configuration
The error log location is controlled by the log_error
system variable:
-- Check current error log path
SHOW VARIABLES LIKE 'log_error';
Output:
+---------------+---------------------+
| Variable_name | Value |
+---------------+---------------------+
| log_error | /var/log/mysql/error.log |
+---------------+---------------------+
You can configure the error log in the my.cnf
configuration file:
[mysqld]
log_error = /var/log/mysql/mysql-error.log
log_error_verbosity = 3
The log_error_verbosity
determines what gets logged:
- 1 = Errors only
- 2 = Errors and warnings
- 3 = Errors, warnings, and notes (most verbose)
Viewing the Error Log
You can directly view the error log using standard file viewing tools:
sudo tail -f /var/log/mysql/error.log
Example error log entries:
2023-06-15T10:23:45.123456Z 0 [Note] MySQL Server started
2023-06-15T11:34:56.789012Z 0 [Warning] IP address '192.168.1.100' could not be resolved
2023-06-15T12:45:12.345678Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
General Query Log
The general query log records all SQL statements received from clients. It's helpful for debugging application issues but can generate large files and impact performance.
Configuration
Enable the general query log:
-- Check if general log is enabled
SHOW VARIABLES LIKE 'general_log%';
-- Enable general log (dynamic)
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log_file = '/var/log/mysql/mysql-query.log';
Permanent configuration in my.cnf
:
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/mysql-query.log
Sample General Log Output
2023-06-15T14:25:10.123456Z 42 Connect webapp_user@localhost on webapp_db using TCP/IP
2023-06-15T14:25:10.234567Z 42 Query SELECT * FROM users WHERE active = 1
2023-06-15T14:25:11.345678Z 42 Query UPDATE users SET last_login = NOW() WHERE id = 123
2023-06-15T14:25:12.456789Z 42 Quit
Binary Log
The binary log contains "events" that describe database changes (INSERT, UPDATE, DELETE, etc.) and is stored in a binary format. It's essential for:
- Point-in-time recovery
- Replication
- Auditing data changes
Configuration
Enable binary logging:
-- Check binary log status
SHOW VARIABLES LIKE 'log_bin%';
-- Check binary log format
SHOW VARIABLES LIKE 'binlog_format';
Configure in my.cnf
:
[mysqld]
log_bin = /var/log/mysql/mysql-bin.log
binlog_format = ROW
max_binlog_size = 100M
expire_logs_days = 14
Binary log formats:
- STATEMENT: Logs SQL statements (smallest size, potential replication issues)
- ROW: Logs changes to individual rows (most reliable, larger size)
- MIXED: Uses statement-based by default, switches to row-based when needed