在Ubuntu系统中,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日志。