目次

スローログの自動ローテート

アカウント情報の設定フィルを作成

# vi /root/.my.cnf
[client]
user = root
password = rootpassword

rootしかアクセスできないように権限設定

# chown root:root /root/.my.cnf
# chmod 600 /root/.my.cnf

実行テスト

# mysql --defaults-extra-file=.my.cnf -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ibdata             |
| mysql              |
| nagios             |
| performance_schema |
+--------------------+

ローテートの設定ファイル作成

# vi /etc/logrotate.d/mysql-slow-rotate
/var/log/mysql/mysql-slow.log {
        create 644 mysql mysql
        notifempty
        daily
        rotate 14
        missingok
        nocompress
        dateext
    prerotate
        /usr/bin/mysqldumpslow -s t /var/log/mysql/mysql-slow.log 2>/dev/null | \
        mail -s "slowlog" clown@example.com
    endscript
    postrotate
        /usr/bin/mysql --defaults-extra-file=/root/.my.cnf -e "flush slow logs"
    endscript
}

Command

# logrotate --help                                                       
使い方: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation

Help options:
  -?, --help                Show this help message
  --usage                   Display brief usage message

ローテートのテスト

# logrotate -dv /etc/logrotate.d/mysql-slow-rotate 
reading config file /etc/logrotate.d/mysql-slow-rotate
reading config info for /var/log/mysql/mysql-slow.log 

Handling 1 logs

rotating pattern: /var/log/mysql/mysql-slow.log  after 1 days (14 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/mysql/mysql-slow.log
  log does not need rotating
not running postrotate script, since no logs were rotated
# logrotate -df /etc/logrotate.d/mysql-slow-rotate 
reading config file /etc/logrotate.d/mysql-slow-rotate
reading config info for /var/log/mysql/mysql-slow.log 

Handling 1 logs

rotating pattern: /var/log/mysql/mysql-slow.log  forced from command line (14 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/mysql/mysql-slow.log
  log needs rotating
rotating log /var/log/mysql/mysql-slow.log, log->rotateCount is 14
glob finding old rotated logs failed
running prerotate script
running script with arg /var/log/mysql/mysql-slow.log: "
        /usr/bin/mysqldumpslow -s t /var/log/mysql/mysql-slow.log 2>/dev/null | \
        mail -s "slowlog" clown@example.com
"
renaming /var/log/mysql/mysql-slow.log to /var/log/mysql/mysql-slow.log-20131211
creating new log mode = 0644 uid = 27 gid = 27
running postrotate script
running script with arg /var/log/mysql/mysql-slow.log: "
        /usr/bin/mysql --defaults-extra-file=.my.cnf -e "flush slow logs"
"