ubuntu

Ubuntu系统中MariaDB的日志管理

小樊
44
2025-05-26 05:04:59
栏目: 云计算

在Ubuntu系统中,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;

日志分析与监控

查看日志

使用journalctl命令查看日志:

journalctl --vacuum-size=100M  # 清理日志文件,使其总大小不超过100MB
journalctl --vacuum-time=2weeks  # 清理两周前的日志

使用tail命令实时查看日志文件:

tail -f /var/log/mysql/mariadb-slow.log

使用lessmore命令分页查看日志文件:

less /var/log/mysql/mariadb-slow.log

通过以上方法,你可以有效地管理和监控Ubuntu系统中的MariaDB日志。

0
看了该问题的人还看了