SoFunction
Updated on 2025-05-23

Detailed explanation of MySQL log system error log, slow query log, and binary log

Preface

Logging systems play a crucial role in MySQL, not only helping us monitor the operating status of the system and troubleshoot errors, but also provide strong support for query performance tuning and data recovery. This article will introduce in detail the three major log types in MySQL:Error log, slow query log and binary log, discuss their functions, configuration methods and practical application scenarios, and help you better understand and use the log system to improve database operation and maintenance efficiency.

1. Error log

1.1 Functions and Characteristics

Error logs are mainly used to record errors, warnings and other important information during the database startup, operation and stopping process. It is of great significance to troubleshooting and system diagnosis, and common records include:

  • Start and stop information
  • Various errors (such as connection errors, permission errors)
  • Warning information (such as configuration problems, insufficient resources)

1.2 Configuration and use

  • Default location: The error log will be automatically created when MySQL starts, and the location and name can be found in the configuration file.Passedlog-errorInstruction specification. For example:
    [mysqld]
    log-error = /var/log/mysql/
    
  • Real-time monitoring: Operations and maintenance personnel can use real-time monitoring tools or commands (such astail -f) Check the error log to facilitate timely discovery and solve problems.

2. Slow query logs

2.1 Functions and characteristics

Slow query logs are used to record SQL statements whose execution time exceeds a preset threshold, helping developers and DBAs analyze and optimize query performance. By slow query logs, you can:

  • Identify performance bottlenecks
  • Adjust index and query logic
  • Evaluate the performance of the system under high loads

2.2 Configuration and use

  • Enable slow query log:existEnable slow query logs in   and set thresholds. For example:
    [mysqld]
    slow_query_log = 1
    slow_query_log_file = /var/log/mysql/
    long_query_time = 2
    
    The above configuration indicates that all queries that have been executed for more than 2 seconds are recorded.
  • Analytical Tools: Tools such asmysqldumpslowor Percona’spt-query-digestConduct statistical analysis of log content to find the most time-consuming queries and common performance problems.

3. Binary log

3.1 Functions and characteristics

Binary Log is an important log that records all data modification operations in the database, mainly used for:

  • Data replication: In the master-slave replication architecture, the slave server synchronizes data by parsing binary logs.
  • Data recovery: When failing to recover, the database can be restored to the specified point in time (Point-In-Time Recovery, PITR) using binary logs.
  • Audit and debugging: Record all DML and some DDL operations to help track data changes and debug problems.

3.2 Configuration and use

  • Enable binary logging:existPassedlog-binThe instruction enables binary logs and specifies the log file prefix:
    [mysqld]
    log-bin = mysql-bin
    server-id = 1
    binlog_format = ROW
    
    Note: When binary logging is enabled, MySQL will automatically record all modification operations to the data.
  • Log format: Common log formats include STATEMENT, ROW and MIXED, where the ROW format records changes in each row of data and is suitable for complex replication scenarios.
  • Log Management: Binary logs may occupy a lot of disk space and need to be cleaned regularly. MySQL providesPURGE BINARY LOGSThe command is used to delete old logs:
    PURGE BINARY LOGS BEFORE '2025-01-01 00:00:00';
    

4. Comprehensive application of log system

4.1 Performance optimization and troubleshooting

  • Error logHelps to promptly detect system exceptions and configuration problems.
  • Slow query logGuide developers to optimize SQL queries in a targeted manner to improve overall performance.
  • Binary logEnsure data security, support replication and recovery mechanisms, and provide a data foundation for high database availability and disaster recovery.

4.2 Safety and compliance

The logging system also plays a key role in security auditing, recording user operations and data modifications, which facilitates post-review and security incident investigation.

4.3 Log monitoring and automation management

Using third-party log monitoring systems (such as ELK, Prometheus, etc.), various MySQL logs can be collected and monitored in real time, and responded to abnormal events in a timely manner through the alarm system.

5. Summary

The MySQL log system is an indispensable part of database management. Various log types have their own emphasis, which together constitute a complete monitoring, tuning and recovery system. By rationally configuring and utilizing error logs, slow query logs and binary logs, it can not only help us quickly locate problems and optimize query performance, but also provide strong support for data replication and disaster recovery. I hope this article can provide you with practical reference and guidance in the database operation and maintenance and performance tuning process!

This is the article about error logs, slow query logs, and binary logs in MySQL log system. For more related contents of MySQL log system, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!