Understanding and Configuring MySQL Server Logging

Introduction to Database Logging Mechanisms

MySQL employs a modular logging architecture to capture operational events, diagnose runtime failures, monitor query efficiency, and support disaster recovery. Each log subsystem serves a distinct purpose within the server lifecycle, and cnofiguring them correctly is fundamental to maintaining a stable, high-performance database environment.

Core Log File Categories

1. Error Log

Tracks critical failures, severe warnings, and daemon startup/shutdown sequences during mysqld execution. By default, the logging feature is disabled and errors route to standard error (stderr). Activation requires explicitly defining the log-error directive. The output defaults to <hostname>.err inside the data directory. Administrators can safely rotate the file by executing FLUSH LOGS, which archives the active file with an .old extension and initializes a fresh stream.

2. Binary Log (Binlog)

Essential for point-in-time recovery and asynchronous replication. Records all data-altering transactions (INSERT, UPDATE, DELETE, DDL) in a compact binary format, preserving execution timestamps, resource metrics, and transactional metadata. The feature is disabled by default and requires log-bin. Files rotate automatically when max_binlog_size is reached. Large transactions may temporarily exceed this limit to preserve atomicity. The accompanying .index file maintains absolute paths, enabling background threads to locate historical streams. Filtering via binlog-do-db or binlog-ignore-db evaluates the current connection context (set via USE), not the target schema referenced in the query. Cross-database updates bypass these filters unless the session context matches.

3. General Query Log

Audits every statement received by the server, including SELECT operations. This comprehensive tracking generates significant disk I/O and degrades throughput. It is intended strictly for transient debugging or compliance auditing. Controlled via general_log and general_log_file.

4. Slow Query Log

Identifies statements exceeding a defined execution threshold. Entries are written after query completion; for real-time blocking, lock contention, or live process inspection, rely on SHOW FULL PROCESSLIST instead. Key configuration directives include:

  • slow_query_log: Toggles the feature.
  • long_query_time: Threshold in seconds. Excludes lock wait duration.
  • slow_query_log_file: Target filesystem path.
  • log_queries_not_using_indexes: Captures full table scans regardless of duration.
  • log_throttle_queries_not_using_indexes: Rate-limits duplicate entries per minute.
  • min_examined_row_limit: Ignores queries scanning fewer rows than specified.
  • log_slow_admin_statements: Includes ALTER TABLE, CHECK TABLE, etc.
  • log_slow_slave_statements: Logs delayed replica operations when statement-based binlog is active.

5. InnoDB Redo Log

Ensures durability and crash recovery by writing physical page modifications to disk before dirty buffers are flushed to data files. Works in tandem with undo segments to guarantee ACID compliance. Storage location and file count are managed via innodb_log_group_home_dir and innodb_log_files_in_group.

6. Legacy Update Log

A text-based precursor to the binary log. Removed in MySQL 5.0 due to redundancy and performance limitations. Modern deployments rely exclusively on binlog.

Configuration and Activation

Below is a consolidated configuration snippet demonstrating how to initialize these subsystems with production-grade parameters. Values and paths have been adjusted for a standardized deployment layout.

[mysqld]
# System Diagnostics
log-error = /srv/database/runtime/service_failure.err

# Transaction Stream & Replication
log-bin = /mnt/storage/mysql/archives/binlog_primary
log-bin-index = /mnt/storage/mysql/archives/binlog_primary.index
binlog-format = ROW
binlog-checksum = NONE
binlog-cache-size = 8M
max-binlog-cache-size = 8G
max-binlog-size = 2G
binlog-expire-logs-seconds = 86400
sync-binlog = 1
innodb-flush-log-at-trx-commit = 1

# Full Query Audit (High Overhead)
general-log = ON
general-log-file = /tmp/audit_query_trace.log

# Replica Relay Processing
relay-log = /mnt/storage/mysql/relay/slave_relay
relay-log-index = /mnt/storage/mysql/relay/slave_relay.index
relay-log-recovery = ON
relay-log-purge = ON

# Performance Threshold Monitoring
slow-query-log = ON
slow-query-log-file = /var/log/mysql/bottleneck_analysis.log
long-query-time = 0.05
log-queries-not-using-indexes = ON
log-throttle-queries-not-using-indexes = 150
min-examined-row-limit = 250
log-slow-admin-statements = ON
log-slow-slave-statements = ON

For secure log rotation without service interruption, administrators can utilize filesystem operations combined with an explicit flush command:

TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
ARCHIVE_DIR="/opt/mysql_archives"
SOURCE_PATH="/var/log/mysql/bottleneck_analysis.log"

mkdir -p "${ARCHIVE_DIR}"
cp "${SOURCE_PATH}" "${ARCHIVE_DIR}/slow_backup_${TIMESTAMP}.log"
truncate -s 0 "${SOURCE_PATH}"
mysql -u dba_admin --password=strong_pass -S /run/mysqld/mysqld.sock -e "FLUSH SLOW LOGS;"

Primary Applications for Binary Logging

  • Asynchronous Replication: Propagates data changes from a primary instance to multiple replicas, enabling horizontal scaling, read offloading, and geographic distribution.
  • Read/Write Splitting Architectures: Binlog streams feed dedicated read nodes, isolating transactional workloads from analytical queries.
  • Point-in-Time Recovery: Reconstructs database state by replaying transaction events up to a specific timestamp, effectively reversing accidental deletions or corruption.
  • Cross-System Synchronization: Maintains eventual consistency between relational storage and downstream systems like Elasticsearch, message queues, or caching layers.
  • Multi-Region Active-Active Topologies: Facilitates bidirectional data synchronization across distributed data centers.

Tags: mysql-server-logs binary-log-configuration slow-query-log innodb-redo-log database-replication

Posted on Sat, 18 Jul 2026 17:17:20 +0000 by phpfre@k*