天天看点

mySQL Slow Query Log Rotation(慢查询日志轮循设置)

要分析公司网站mysql性能,常规my.cnf调优是一方面,慢查日志分析也重要。

但现在日志太大了,作一个每天自动切割的任务。将脚本放在/etc/logrotate.d/。

如何分析导出日志,以后再写。

~~~~~~~~~~~~~~~~~~

/var/log/xxx_mysql_slow.log {

    daily

    rotate 5

    missingok

    delaycompress

    create 0640 mysql mysql

    # skip 'notifempty'

    postrotate

        mycnf_file=/root/.my.cnf

        mysqladmin=/usr/bin/mysqladmin

        if test -x $mysqladmin && \

                $mysqladmin --defaults-file="$mycnf_file" ping >/dev/null

        then

            $mysqladmin --defaults-file="$mycnf_file" flush-logs

        fi

    endscript

}

# intentionally not rotating mysqld.log

~~~~~~~~~

相关英文文档:

<a href="http://administratosphere.wordpress.com/2011/12/02/log-rotation-for-mysql/">http://administratosphere.wordpress.com/2011/12/02/log-rotation-for-mysql/</a>

<a href="http://blog.oneiroi.co.uk/mysql/mysql-slow-query-log-rotation/">http://blog.oneiroi.co.uk/mysql/mysql-slow-query-log-rotation/</a>

以下两篇文档,用于了解centos linux的crontab和logrotate的,看完了大致对过程就清楚了。

<a href="http://yaksayoo.blog.51cto.com/510938/162062/">http://yaksayoo.blog.51cto.com/510938/162062/</a>

   cron把命令行保存在/etc/crontab文件里,每个系统用户如果设置了自己的cron,那都会在/var/spool/cron下面有对应用户名的crontab。无论编写/var/spool/cron目录内的文件还是/etc/crontab文件,都能让cron准确无误地执行安排的任务,区别是/var/spool/cron下各系统用户的crontab文件是对应用户级别的的任务配置,而/var/crontab文件则是对应系统级别的任务配置。cron服务器每分钟读取一次/var/crontab/cron目录内的所有文件和/etc/crontab文件。

<a href="http://5iqiong.blog.51cto.com/2999926/790908">http://5iqiong.blog.51cto.com/2999926/790908</a>

logrotate是作为linux系统日志的管理工具存在。他可以轮换,压缩,邮件系统日志文件。

默认的logrotate被加入cron的/etc/cron.daily中作为每日任务执行。

/etc/logrotate.conf为其默认配置文件指定每个日志文件的默认规则。

/etc/logrotate.d/* 为/etc/logrotate.conf默认包含目录其中文件也会被logrotate读取。指明每个日志文件的特定规则。

/var/lib/logrotate/statue中默认记录logrotate上次轮换日志文件的时间。

在debian下,在/etc/cron.daily/中会存在一个sysklogd任务。这个每日执行任务由cron启动,

任务会轮换系统默认日志文件。默认日志文件会由syslogd-listfiles命令给出。同时会killall -hup syslogd重启syslog服务。

在redhat下,会在/etc/logrotate.d/下会存在一个syslog任务,这个任务由logrotate启动,

也会轮换系统默认日志文件。同时重启syslog服务。

。。。。

include /etc/logrotate.d 告诉 logrotate 读入存放在/etc/logrotate.d 目录中的日志转储参数,当系统中安装了rpm 软件包时,使用include 选项十分有用。rpm 软件包的日志转储参数一般存放在/etc/logrotate.d 目录。

include 选项十分重要,一些应用把日志转储参数存放在 /etc/logrotate.d 。

典型的应用有:apache, linuxconf, samba, cron 以及syslog。

这样,系统管理员只要管理一个 /etc/logrotate.conf 文件就可以了。

当 /etc/logrotate.conf 读入文件时,include 指定的文件中的转储参数将覆盖缺省的参数,