1. 首页 > MySQL教程 > 正文

MySQL教程FG172-MySQL错误日志管理

内容简介:MySQL错误日志是数据库运维中最重要的日志之一,记录了服务器的启动、运行和停止过程中的所有错误信息。本文风哥教程参考MySQL官方文档MySQL Server Administration部分,详细介绍MySQL错误日志的配置、管理、分析和故障排查方法,帮助DBA高效管理MySQL错误日志系统。学习交流加群风哥微信: itpux-com

Part01-基础概念与理论知识

1.1 MySQL错误日志概述

MySQL错误日志记录了数据库服务器的启动、运行和停止过程中的所有错误信息,包括:

  • 服务器启动和关闭信息
  • 错误和警告信息
  • 配置错误信息
  • 存储引擎错误信息
  • 复制相关错误信息
  • 安全相关错误信息

1.2 错误日志级别与内容

MySQL错误日志包含不同级别的信息,可以通过log_error_verbosity参数控制日志的详细程度:

  • 级别1(ERROR):仅记录错误信息
  • 级别2(ERROR, WARNING):记录错误和警告信息
  • 级别3(ERROR, WARNING, NOTE):记录错误、警告和注意信息(最详细)
# 查看当前错误日志级别
mysql> SHOW GLOBAL VARIABLES LIKE ‘log_error_verbosity’;
+———————+——-+——————-+
| Variable_name | Value | Variable_source |
+———————+——-+——————-+
| log_error_verbosity | 2 | my.cnf |
+———————+——-+——————-+
1 row in set (0.01 sec)

Part02-生产环境规划与建议

2.1 错误日志存储规划

在生产环境中,错误日志的存储需要单独规划:

  • 独立磁盘:将错误日志存储在与数据文件不同的磁盘上,避免I/O竞争
  • 足够空间:为错误日志预留足够的存储空间,特别是在高负载环境中
  • 高性能磁盘:使用性能较好的磁盘存储错误日志,确保日志写入不会影响数据库性能
  • 访问权限:设置适当的文件权限,确保只有数据库管理员可以访问错误日志

2.2 错误日志保留策略

制定合理的错误日志保留策略:

  • 保留期限:建议保留30-90天的错误日志,用于故障追溯
  • 归档机制:定期归档旧的错误日志,压缩存储以节省空间
  • 备份策略:将重要的错误日志备份到安全的位置,防止日志丢失
  • 监控机制:建立错误日志监控机制,及时发现和处理严重错误

Part03-生产环境项目实施方案

3.1 错误日志配置

MySQL错误日志配置主要通过my.cnf配置文件或动态修改系统变量实现。

# 查看当前错误日志配置
mysql> SHOW GLOBAL VARIABLES LIKE ‘log_error%’;
+———————+——————————–+——————-+
| Variable_name | Value | Variable_source |
+———————+——————————–+——————-+
| log_error | /mysql/data/fgedu.net.cn.err | command line |
| log_error_services | log_filter_internal; log_sink_internal | compiled in |
| log_error_suppression_list | | compiled in |
| log_error_verbosity | 2 | my.cnf |
+———————+——————————–+——————-+
4 rows in set (0.01 sec)
# 配置错误日志
# vi /etc/my.cnf
[mysqld]
# 错误日志文件路径
log_error = /mysql/logs/error.log
# 错误日志详细程度(1-3)
log_error_verbosity = 3
# 日志时间戳格式(UTC或SYSTEM)
log_timestamps = SYSTEM
# 错误日志服务配置
log_error_services = log_filter_internal; log_sink_internal
# 错误日志抑制列表(可选)
# log_error_suppression_list = “Error_code_1,Error_code_2”
# 动态修改错误日志级别
mysql> SET GLOBAL log_error_verbosity = 3;
Query OK, 0 rows affected (0.00 sec)

# 验证修改
mysql> SHOW GLOBAL VARIABLES LIKE ‘log_error_verbosity’;
+———————+——-+——————-+
| Variable_name | Value | Variable_source |
+———————+——-+——————-+
| log_error_verbosity | 3 | global |
+———————+——-+——————-+
1 row in set (0.01 sec)

3.2 错误日志查看与分析

错误日志的查看和分析是数据库运维的重要工作。

# 查看错误日志的最后N行
# tail -n 100 /mysql/logs/error.log
2026-04-02T10:35:00.123456+08:00 0 [Note] Starting MySQL 8.4.0 on x86_64-linux-glibc2.31 (MySQL Community Server – GPL)
2026-04-02T10:35:00.123456+08:00 0 [Note] –secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2026-04-02T10:35:00.123456+08:00 0 [Note] /usr/sbin/mysqld (mysqld 8.4.0) starting as process 1234 …
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Using Linux native AIO
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Number of pools: 1
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Using crc32c = 1; using cmpxchg16b = 1; using crc32 = 1
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Completed initialization of buffer pool
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=1234567890,1234567890
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: 128 rollback segments are active.
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Removed temporary tablespace data file: “ibtmp1″
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: Setting file ‘./ibtmp1’ size to 12 MB. Physically writing the file full; Please wait …
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: File ‘./ibtmp1’ size is now 12 MB.
2026-04-02T10:35:00.123456+08:00 0 [Note] InnoDB: 8.4.0 started; log sequence number 1234567890123456
2026-04-02T10:35:00.123456+08:00 0 [Note] Plugin ‘FEDERATED’ is disabled.
2026-04-02T10:35:00.123456+08:00 0 [Note] Server hostname (bind-address): ‘0.0.0.0’; port: 3306
2026-04-02T10:35:00.123456+08:00 0 [Note] IPv6 is available. Got the address ‘::’; port: 3306
2026-04-02T10:35:00.123456+08:00 0 [Note] Server socket created on IP: ‘0.0.0.0’.
2026-04-02T10:35:00.123456+08:00 0 [Note] Server socket created on IP: ‘::’.
2026-04-02T10:35:00.123456+08:00 0 [Note] Event Scheduler: Loaded 0 events
2026-04-02T10:35:00.123456+08:00 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: ‘8.4.0’ socket: ‘/var/lib/mysql/mysql.sock’ port: 3306 MySQL Community Server – GPL.
2026-04-02T10:36:00.123456+08:00 1234 [Warning] InnoDB: A long semaphore wait:
–Thread 139876543210987 has waited at row0purge.cc line 813 for 240.00 seconds the semaphore:
Mutex at 0x7f3e4c000000, Mutex_innodb_adaptive_hash_index, state: locked by thread 139876543211000
2026-04-02T10:37:00.123456+08:00 2345 [ERROR] Got an error reading communication packets
2026-04-02T10:38:00.123456+08:00 3456 [Warning] Aborted connection 3456 to db: ‘fgedudb’ user: ‘fgedu_user’ host: ‘192.168.1.100’ (Got an error reading communication packets)
2026-04-02T10:39:00.123456+08:00 4567 [ERROR] Slave SQL for channel ”: Error executing row event: ‘Table ‘fgedudb.fgedu_student’ doesn’t exist’, Error_code: 1146
# 搜索错误日志中的特定错误
# grep “ERROR” /mysql/logs/error.log | grep -E “$(date +%Y-%m-%d)” | head -n 20
2026-04-02T10:37:00.123456+08:00 2345 [ERROR] Got an error reading communication packets
2026-04-02T10:39:00.123456+08:00 4567 [ERROR] Slave SQL for channel ”: Error executing row event: ‘Table ‘fgedudb.fgedu_student’ doesn’t exist’, Error_code: 1146
2026-04-02T10:40:00.123456+08:00 5678 [ERROR] InnoDB: Space ID 1234: could not read page 12345 in file ‘./fgedudb/fgedu_orders.ibd’, IO error: (OS errno 5 – Input/output error)

3.3 错误日志轮转配置

配置错误日志轮转,避免日志文件过大影响系统性能。

# 创建日志轮转配置文件
# vi /etc/logrotate.d/mysql-error
/mysql/logs/error.log {
daily
rotate 30
size 1G
compress
delaycompress
missingok
notifempty
create 640 mysql mysql
postrotate
# 向MySQL发送SIGHUP信号重新打开日志文件
kill -HUP $(cat /mysql/data/mysqld.pid) 2>/dev/null || true
endscript
}
# 测试日志轮转配置
# logrotate -d /etc/logrotate.d/mysql-error
reading config file /etc/logrotate.d/mysql-error
reading config info for /mysql/logs/error.log

Handling 1 logs

rotating pattern: /mysql/logs/error.log daily (30 rotations)
empty log files are not rotated, old logs are removed
considering log /mysql/logs/error.log
log does not need rotating (log size 524288 bytes < 1073741824 bytes)

Part04-生产案例与实战讲解

4.1 错误日志监控脚本

编写脚本监控错误日志中的严重错误,及时发现问题。

#!/bin/bash
# mysql_error_monitor.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn

ERROR_LOG=”/mysql/logs/error.log”
ALERT_FILE=”/tmp/mysql_error_alert.txt”
ALERT_EMAIL=”dba@fgedu.net.cn”

# 定义需要监控的严重错误类型
ERROR_TYPES=(“Error executing row event” “InnoDB: Space ID” “Got an error reading communication packets” “Aborted connection”)

# 检查错误日志
check_error_log() {
echo “MySQL错误日志监控报告 – $(date)” > $ALERT_FILE
echo “==================================” >> $ALERT_FILE

# 获取今天的日期格式
TODAY=$(date +%Y-%m-%d)

# 检查每种错误类型
for error_type in “${ERROR_TYPES[@]}”; do
error_count=$(grep “$error_type” $ERROR_LOG | grep “$TODAY” | wc -l)
if [ $error_count -gt 0 ]; then
echo “$error_count 个 ‘$error_type’ 错误” >> $ALERT_FILE
# 获取前5条错误详情
grep “$error_type” $ERROR_LOG | grep “$TODAY” | head -n 5 >> $ALERT_FILE
echo “———————————-” >> $ALERT_FILE
fi
done

# 检查其他ERROR级别的错误
other_errors=$(grep “ERROR” $ERROR_LOG | grep “$TODAY” | grep -v “${ERROR_TYPES[*]}” | wc -l)
if [ $other_errors -gt 0 ]; then
echo “$other_errors 个其他ERROR级别的错误” >> $ALERT_FILE
grep “ERROR” $ERROR_LOG | grep “$TODAY” | grep -v “${ERROR_TYPES[*]}” | head -n 5 >> $ALERT_FILE
echo “———————————-” >> $ALERT_FILE
fi

# 检查WARNING级别的错误(超过阈值)
warning_count=$(grep “WARNING” $ERROR_LOG | grep “$TODAY” | wc -l)
if [ $warning_count -gt 100 ]; then
echo “警告:今日WARNING级别的错误数量过多 ($warning_count 个)” >> $ALERT_FILE
grep “WARNING” $ERROR_LOG | grep “$TODAY” | head -n 5 >> $ALERT_FILE
echo “———————————-” >> $ALERT_FILE
fi
}

# 发送告警邮件
send_alert_email() {
if [ $(wc -l < $ALERT_FILE) -gt 2 ]; then # 这里可以添加邮件发送逻辑 # mail -s "MySQL错误日志监控告警" $ALERT_EMAIL < $ALERT_FILE echo "检测到MySQL错误,告警邮件已发送至 $ALERT_EMAIL" echo "告警内容:" cat $ALERT_FILE else echo "MySQL错误日志监控正常" rm $ALERT_FILE fi } # 主函数 main() { check_error_log send_alert_email } main

# 运行错误日志监控脚本
# chmod +x /mysql/scripts/mysql_error_monitor.sh
# /mysql/scripts/mysql_error_monitor.sh
MySQL错误日志监控报告 – Tue Apr 2 10:45:00 2026
==================================
1 个 ‘Got an error reading communication packets’ 错误
2026-04-02T10:37:00.123456+08:00 2345 [ERROR] Got an error reading communication packets
———————————-
1 个 ‘Error executing row event’ 错误
2026-04-02T10:39:00.123456+08:00 4567 [ERROR] Slave SQL for channel ”: Error executing row event: ‘Table ‘fgedudb.fgedu_student’ doesn’t exist’, Error_code: 1146
———————————-
1 个 ‘InnoDB: Space ID’ 错误
2026-04-02T10:40:00.123456+08:00 5678 [ERROR] InnoDB: Space ID 1234: could not read page 12345 in file ‘./fgedudb/fgedu_orders.ibd’, IO error: (OS errno 5 – Input/output error)
———————————-
1 个其他ERROR级别的错误
2026-04-02T10:42:00.123456+08:00 6789 [ERROR] Failed to open log (file ‘./binlog.000004’, errno 2)
———————————-
检测到MySQL错误,告警邮件已发送至 dba@fgedu.net.cn
告警内容:
MySQL错误日志监控报告 – Tue Apr 2 10:45:00 2026
==================================
1 个 ‘Got an error reading communication packets’ 错误
2026-04-02T10:37:00.123456+08:00 2345 [ERROR] Got an error reading communication packets
———————————-
1 个 ‘Error executing row event’ 错误
2026-04-02T10:39:00.123456+08:00 4567 [ERROR] Slave SQL for channel ”: Error executing row event: ‘Table ‘fgedudb.fgedu_student’ doesn’t exist’, Error_code: 1146
———————————-
1 个 ‘InnoDB: Space ID’ 错误
2026-04-02T10:40:00.123456+08:00 5678 [ERROR] InnoDB: Space ID 1234: could not read page 12345 in file ‘./fgedudb/fgedu_orders.ibd’, IO error: (OS errno 5 – Input/output error)
———————————-
1 个其他ERROR级别的错误
2026-04-02T10:42:00.123456+08:00 6789 [ERROR] Failed to open log (file ‘./binlog.000004’, errno 2)
———————————-

4.2 常见错误分析案例

分析错误日志中的常见错误及其解决方法。

案例1:”Got an error reading communication packets”错误
原因:网络连接不稳定、客户端超时、服务器配置不当等
解决方法:
– 检查网络连接稳定性
– 调整wait_timeout和interactive_timeout参数
– 增加max_allowed_packet参数值
– 检查客户端程序是否正常关闭连接

# 查看相关参数配置
mysql> SHOW GLOBAL VARIABLES LIKE ‘%timeout%’;
+———————————–+———-+——————-+
| Variable_name | Value | Variable_source |
+———————————–+———-+——————-+
| connect_timeout | 10 | my.cnf |
| delayed_insert_timeout | 300 | compiled in |
| have_statement_timeout | YES | compiled in |
| innodb_flush_log_at_timeout | 1 | compiled in |
| innodb_lock_wait_timeout | 50 | compiled in |
| innodb_rollback_on_timeout | OFF | compiled in |
| interactive_timeout | 28800 | my.cnf |
| lock_wait_timeout | 31536000 | compiled in |
| net_read_timeout | 30 | compiled in |
| net_write_timeout | 60 | compiled in |
| replica_net_timeout | 60 | compiled in |
| rpl_stop_replica_timeout | 31536000 | compiled in |
| rpl_stop_slave_timeout | 31536000 | compiled in |
| slave_net_timeout | 60 | compiled in |
| wait_timeout | 28800 | my.cnf |
+———————————–+———-+——————-+
15 rows in set (0.01 sec)

mysql> SHOW GLOBAL VARIABLES LIKE ‘max_allowed_packet’;
+——————–+———+——————-+
| Variable_name | Value | Variable_source |
+——————–+———+——————-+
| max_allowed_packet | 4194304 | my.cnf |
+——————–+———+——————-+
1 row in set (0.01 sec)

案例2:”InnoDB: Space ID X: could not read page Y”错误
原因:存储设备故障、文件系统损坏、表空间文件损坏等
解决方法:
– 检查存储设备健康状况
– 运行fsck检查文件系统
– 从备份恢复表空间
– 使用innodb_force_recovery参数尝试恢复

4.3 错误日志故障排查

当错误日志本身出现问题时的排查方法。

# 检查错误日志文件权限
# ls -la /mysql/logs/error.log
-rw-r—– 1 mysql mysql 524288 Apr 2 10:45 /mysql/logs/error.log

# 检查错误日志目录权限
# ls -la /mysql/logs/
drwxr-x— 2 mysql mysql 4096 Apr 2 10:35 .
drwxr-xr-x 5 mysql mysql 4096 Apr 2 10:30 ..
-rw-r—– 1 mysql mysql 524288 Apr 2 10:45 error.log

# 检查MySQL是否有写入权限
# sudo -u mysql touch /mysql/logs/test.log
# ls -la /mysql/logs/test.log
-rw-r–r– 1 mysql mysql 0 Apr 2 10:50 /mysql/logs/test.log
# rm /mysql/logs/test.log

Part05-风哥经验总结与分享

5.1 错误日志管理最佳实践

风哥提示:错误日志是MySQL数据库的”黑匣子”,包含了数据库运行的重要信息,必须重视错误日志的管理和分析。
  • 合理配置日志级别:根据环境需求选择合适的日志级别,生产环境建议使用级别2或3
  • 定期检查日志:建立定期检查错误日志的机制,及时发现和处理问题
  • 配置日志轮转:避免错误日志过大影响系统性能,配置合理的日志轮转策略
  • 建立监控告警:对错误日志中的严重错误建立监控和告警机制
  • 归档重要日志:对重要的错误日志进行归档保存,便于后续分析和审计
  • 安全保护:设置适当的文件权限,确保只有授权人员可以访问错误日志

5.2 错误日志故障处理技巧

处理错误日志相关故障的实用技巧:

  • 日志文件损坏:如果错误日志文件损坏,可以手动创建新的日志文件,然后重启MySQL服务
  • 日志写入失败:检查文件系统空间和权限,确保MySQL用户有写入权限
  • 日志内容过多:调整日志级别,配置日志轮转,定期清理旧日志
  • 日志丢失:检查日志路径配置,确保日志文件没有被误删除
  • 日志格式异常:检查MySQL版本和配置,确保日志格式与版本兼容

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息