MySQLやMariaDBは標準ではログローテートされない

MySQLやMariaDBをインストールすると、ログローテートの設定ファイルが設置されるものの、すべてコメントアウトされていてローテートは実行されません。

これはログローテートを行う際に、ログの書き込みファイルを新しいファイルに変更する(flush-logs)には、mysqladminのroot権限の設定が必要だからです。

ログローテートの設定方法は、標準で設置されるログローテートの設定ファイルのコメントに記載がされています。

ログローテートを実行する方法

/root/.my.cnfを作成し、mysqladminの実行権限を記載します。

# vi /root/.my.cnf

[mysqladmin]
password = xxxxxxxx
user= root

/root/.my.cnfをrootのみ読み込み可能にします。

# chmod 600 /root/.my.cnf

/etc/logrotate.d/mariadb(MySQLなら/etc/logrotate.d/mysqld)のログローテート部分のコメントアウトを外します。

ログローテートの確認

デバッグモードで実行します。

# logrotate -dv /etc/logrotate.d/mariadb

権限のエラーでローテートされない場合

flush-logsで権限ファイルを指定します。

if test -x /usr/bin/mysqladmin && \
   /usr/bin/mysqladmin ping &>/dev/null
then
   /usr/bin/mysqladmin flush-logs
fi

if test -x /usr/bin/mysqladmin && \
   /usr/bin/mysqladmin ping &>/dev/null
then
   /usr/bin/mysqladmin --defaults-extra-file=/root/.my.cnf flush-logs
fi

強制的にログの書き込みファイルを新しいファイルに変更する(flush-logs)には

/usr/bin/mysqladmin --defaults-extra-file=/root/.my.cnf flush-logs

ログローテートのログを出力させる方法

ログローテートが正しく実行されない場合は、ログを出力させたりして調査をします。

ログを出力させるには次のようにします。
# vi /etc/cron.daily/logrotate

/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
 ↓
/usr/sbin/logrotate -v /etc/logrotate.conf >/var/log/logrotate 2>&1

/var/log/logrotateにログが出力されます。

mysqladminの実行権限が間違っていると次のようなログが出力されます。
# cat /var/log/logrotate

running postrotate script
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
error: error running non-shared postrotate script for /var/log/mysql/error.log of '/var/log/mysql/error.log '

標準で設置されるログローテートの設定ファイル

ログローテートの設定ファイルは、MariaDBであれば、次のようになっています。

/etc/logrotate.d/mariadb

# This logname can be set in /etc/my.cnf
# by setting the variable "log-error"
# in the [mysqld_safe] section as follows:
#
# [mysqld_safe]
# log-error=/var/log/mariadb/mariadb.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = <secret>
# user= root
#
# where "<secret>" is the password.
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !

# Then, un-comment the following lines to enable rotation of mysql's log file:

#/var/log/mariadb/mariadb.log {
#        create 640 mysql mysql
#        notifempty
#       daily
#        rotate 3
#        missingok
#        compress
#    postrotate
#       # just if mysqld is really running
#       if test -x /usr/bin/mysqladmin && \
#          /usr/bin/mysqladmin ping &>/dev/null
#       then
#          /usr/bin/mysqladmin flush-logs
#       fi
#    endscript
#}
This logname can be set in /etc/my.cnf by setting the variable "log-error" in the [mysqld_safe] section as follows: このログ名は、[mysqld_safe]セクションの変数 "log-error"を次のように設定することで/etc/my.cnfに設定できます。
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
If the root user has a password you have to create a /root/.my.cnf configuration file with the following content: rootユーザーにパスワードがある場合は、次の内容で/root/.my.cnf構成ファイルを作成する必要があります。
[mysqladmin]
password = <secret>
user= root
where "<secret>" is the password. 「<secret>」はパスワードです。
ATTENTION: This /root/.my.cnf should be readable ONLY for root ! 注意:この/root/.my.cnfはrootだけが読めるはずです。
Then, un-comment the following lines to enable rotation of mysql's log file: 次に、mysqlのログファイルのローテーションを有効にするために次の行のコメントを外します。
#/var/log/mariadb/mariadb.log {
#        create 640 mysql mysql #ローテーション後に空のログファイルを新規作成。
#        notifempty #ログファイルが空ならローテーションしない
#       daily #ログを毎日ローテーションする
#        rotate 3 #ローテーションする回数を指定
#        missingok #ログファイルが存在しなくてもエラーを出さずに処理を続行
#        compress #ローテーションしたログをgzipで圧縮
#    postrotate #postrotateとendscriptの間に記述されたコマンドをログローテーション後に実行
#       # just if mysqld is really running
#       if test -x /usr/bin/mysqladmin && \
#          /usr/bin/mysqladmin ping &>/dev/null #MySQLサーバが起動中であれば
#       then
#          /usr/bin/mysqladmin flush-logs #flush-logsを実行
#       fi
#    endscript
#}

関連記事

スポンサーリンク

for-each-ref

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る