MySQL8のバイナリーログ自動消去

MySQLからデフォルトでバイナリーログがONになったので、ストレージが足りなくなる事象が発生した。
ログを定期的に消去してログのローテーションをする設定をする。

まずはログイン

$ mysql -u root -p
Server version: 8.0.32 Source distribution

バイナリーログ設定の確認

mysql> SHOW GLOBAL VARIABLES LIKE 'log_bin';
Variable_name Value
log_bin ON
1 row in set (0.00 sec)

expire_logs_daysでログを消去するタイミングを指定する。

mysql> show variables like 'expire_logs_days';
Variable_name Value
expire_logs_days 0
1 row in set (0.01 sec) mysql> SET GLOBAL expire_logs_days = 14; ERROR 3683 (HY000): The option expire_logs_days and binlog_expire_logs_seconds cannot be used together. Please use binlog_expire_logs_seconds to set the expire time (expire_logs_days is deprecated)

とエラーを吐かれる。binlog_expire_logs_secondsを使ってくれと言われるので、秒単位で設定する。14日 (14*24*60*60秒=1209600秒)

mysql> SET GLOBAL binlog_expire_logs_seconds = 1209600;
Query OK, 0 rows affected (0.00 sec)

今度は通ったので確認。

mysql> show variables like 'binlog_expire_logs_seconds';
Variable_name Value
binlog_expire_logs_seconds 1209600
1 row in set (0.01 sec) mysql>exit

どうやら公式ドキュメントを見るとbinlog_expire_logs_secondsはデフォルトで30日(2592000秒)と設定されているようだ。この環境のストレージだと30日もたないみたい。

参考
MySQL公式 17.1.6.4 バイナリロギングのオプションと変数
https://dev.mysql.com/doc/refman/8.0/ja/replication-options-binary-log.html