阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL的logrotate脚本

148次阅读
没有评论

共计 1423 个字符,预计需要花费 4 分钟才能阅读完成。

随着业务量的增加和时间的推移 MySQL 的日志势必会越来越多、越来越大, 那么就要用系统的 logrotate 轮替 mysql 的日志(logrotate 可以参看: http://www.linuxidc.com/Linux/2017-02/140261.htm),当然平时用的 mysql 都是二进制包部署安装的,其实 mysql 的二进制包中的 support-files 文件夹里就有提供 mysql 的 logrotate 脚本,文件名为:mysql-log-rotate,当然如果要使用的话还是要修改下才能使用,如下是我修改过的 mysql 的 logrotate 脚本:

# This logname can be set in /etc/my.cnf
# by setting the variable “err-log”
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/usr/local/mysql/data/mysqld.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 !
 
# 这里日志文件为 datadir 下的 *.log 和 *.err 文件
/data/mysqldata/*.log /data/mysqldata/*.err {
        create 600 mysql mysql
        notifempty# 如果日志为空 logrotate 不会进行
      daily
        rotate 3
        missingok# 日志 logrotate 期间任何错误都忽略
        compress
    postrotate# 在 logrotate 之前要判断 mysql 是否启动,如果没有启动就 flush-logs
      # 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
}
[root@localhost support-files]# cp mysql-log-rotate /etc/logrotate.d/mysqld

把 logrotate 文件复制在 logrotate.d 文件夹里,这里主要一点是如果你的 mysql 的 root 用户有加密码,那么你要在 root 的家目录中新建.my.cnf 添加以下内容, 使得 mysqladmin 能用:

[root@localhost ~]# vim .my.cnf
[mysqladmin]
user = root
host = <youhost>
password = <youpassword>

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-02/141146.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计1423字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中