在Ubuntu系统中,MariaDB的日志管理可以通过多种方式进行,包括配置日志类型、查看日志、清理日志以及使用日志管理工具。以下是详细的步骤和方法:
MariaDB日志类型
- 错误日志:记录服务器启动、运行和关闭过程中的错误信息。
- 查询日志:记录所有到达MariaDB的查询。
- 慢查询日志:记录执行时间超过指定阈值的查询。
- 二进制日志:记录所有更改数据的语句,用于复制和时间点恢复。
- 审计日志:记录服务器活动,用于安全审计。
- 中继日志:在复制环境中使用,从主服务器接收的二进制日志事件。
日志配置
错误日志配置
查看当前错误日志配置:
SHOW VARIABLES LIKE 'log_error';
在my.cnf/my.ini
中配置:
[mysqld] log_error = /var/log/mysql/mariadb-error.log log_error_verbosity = 3
通用查询日志配置
临时启用通用查询日志:
SET GLOBAL general_log = 'ON'; SET GLOBAL general_log_file = '/var/log/mysql/mariadb-query.log';
永久配置(my.cnf
):
[mysqld] general_log = 1 general_log_file = /var/log/mysql/mariadb-query.log general_log_output = FILE
慢查询日志配置
配置慢查询日志:
SET GLOBAL slow_query_log = 'ON'; SET GLOBAL long_query_time = 2; SET GLOBAL slow_query_log_file = '/var/log/mysql/mariadb-slow.log'; SET GLOBAL log_queries_not_using_indexes = 'ON';
在my.cnf
中配置:
[mysqld] slow_query_log = 1 slow_query_log_file = /var/log/mysql/mariadb-slow.log long_query_time = 2 log_queries_not_using_indexes = 1
二进制日志配置
二进制日志基本配置:
SET GLOBAL log_bin = 'ON'; SET GLOBAL binlog_format = 'ROW'; SET GLOBAL expire_logs_days = 7;
在my.cnf
中配置:
[mysqld] log_bin = /var/log/mysql/mariadb-bin binlog_format = ROW expire_logs_days = 7 sync_binlog = 1
日志轮转与管理
使用logrotate
创建配置文件:
/etc/logrotate.d/mariadb { daily rotate 7 missingok compress delaycompress notifempty create 640 mysql mysql postrotate if test -x /usr/bin/mysqladmin && \ /usr/bin/mysqladmin ping &/dev/null; then /usr/bin/mysqladmin flush-logs fi endscript }
手动轮转日志:
mysqladmin flush-logs
或通过SQL:
FLUSH LOGS;
日志分析与监控
- 慢查询分析工具:使用
mysqldumpslow
分析慢查询日志。 - 二进制日志分析:使用
mysqlbinlog
查看二进制日志内容。
查看日志
使用journalctl
命令查看日志:
journalctl --vacuum-size=100M # 清理日志文件,使其总大小不超过100MB journalctl --vacuum-time=2weeks # 清理两周前的日志
使用tail
命令实时查看日志文件:
tail -f /var/log/mysql/mariadb-slow.log
使用less
或more
命令分页查看日志文件:
less /var/log/mysql/mariadb-slow.log
通过以上方法,你可以有效地管理和监控Ubuntu系统中的MariaDB日志。