MySQL 错误日志:查看与排查故障指南

MySQL 错误日志:查看与排查故障指南

概述

错误日志(Error Log)是 MySQL 最重要的排障日志,记录了 MySQL 服务在启动、运行和关闭过程中发生的错误、警告和重要事件。当数据库出问题时,错误日志通常是第一个需要查看的地方。

错误日志记录的内容

1. 启动和关闭信息

2026-05-18T08:00:00.123456+08:00 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 1234
2026-05-18T08:00:02.456789+08:00 0 [System] [MY-010229] [Server] Starting XA crash recovery...
2026-05-18T08:00:02.789012+08:00 0 [System] [MY-010230] [Server] XA crash recovery finished.
2026-05-18T08:00:03.123456+08:00 0 [System] [MY-010931] [InnoDB] Starting crash recovery...
2026-05-18T08:00:04.567890+08:00 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) ready for connections.

2. 连接和认证错误

2026-05-18T10:30:00.123456+08:00 42 [Warning] [MY-010055] [Server] IP address '192.168.1.100' could not be resolved: Name or service not known
2026-05-18T10:31:00.789012+08:00 50 [Note] [MY-010926] [Server] Access denied for user 'root'@'192.168.1.100' (using password: YES)

3. InnoDB 错误

2026-05-18T11:00:00.123456+08:00 0 [ERROR] [MY-012681] [InnoDB] Transaction cannot allocate memory from the memory pool
2026-05-18T11:05:00.456789+08:00 0 [ERROR] [MY-012678] [InnoDB] Cannot allocate memory for the buffer pool

4. 复制错误

2026-05-18T12:00:00.123456+08:00 100 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': error connecting to master 'repl@192.168.1.10:3306' - retry-time: 60  retries: 1
2026-05-18T12:01:00.456789+08:00 100 [ERROR] [MY-013124] [Repl] Slave SQL for channel '': The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has a purged binary log.

5. 死锁警告

2026-05-18T13:00:00.123456+08:00 0 [Note] [MY-012907] [InnoDB] Transactions deadlock detected:
------------------------
LATEST DETECTED DEADLOCK
------------------------
2026-05-18 13:00:00 0x7f1234abc700
*** (1) TRANSACTION:
TRANSACTION 12345, ACTIVE 10 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s)

(完整的死锁信息还包含两个事务的锁状态和 SQL)

配置错误日志

# 错误日志文件路径
log_error = /var/log/mysql/error.log

# 错误日志级别(MySQL 8.0)
log_error_verbosity = 3
# 1 = ERROR 级别
# 2 = ERROR + WARNING 级别
# 3 = ERROR + WARNING + NOTE 级别(最详细)

# 记录告警到错误日志(MySQL 8.0)
log_warnings = 2

查看错误日志

# 实时跟踪错误日志
tail -f /var/log/mysql/error.log

# 查找最近 50 行 ERROR(重点关注)
grep -i "ERROR" /var/log/mysql/error.log | tail -50

# 查找特定时间段的错误
grep "2026-05-18T10:" /var/log/mysql/error.log | grep -i "ERROR"

# 查找 InnoDB 相关错误
grep -i "InnoDB.*ERROR" /var/log/mysql/error.log

# 统计各类错误数量
grep -oP '\[MY-\d+\]' error.log | sort | uniq -c | sort -rn | head -10

常见错误及含义

错误码 含义 常见原因
MY-010055 IP 解析失败 DNS 反向解析问题,skip_name_resolve 可解决
MY-010926 访问被拒绝 用户名或密码错误,或主机不允许连接
MY-012681 InnoDB 内存分配失败 Buffer Pool 太大或系统内存不足
MY-010584 复制连接失败 网络不通、授权错误、主库未启动
MY-012907 死锁检测 事务并发冲突,需检查应用 SQL 顺序
MY-010737 表损坏 硬件故障或异常关闭,需 REPAIR TABLE
MY-013114 主键冲突 主从复制中主键冲突,需 slave_skip_errors 或修复
MY-011022 binlog 文件无法创建 磁盘空间不足或权限问题

排障流程示例

场景:MySQL 突然无法连接

# 1. 检查错误日志
tail -100 /var/log/mysql/error.log

# 看到:
# [ERROR] [MY-012681] [InnoDB] Cannot allocate memory for the buffer pool
# [ERROR] [MY-010737] [Server] Out of memory

# 2. 检查内存
free -m
# 发现内存不足,Buffer Pool 太大

# 3. 修改配置
# 调整 innodb_buffer_pool_size 为可用内存的 50%
# 重启 MySQL

场景:主从复制中断

# 1. 查看错误日志
grep -i "repl" /var/log/mysql/error.log

# 看到:
# [ERROR] [MY-010584] Slave I/O: error connecting to master

# 2. 检查网络
ping master_host

# 3. 检查复制账号
mysql -u repl -p -h master_host -P 3306

# 4. 查看复制状态
SHOW SLAVE STATUS\G

最佳实践

# 生产推荐配置
log_error = /data/mysql/error.log
log_error_verbosity = 3
log_warnings = 2

# 建议配置日志轮转
# 使用 logrotate 每日切割
# /etc/logrotate.d/mysql
/var/log/mysql/error.log {
    daily
    rotate 30
    compress
    missingok
    notifempty
    postrotate
        /usr/bin/mysqladmin flush-logs
    endscript
}

面试要点

  1. 错误日志是 MySQL 排障的第一入口——出了事先看 error.log
  2. 日志级别:MySQL 8.0 使用 log_error_verbosity 控制详细程度(1-3)
  3. 常见问题:无法启动(权限/配置)、连接拒绝(认证/网络)、死锁、主从中断
  4. 不要忽略 Warning:很多 Warning 是潜在的 Error
  5. 日志轮转很重要:避免日志撑爆磁盘
  6. 配合其他日志:慢查询日志看性能,错误日志看故障
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容