You are here

MariaDB Log Rotation

Modern Linux Systems have a mechanism called logrotate to rotate different log files.

The general configuration file is located under /etc/logrotate.conf and specific changes are under /etc/logrotate.d/

By default the logrotate job is started once a day by a O/S cron.daily job: /etc/cron.daily/logrotate

Because the default log rotation configuration does not exactly what I want I have adapted it a bit:

First I need a database user for log rotation:

CREATE USER 'logrotate'@'localhost' IDENTIFIED BY 'secret';
GRANT RELOAD ON *.* to 'logrotate'@'localhost';

This user needs a password file:

#
# ~/.logrotate.cnf (chmod 600)
#

[mysql]
user     = logrotate
password = secret

And this is my log rotation configuration file:

#
# /etc/logrotate.d/mysql
#

/var/log/mysql/error.log {
  compress
  notifempty
  size 100M
  rotate 5
  maxage 365
  postrotate
    /usr/bin/mysql --defaults-extra-file=~/.logrotate.cnf --execute='FLUSH ERROR LOGS'
  endscript
}

/var/log/mysql/slow.log {
  compress
  notifempty
  weekly
  rotate 4
  maxage 30
  postrotate
    /usr/bin/mysql --defaults-extra-file=~/.logrotate.cnf --execute='FLUSH SLOW LOGS'
  endscript
}

/var/log/mysql/general.log {
  compress
  notifempty
  size 1G
  rotate 3
  maxage 14
  postrotate
    /usr/bin/mysql --defaults-extra-file=~/.logrotate.cnf --execute='FLUSH GENERAL LOGS'
  endscript
}

Comments

/usr/sbin/logrotate /etc/logrotate.conf
Shinguzcomment

cd /var/log
ll -d -Z mysql* mysql/*
mkdir mysql
chown mysql: mysql
semanage fcontext -a -t mysqld_db_t "/var/log/mysql/(/.*)?"
restorecon -Rv /var/log/mysql
ll -d -Z mysql* mysql/*

And Non-standard database set up with SELinux

Shinguzcomment