使用logrotate對MySQL指定記錄檔執行按天輪轉備份

來源:互聯網
上載者:User

標籤:mysql

根據自己的需要可以對MySQL的相關記錄檔(錯誤記錄檔、通用查詢記錄檔和慢查詢記錄檔)進行按天的轉儲,並只保留指定天數的備份檔案,下面是一個例子:

1)、安裝 crond 服務 (如果未安裝   yum install logrotate crontabs)

rpm  -qa  |  grep crontabs

yum  install  crontabs

chkconfig  crond  --list   #如果未設定為自動重啟這執行如下命令修改

chkconfig  crond  on

service  crond  restart


2)、建立MySQL root密碼檔案

vi /root/.my.cnf

[mysqladmin]  

user = sysadmin

password = mysql  

chmod 600 /root/.my.cnf


3)、把mysql-log-rotate拷貝至/etc/logrotate.d目錄下,修改其內容為:

# mysql_error.log        錯誤記錄檔檔案

# mysql_general.log    通用查詢記錄檔

# mysql_slow.log         慢查詢記錄檔

/app/mysql/mysql3306/logs/mysql_error.log

/app/mysql/mysql3306/logs/mysql_general.log

/app/mysql/mysql3306/logs/mysql_slow.log {

       # create 600 mysql mysql

       notifempty

       daily

       rotate 5

       copytruncate

       nocreate

       missingok

       compress

       dateext

   postrotate

# just if mysqld is really running

if test -x /app/mysql/mysql3306/bin/mysqladmin && \

  /app/mysql/mysql3306/bin/mysqladmin ping &>/dev/null

then

  /app/mysql/mysql3306/bin/mysqladmin flush-logs

fi

   endscript


4)執行以下命令以測試是否配置正確

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

註:需要查看已備份壓縮檔這執行 gunzip   file_name.gz  即可


5)定時執行 (每天淩晨執行)

# vim /etc/crontab

59 23 * * * root /usr/sbin/logrotate -f /etc/logrotate.d/mysql-log-rotate 


本文出自 “獨孤九劍” 部落格,請務必保留此出處http://130030.blog.51cto.com/120030/1922805

使用logrotate對MySQL指定記錄檔執行按天輪轉備份

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.