首页 数据库 mysql教程 [Linux] 利用logrotate对MySQL日志进行轮转_MySQL

[Linux] 利用logrotate对MySQL日志进行轮转_MySQL

Jun 01, 2016 pm 01:28 PM
linux

bitsCN.com

[Linux] 利用logrotate对MySQL日志进行轮转

 

日志轮转特别适用于具有固定文件名的日志文件,比如MySQL的出错日志、常规查询日志、慢查询日志等。Linux系统有一个非常好用的根据logratate可以实现自动轮转,本文介绍它的原理和用法。

默认情况下,logratate部署为每天运行的cron job,你可以在目录/etc/cron.daily里找到名为logratate的配置文件。那么它是在每天的上面时候运行的呢?打开文件/etc/crontab就知道了,下面是我机器上的情况:

[plain] 

SHELL=/bin/bash  

PATH=/sbin:/bin:/usr/sbin:/usr/bin  

MAILTO=root  

HOME=/  

  

# run-parts  

01 * * * * root run-parts /etc/cron.hourly  

02 4 * * * root run-parts /etc/cron.daily  

22 4 * * 0 root run-parts /etc/cron.weekly  

42 4 1 * * root run-parts /etc/cron.monthly  

从上面的配置我们可以知道,/etc/cron.daily是在每天凌晨4:02执行。也就是说,每天4:02分/etc/cron.daily/logratate将会自动执行,下面是它的内容:

[plain] 

#!/bin/sh  

  

/usr/sbin/logrotate /etc/logrotate.conf  

EXITVALUE=$?  

if [ $EXITVALUE != 0 ]; then  

    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"  

fi  

exit 0  

从上面我们可以知道,logratate默认的配置文件是/etc/logratate.conf,下面是它的内容:

[plain] 

EXITVALUE=$?  

if [ $EXITVALUE != 0 ]; then  

    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"  

fi  

exit 0  

[root@lx202 /etc/cron.daily ]# cat /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  

}  

  

/var/log/btmp {  

    missingok  

    monthly  

    minsize 1M  

    create 0600 root utmp  

    rotate 1  

}  

从上面我们可以知道,这个默认的配置文件将读取目录/etc/logrotate.d,所以我们只要把自己写的配置文件放到该目录下即可。

MySQL本省提供了一个rotate的参考配置文件,在support-files目录下,文件名为mysql-log-rotate,内容如下:

[plain] 

# 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=/opt/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 =  

# user= root  

#  

# where "" is the password.  

#  

# ATTENTION: This /root/.my.cnf should be readable ONLY  

# for root !  

  

/opt/mysql/data/mysqld.log {  

        # create 600 mysql mysql  

        notifempty  

        daily  

        rotate 3  

        missingok  

        compress  

    postrotate  

        # just if mysqld is really running  

        if test -x /opt/mysql/bin/mysqladmin && /  

           /opt/mysql/bin/mysqladmin ping &>/dev/null  

        then  

           /opt/mysql/bin/mysqladmin flush-logs  

        fi  

    endscript  

}  

logrotate常见选项:

选项 含义

compress 压缩日志文件的所有非当前版本

copy 复制当前的日志文件,忽略create参数

copytruncate 复制当前的日志文件,并置空当前文件

daily 每天轮日志文件i

dateext 轮换的日志后缀为-YYYYMMDD格式

delaycompress 压缩除了当前和最近之外的所有其他版本

missingok 如果日志不存在,不会报错

notifempty 如果日志为空,则不轮换

rotate n 在轮换方案中包含n个版本的日志

size=logsize 如果日志文件大于logsize才轮换

我们只要根据自己的需要,修改相应配置即可,下面是一个例子:

1)创建MySQL root密码文件

vi /root/.my.cnf

[plain] 

[mysqladmin]  

password = ***  

user= root  

chmod 600 /root/.my.cnf

2)把mysql-log-rotate拷贝至/etc/logrotate.d目录下,修改其内容为:

[plain] 

/data/mysql/log/slow.log  

/data/mysql/log/alert.log {  

        create 600 mysql mysql  

        notifempty  

        daily  

        rotate 7  

        missingok  

        # compress  

    postrotate  

        # just if mysqld is really running  

        if test -x /opt/mysql/bin/mysqladmin && /  

           /opt/mysql/bin/mysqladmin ping &>/dev/null  

        then  

           /opt/mysql/bin/mysqladmin flush-logs  

        fi  

    endscript  

}  

3)执行以下命令测试

[plain] 

/usr/sbin/logrotate -f /etc/logrotate.d/mysql-log-rotate  

 

bitsCN.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1664
14
CakePHP 教程
1423
52
Laravel 教程
1318
25
PHP教程
1269
29
C# 教程
1248
24
Linux体系结构:揭示5个基本组件 Linux体系结构:揭示5个基本组件 Apr 20, 2025 am 12:04 AM

Linux系统的五个基本组件是:1.内核,2.系统库,3.系统实用程序,4.图形用户界面,5.应用程序。内核管理硬件资源,系统库提供预编译函数,系统实用程序用于系统管理,GUI提供可视化交互,应用程序利用这些组件实现功能。

git怎么查看仓库地址 git怎么查看仓库地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 仓库地址,请执行以下步骤:1. 打开命令行并导航到仓库目录;2. 运行 "git remote -v" 命令;3. 查看输出中的仓库名称及其相应的地址。

vscode上一步下一步快捷键 vscode上一步下一步快捷键 Apr 15, 2025 pm 10:51 PM

VS Code 一步/下一步快捷键的使用方法:一步(向后):Windows/Linux:Ctrl ←;macOS:Cmd ←下一步(向前):Windows/Linux:Ctrl →;macOS:Cmd →

notepad怎么运行java代码 notepad怎么运行java代码 Apr 16, 2025 pm 07:39 PM

虽然 Notepad 无法直接运行 Java 代码,但可以通过借助其他工具实现:使用命令行编译器 (javac) 编译代码,生成字节码文件 (filename.class)。使用 Java 解释器 (java) 解释字节码,执行代码并输出结果。

sublime写好代码后如何运行 sublime写好代码后如何运行 Apr 16, 2025 am 08:51 AM

在 Sublime 中运行代码的方法有六种:通过热键、菜单、构建系统、命令行、设置默认构建系统和自定义构建命令,并可通过右键单击项目/文件运行单个文件/项目,构建系统可用性取决于 Sublime Text 的安装情况。

Linux的主要目的是什么? Linux的主要目的是什么? Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服务器操作系统,2.嵌入式系统,3.桌面操作系统,4.开发和测试环境。Linux在这些领域表现出色,提供了稳定性、安全性和高效的开发工具。

laravel安装代码 laravel安装代码 Apr 18, 2025 pm 12:30 PM

要安装 Laravel,需依序进行以下步骤:安装 Composer(适用于 macOS/Linux 和 Windows)安装 Laravel 安装器创建新项目启动服务访问应用程序(网址:http://127.0.0.1:8000)设置数据库连接(如果需要)

git软件安装 git软件安装 Apr 17, 2025 am 11:57 AM

安装 Git 软件包括以下步骤:下载安装包运行安装包验证安装配置 Git安装 Git Bash(仅限 Windows)

See all articles