星期三, 3月 04, 2015

[備份] 超佔用空間的 mysql binary log 設定只保留 N 天

當硬碟空間(/var分割區)不是很大且mysql的資料量又不小時,常常因為保留太多的 binary log 而造成空間不足。

解法如下:

(開啟 mysql設定檔) vi /etc/mysql/my.cnf

# 找到以下紅字部份,如無請自行新增# The following can be used as easy to replay backup logs or for replication.
#server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
# WARNING: Using expire_logs_days without bin_log crashes the server! See README.Debian!
expire_logs_days = 10max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name

# 將預設保留天數改成 N 天expire_logs_days = 3

改完之後記得重新啟動 mysql 就一切搞定了。

(重新啟動時就會自動刪去超過 N 天的 binary log)