搜尋
首頁資料庫mysql教程[Linux] 利用logrotate对MySQL日志进行轮转_MySQL

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
將用戶添加到MySQL:完整的教程將用戶添加到MySQL:完整的教程May 12, 2025 am 12:14 AM

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

掌握mySQL字符串數據類型:varchar vs.文本與char掌握mySQL字符串數據類型:varchar vs.文本與charMay 12, 2025 am 12:12 AM

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

MySQL:字符串數據類型和索引:最佳實踐MySQL:字符串數據類型和索引:最佳實踐May 12, 2025 am 12:11 AM

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。

mysql:如何遠程添加用戶mysql:如何遠程添加用戶May 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

MySQL字符串數據類型的最終指南:有效的數據存儲MySQL字符串數據類型的最終指南:有效的數據存儲May 12, 2025 am 12:05 AM

tostorestringsefliceflicyInmySql,ChooSetherightDataTypeBasedyOrneOrneEds:1)USEcharforFixed-LengthStstringStringStringSlikeCountryCodes.2)UseVarcharforvariable-lengtthslikenames.3)USETEXTCONTENT.3)

mysql blob vs.文本:為大對象選擇正確的數據類型mysql blob vs.文本:為大對象選擇正確的數據類型May 11, 2025 am 12:13 AM

選擇MySQL的BLOB和TEXT數據類型時,BLOB適合存儲二進制數據,TEXT適合存儲文本數據。 1)BLOB適用於圖片、音頻等二進制數據,2)TEXT適用於文章、評論等文本數據,選擇時需考慮數據性質和性能優化。

MySQL:我應該將root用戶用於產品嗎?MySQL:我應該將root用戶用於產品嗎?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL字符串數據類型說明了:選擇適合您數據的合適類型MySQL字符串數據類型說明了:選擇適合您數據的合適類型May 11, 2025 am 12:10 AM

mySqlStringDatatAtatPessHouldBechoseBasedondatActarActeristicsAndusecases:1)USEcharforFixed lengthStstringStringStringSlikeCountryCodes.2)usevarcharforvariable-lengtthslikeLikenames.3)usebarnionororvarinyorvarinyorvarybinarydatalgebenedaTalgeextocrabextrapon.4)

See all articles

熱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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中