我使用CentOS5,我有以下版本的mysql:
mysql Ver 14.12 Distrib 5.0.77,用于使用readline 5.1的redhat-linux-gnu(i686)
我想使用logrotate旋转mysql的日志。
让我们考虑一下,我只想旋转文件/var/lib/mysql/mysql-log.log作为例子。 我在下面描述了我所遵循的过程,我已经注意到的logrotate行为,然后给出了我的configuration文件的内容。
我遵循这些步骤:
我注意到的行为:
我现在给你我的onfig文件,而日志的提取产生了我关心mysql的命令logrotate。
/etc/logrotate.conf中
# see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs rotate 4 # create new (empty) log files after rotating old ones create # uncomment this if you want your log files compressed #compress # RPM packages drop log rotation information into this directory include /etc/logrotate.d # no packages own wtmp -- we'll rotate them here /var/log/wtmp { monthly minsize 1M create 0664 root utmp rotate 1 } # system-specific logs may be also be configured here.
/etc/logrotate.d/mysql
/var/lib/mysql/mysql-log.log { rotate 4 daily nocompress missingok create 660 mysql mysql postrotate if ls /proc/`cat /var/run/mysqld/mysqld.pid` > /dev/null then /usr/bin/mysqladmin -pMotDePasseSqlRoot flush-logs fi endscript }
这里是我注意到有关mysql的logrotate日志:
reading config file mysql reading config info for /var/lib/mysql/mysql-log.log ... rotating pattern: /var/lib/mysql/mysql-log.log forced from command line (4 rotations) empty log files are rotated, old logs are removed considering log /var/lib/mysql/mysql-log.log log needs rotating rotating log /var/lib/mysql/mysql-log.log, log->rotateCount is 4 renaming /var/lib/mysql/mysql-log.log.4 to /var/lib/mysql/mysql-log.log.5 (rotatecount 4, logstart 1, i 4), renaming /var/lib/mysql/mysql-log.log.3 to /var/lib/mysql/mysql-log.log.4 (rotatecount 4, logstart 1, i 3), renaming /var/lib/mysql/mysql-log.log.2 to /var/lib/mysql/mysql-log.log.3 (rotatecount 4, logstart 1, i 2), renaming /var/lib/mysql/mysql-log.log.1 to /var/lib/mysql/mysql-log.log.2 (rotatecount 4, logstart 1, i 1), renaming /var/lib/mysql/mysql-log.log.0 to /var/lib/mysql/mysql-log.log.1 (rotatecount 4, logstart 1, i 0), renaming /var/lib/mysql/mysql-log.log to /var/lib/mysql/mysql-log.log.1 creating new log mode = 0660 uid = 27 gid = 27 running postrotate script running script with arg /var/lib/mysql/mysql-log.log : " if ls /proc/`cat /var/run/mysqld/mysqld.pid` > /dev/null then /usr/bin/mysqladmin -pMotDePasseSqlRoot flush-logs fi " removing old log /var/lib/mysql/mysql-log.log.5
你有什么想法为什么logrotates报告一个不实际的行动? 我在定义我的文件/etc/logrotated.d/mysql之前还是之后错过了一个步骤?
谢谢你的帮助 !
西尔
Threre没有旋转,因为你正在使用-d选项。 从手册:
-d打开debugging模式并隐含-v。 在debugging模式下, 不会对日志或logrotate状态文件进行更改。
我有两个build议:
-pPassword参数,最好使用--defaults-extra-file=path_to_extra ,它只能由root用户读取。 当有人访问服务器时执行ps -ef | grep mysql ps -ef | grep mysql ,它会看到数据库的root密码。 这里是我在CentOS 5上使用mysql的logrotate /etc/logrotate.d/mysql脚本(mysql Ver 14.12 Distrib 5.0.77,用于使用readline 5.1的redhat-linux-gnu(x86_64)):
# If the root user has a password you have to create a # /.my.cnf configuration file with the following # content: # # [mysqladmin] # password = <secret> # user = <username> # # where "<secret>" is the password and <username> is user. # # ATTENTION: This /.my.cnf should be readable ONLY # for root ! /var/log/mysqld.log { create 640 mysql mysql notifempty missingok postrotate # just if mysqld is really running if test -x /usr/bin/mysqladmin && \ /usr/bin/mysqladmin --defaults-extra-file=/root/mysql/logrotate.cnf ping &>/dev/null then /usr/bin/mysqladmin --defaults-extra-file=/root/mysql/logrotate.cnf flush-logs fi /usr/bin/chcon -u system_u -r object_r -t mysqld_log_t /var/log/mysqld.log endscript }
这适用于--defaults-extra-file=/root/mysql/logrotate.cnf选项,我在这里:
[client] user = root password = TheRootPassword
从我的初始命令中删除-dv选项后,我可以testing旋转。
我一直无法确定守护进程的-HUP是否会导致它刷新并重新打开它的日志。
这里有另一个参考(我试图澄清,提示一个简单的HUP页面的另一个URL将工作): http : //www.mysqlperformanceblog.com/2007/12/09/be-careful-rotating-mysql-logs/评论页-1 /#评论-1043963
YMMV,因为我不是一个MySQL的人,但如果这是正确的,你的logrotate可以像所有其他有礼貌的应用程序一样HUP的过程。