Tenken blog

Linux环境下nginx+php+mysql日志分割

由于nginx没带日志分割功能,access.log和error.log会随时间越来越大,后续不好分析和管理,如果网站的流量很大的话,还可能会爆磁盘。现在主流的做法有两种:1、用Linux发行版系统自带的logrotate日志分割功能,2、自己写脚本定时分割日志。对于不懂shell、python的人来说,自己动手写脚本的难度会高好多,现在我们就用第一种方法来实现nginx+mysql+php的日志分割。

nginx日志分割

在搞日志分割之前,首先要弄清楚nginx的日志目录,nginx.pid的路径(nginx启动进程通信号)。
下面以我服务器作为例子
nginx日志目录:/var/log/nignx
nginx.pid :/var/run/nginx/nginx.pid
在/etc/logrotate.d/目录下建一个nginx文件
vi /etc/logrotate.d/nginx

1
2
3
4
5
6
7
8
9
10
11
12
/var/log/nginx/*.log {
daily
##dateext
missingok
rotate 30
notifempty
create
sharedscripts
postrotate
[ -f /var/run/nginx/nginx.pid ] && kill -USR1 `cat /var/run/nginx/nginx.pid`
endscript
}

配置说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
###日志文件每天进行滚动 
daily
###以时间日期的格式作为后缀,如:error.log-20170416
dateext
###如果找不到这个log档案,就忽略过去
missingok
###保留最近30次滚动的日志(保存30天)
rotate
###通过gzip压缩转储以后的日志(这个可以不用压缩)
compress
###和compress一起使用时,转储的日志文件到下一次转储时才压缩
delaycompress
###如果是空文件的话,不转储
notifempty
###转储文件,使用指定的文件模式创建新的日志文件,如 create 640 nginx nginx
create mode owner group
###运行postrotate脚本(该脚本作用为让nginx重新生成日志文件)
sharedscripts
###在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript

mysql日志分割

vi /etc/logrotate.d/mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/var/log/mysql/*.log {
daily
missingok
rotate 30
notifempty
create
sharedscripts
postrotate
if test -x /usr/local/mysql/bin/mysqladmin && /usr/local/mysql/bin/mysqladmin ping &>/dev/null
then
/usr/local/mysql/bin/mysqladmin flush-logs
fi
endscript
}

MySQL的日志分割脚本在MySQL的安装目录下的/support-files/mysql-log-rotate
例如我的MySQL:
/usr/local/mysql/support-files/mysql-log-rotate
打开mysql-log-rotate文件可以看到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# The log file name and location can be set in
# /etc/my.cnf by setting the "log-error" option
# in either [mysqld] or [mysqld_safe] section as
# follows:
#
# [mysqld]
# log-error=/usr/local/mysql/data/mysqld.log
#
# In case the root user has a password, then 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: The /root/.my.cnf file should be readable
# _ONLY_ by root !

/usr/local/mysql/data/mysqld.log {
# create 600 mysql mysql
notifempty
daily
rotate 5
missingok
compress
postrotate
# just if mysqld is really running
if test -x /usr/local/mysql/bin/mysqladmin && \
/usr/local/mysql/bin/mysqladmin ping &>/dev/null
then
/usr/local/mysql/bin/mysqladmin flush-logs
fi
endscript
}

从上面可以看到,如果MySQL的root账号有密码的话就需要在/root/下面建一个.my.cnf文件,内容为:

1
2
3
[mysqladmin]
password = <secret> ##root的密码
user= root

并且设置权限:chmod 600 /root/.my.cnf
注意:经过我测试,加了dateext后MySQL日志分割之后就不会在原来的日志文件上继续写,而是停留在分割后的日志文件上,这个不知道为啥,去掉dataext之后又正常了,nginx、php-fpm这两个正常,但是还是去掉dateext选项好,反正都是以时间为后缀的

php-fpm日志分割

vi /etc/logrotate.d/php-fpm

1
2
3
4
5
6
7
8
9
10
11
12
/var/log/php/*.log {
daily
##dateext
missingok
rotate 30
notifempty
create
sharedscripts
postrotate
[ -f /usr/local/php/var/run/php-fpm.pid ] && kill -USR1 `cat /usr/local/php/var/run/php-fpm.pid`
endscript
}

测试日志分割

logrotate测试命令:
logrotate -f /etc/logrotate.d/nginx
logrotate -f /etc/logrotate.d/php-fpm
logrotate -f /etc/logrotate.d/mysql

请我吃辣条吧~~