MariaDB 在 Linux 的日志管理实操指南
一 日志类型与用途
二 启用与配置常用日志
[mariadb]
log_error = /var/log/mysql/error.log
log_warnings = 2
SHOW GLOBAL VARIABLES LIKE 'log_error';
sudo journalctl -u mariadb -f
[mariadb]
log_output = TABLE
general_log = 1
查询:SELECT * FROM mysql.general_log;[mariadb]
log_output = FILE
general_log = 1
general_log_file = /var/log/mysql/queries.log
SET GLOBAL general_log = 1; -- 开启
SET GLOBAL general_log = 0; -- 关闭
[mariadb]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
log_slow_verbosity = full
[mariadb]
log_bin = /var/log/mysql/mysql-bin
binlog_format = ROW
server_id = 1
sync_binlog = 1
max_binlog_size = 1073741824
expire_logs_days = 7
提示:开启 binlog 会带来一定性能开销;ROW 格式更利于精确恢复与闪回。三 日志轮转与清理
PURGE BINARY LOGS TO 'mysql-bin.000063';
PURGE BINARY LOGS BEFORE '2025-12-01 00:00:00';
RESET MASTER;
/var/log/mysql/*.log {
daily
rotate 7
missingok
compress
delaycompress
notifempty
create 640 mysql mysql
sharedscripts
postrotate
systemctl reload mariadb >/dev/null 2>&1 || true
endscript
}
sudo logrotate -vf /etc/logrotate.d/mysql
SET GLOBAL server_audit_file_rotate_size = 1073741824; -- 1GB
SET GLOBAL server_audit_file_rotations = 10;
SET GLOBAL server_audit_file_rotate_now = ON;
四 查看与分析日志
sudo tail -f /var/log/mysql/error.log
sudo journalctl -u mariadb -f
sudo tail -f /var/log/mysql/queries.log
grep -i "SELECT\|UPDATE" /var/log/mysql/queries.log
sudo mysqldumpslow -s t /var/log/mysql/slow.log
mysqlbinlog /var/log/mysql/mysql-bin.000001 | mysql -u root -p
mysqlbinlog mysql-bin.000001 --start-datetime='2025-12-01 10:00:00' \
--stop-datetime='2025-12-01 10:05:00' | mysql -u root -p
mysqlbinlog --no-defaults mysql-bin.000001 | mysql -u root -p
SHOW VARIABLES LIKE 'log_%';
SHOW BINARY LOGS;
SHOW MASTER STATUS;
SHOW BINLOG EVENTS IN 'mysql-bin.000001' LIMIT 10\G;
FLUSH LOGS;
五 安全与性能建议