MySQL Enterprise Auditのログローテーションについての追加の手法。
合わせて、NFS上に監査ログを設定していて、万が一NFSが一時的に切れてしまった場合の挙動の確認。
1) ログサイズによる自動ローテーション
audit_log_rotate_on_size
2) mysqlutilitiesを利用したローテーション
mysqlauditadmin.exe
上記は、前回のブログ記事で紹介。
MySQL Audit Logのローテーション
今回は、CRON等で定期的に実行して、自分のカスタマイズしたいようにログをシェルでローテーションする方法です。
3) 自分でSHELLを用意して運用する方法
概要はこちらのマニュアルページで紹介しています。
https://dev.mysql.com/doc/refman/5.7/en/audit-log-logging-control.html
By default, audit_log_rotate_on_size=0 and there is no log rotation. In this case, the audit log plugin closes and reopens the log file when the audit_log_flush value changes from disabled to enabled.
If audit_log_rotate_on_size is greater than 0, setting audit_log_flush has no effect. In this case, the audit log plugin closes and reopens its log file whenever a write to the file causes its size to exceed the audit_log_rotate_on_size value.
MySQLのaudit関連設定
実際に、以下の様にSHELLを作成してログのローテーション確認してみました。
audit.logがリネームされて、新しいaudit.logファイルが再作成されています。
[root@misc01 mysql]# ls -l 合計 396 -rw-r-----. 1 mysql mysql 2813 9月 21 23:19 audit.log -rwxr-xr-x. 1 root root 391 9月 21 23:19 audit_log_rotate.sh -rw-r-----. 1 mysql mysql 98304 11月 18 2015 osc_tablespace01.ibd drwxrwxr-x. 2 mysql mysql 6 4月 29 2015 perl5 drwxrwxr-x. 2 mysql mysql 4096 4月 29 2015 ssl -rw-r-----. 1 mysql mysql 163840 9月 19 21:43 user_tablespace01.ibd -rw-r-----. 1 mysql mysql 65536 4月 22 2015 user_tablespace02.ibd -rw-r-----. 1 mysql mysql 65536 4月 20 2015 user_tablespace02_8k.ibd [root@misc01 mysql]# ./audit_log_rotate.sh mysql: [Warning] Using a password on the command line interface can be insecure. [root@misc01 mysql]# ls -l 合計 400 -rw-r-----. 1 mysql mysql 47 9月 21 23:19 audit.log -rw-r-----. 1 mysql mysql 2822 9月 21 23:19 audit_log_2016-09-21.log -rwxr-xr-x. 1 root root 391 9月 21 23:19 audit_log_rotate.sh -rw-r-----. 1 mysql mysql 98304 11月 18 2015 osc_tablespace01.ibd drwxrwxr-x. 2 mysql mysql 6 4月 29 2015 perl5 drwxrwxr-x. 2 mysql mysql 4096 4月 29 2015 ssl -rw-r-----. 1 mysql mysql 163840 9月 19 21:43 user_tablespace01.ibd -rw-r-----. 1 mysql mysql 65536 4月 22 2015 user_tablespace02.ibd -rw-r-----. 1 mysql mysql 65536 4月 20 2015 user_tablespace02_8k.ibd
簡易版なので、適宜用途によって加工して利用して下さい。
[root@misc01 mysql]# cat audit_log_rotate.sh #!/bin/sh ####################################### # # MySQL Aduit Log Rotate Shell # ####################################### TODAY=`date -d 'today' '+%Y-%m-%d'` AUDIT_LOG=/home/mysql/audit_log_${TODAY}.log # Archive and rotate audit.log mv /home/mysql/audit.log ${AUDIT_LOG} # Flush Audit Log for creating new log file. mysql -u root -ppassword -e "SET GLOBAL audit_log_flush = ON;" [root@misc01 mysql]#
【その他、確認事項】
■ NFS上にaudit.logを配置して、運用中にNFSが切れてしまった場合の挙動の確認。
ログのローテションを確認する為に、一時的にaudit_log_rotate_on_sizeに最小限のサイズを設定しています。
root@localhost [(none)]> show variables like 'audit%'; +-----------------------------+-----------------------+ | Variable_name | Value | +-----------------------------+-----------------------+ | audit_log_buffer_size | 1048576 | | audit_log_connection_policy | ALL | | audit_log_current_session | OFF | | audit_log_exclude_accounts | | | audit_log_file | /home/mysql/audit.log | | audit_log_filter_id | 0 | | audit_log_flush | OFF | | audit_log_format | NEW | | audit_log_include_accounts | | | audit_log_policy | ALL | | audit_log_rotate_on_size | 0 | | audit_log_statement_policy | ALL | | audit_log_strategy | ASYNCHRONOUS | +-----------------------------+-----------------------+ 13 rows in set (0.00 sec) root@localhost [(none)]> set global audit_log_rotate_on_size=4096; Query OK, 0 rows affected (0.00 sec) root@localhost [(none)]> show variables like 'audit%'; +-----------------------------+-----------------------+ | Variable_name | Value | +-----------------------------+-----------------------+ | audit_log_buffer_size | 1048576 | | audit_log_connection_policy | ALL | | audit_log_current_session | OFF | | audit_log_exclude_accounts | | | audit_log_file | /home/mysql/audit.log | | audit_log_filter_id | 0 | | audit_log_flush | OFF | | audit_log_format | NEW | | audit_log_include_accounts | | | audit_log_policy | ALL | | audit_log_rotate_on_size | 4096 | | audit_log_statement_policy | ALL | | audit_log_strategy | ASYNCHRONOUS | +-----------------------------+-----------------------+ 13 rows in set (0.01 sec) root@localhost [(none)]>
ログをリネームして一時的にMySQLからファイルを見えなくしています。この間にAudit対象のQueryを実行して、ローテーションサイズを超えてもadit.logが無いので監査ログは書き込まれません。
[root@misc01 mysql]# ls -l 合計 424 -rw-r-----. 1 mysql mysql 504 9月 22 21:52 audit.log -rw-r-----. 1 mysql mysql 4466 9月 22 21:52 audit.log.14745487509667319.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487651271064.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487704909257.xml -rw-r-----. 1 mysql mysql 2822 9月 21 23:19 audit_log_2016-09-21.log -rwxr-xr-x. 1 root root 391 9月 21 23:19 audit_log_rotate.sh -rw-r-----. 1 mysql mysql 98304 11月 18 2015 osc_tablespace01.ibd drwxrwxr-x. 2 mysql mysql 6 4月 29 2015 perl5 drwxrwxr-x. 2 mysql mysql 4096 4月 29 2015 ssl -rw-r-----. 1 mysql mysql 163840 9月 19 21:43 user_tablespace01.ibd -rw-r-----. 1 mysql mysql 65536 4月 22 2015 user_tablespace02.ibd -rw-r-----. 1 mysql mysql 65536 4月 20 2015 user_tablespace02_8k.ibd [root@misc01 mysql]# mv audit.log on_purpose_rename_audit.log [root@misc01 mysql]# ls -l 合計 424 -rw-r-----. 1 mysql mysql 4466 9月 22 21:52 audit.log.14745487509667319.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487651271064.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487704909257.xml -rw-r-----. 1 mysql mysql 2822 9月 21 23:19 audit_log_2016-09-21.log -rwxr-xr-x. 1 root root 391 9月 21 23:19 audit_log_rotate.sh -rw-r-----. 1 mysql mysql 504 9月 22 21:52 on_purpose_rename_audit.log -rw-r-----. 1 mysql mysql 98304 11月 18 2015 osc_tablespace01.ibd drwxrwxr-x. 2 mysql mysql 6 4月 29 2015 perl5 drwxrwxr-x. 2 mysql mysql 4096 4月 29 2015 ssl -rw-r-----. 1 mysql mysql 163840 9月 19 21:43 user_tablespace01.ibd -rw-r-----. 1 mysql mysql 65536 4月 22 2015 user_tablespace02.ibd -rw-r-----. 1 mysql mysql 65536 4月 20 2015 user_tablespace02_8k.ibd [root@misc01 mysql]# ls -l 合計 424 -rw-r-----. 1 mysql mysql 4466 9月 22 21:52 audit.log.14745487509667319.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487651271064.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487704909257.xml -rw-r-----. 1 mysql mysql 2822 9月 21 23:19 audit_log_2016-09-21.log -rwxr-xr-x. 1 root root 391 9月 21 23:19 audit_log_rotate.sh -rw-r-----. 1 mysql mysql 961 9月 22 21:53 on_purpose_rename_audit.log -rw-r-----. 1 mysql mysql 98304 11月 18 2015 osc_tablespace01.ibd drwxrwxr-x. 2 mysql mysql 6 4月 29 2015 perl5 drwxrwxr-x. 2 mysql mysql 4096 4月 29 2015 ssl -rw-r-----. 1 mysql mysql 163840 9月 19 21:43 user_tablespace01.ibd -rw-r-----. 1 mysql mysql 65536 4月 22 2015 user_tablespace02.ibd -rw-r-----. 1 mysql mysql 65536 4月 20 2015 user_tablespace02_8k.ibd [root@misc01 mysql]# ls -l 合計 428 -rw-r-----. 1 mysql mysql 4466 9月 22 21:52 audit.log.14745487509667319.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487651271064.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487704909257.xml -rw-r-----. 1 mysql mysql 2822 9月 21 23:19 audit_log_2016-09-21.log -rwxr-xr-x. 1 root root 391 9月 21 23:19 audit_log_rotate.sh -rw-r-----. 1 mysql mysql 6902 9月 22 21:53 on_purpose_rename_audit.log -rw-r-----. 1 mysql mysql 98304 11月 18 2015 osc_tablespace01.ibd drwxrwxr-x. 2 mysql mysql 6 4月 29 2015 perl5 drwxrwxr-x. 2 mysql mysql 4096 4月 29 2015 ssl -rw-r-----. 1 mysql mysql 163840 9月 19 21:43 user_tablespace01.ibd -rw-r-----. 1 mysql mysql 65536 4月 22 2015 user_tablespace02.ibd -rw-r-----. 1 mysql mysql 65536 4月 20 2015 user_tablespace02_8k.ibd
NFSの接続が戻ったと仮定して、ログを元の名前に戻す。
21:56の段階でaudit.logが無かった時のデータがaudit.log.14745489750144595.xmlに書き込まれている。
ログローテーションのサイズ(4096)をオーバーしているがデータがロストしていない事が確認出来る。
また、新規でaudit.logが作成されている。ローテーションも設定サイズできちんと行われている。
audit.logに書き込めない間は、audit_log_bufferに蓄積されているようです。
[root@misc01 mysql]# mv on_purpose_rename_audit.log audit.log [root@misc01 mysql]# ls -l 合計 440 -rw-r-----. 1 mysql mysql 1418 9月 22 21:56 audit.log -rw-r-----. 1 mysql mysql 4466 9月 22 21:52 audit.log.14745487509667319.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487651271064.xml -rw-r-----. 1 mysql mysql 4169 9月 22 21:52 audit.log.14745487704909257.xml -rw-r-----. 1 mysql mysql 15594 9月 22 21:56 audit.log.14745489750144595.xml -rw-r-----. 1 mysql mysql 2822 9月 21 23:19 audit_log_2016-09-21.log -rwxr-xr-x. 1 root root 391 9月 21 23:19 audit_log_rotate.sh -rw-r-----. 1 mysql mysql 98304 11月 18 2015 osc_tablespace01.ibd drwxrwxr-x. 2 mysql mysql 6 4月 29 2015 perl5 drwxrwxr-x. 2 mysql mysql 4096 4月 29 2015 ssl -rw-r-----. 1 mysql mysql 163840 9月 19 21:43 user_tablespace01.ibd -rw-r-----. 1 mysql mysql 65536 4月 22 2015 user_tablespace02.ibd -rw-r-----. 1 mysql mysql 65536 4月 20 2015 user_tablespace02_8k.ibd [root@misc01 mysql]#
補足:ログに書けない間も、MySQLのステータス変数はカウントアップされている事がaudit_log_current_sizeから確認する事が出来ます。
ファイルを戻したタイミングで、ログがファイルに書き込まれ、audit_log_current_sizeは値が小さくなっている事が確認出来ます。
root@localhost [(none)]> show status like 'audit%'; +-------------------------------+-------+ | Variable_name | Value | +-------------------------------+-------+ | audit_log_current_size | 15585 | | audit_log_event_max_drop_size | 0 | | audit_log_events | 1 | | audit_log_events_buffered | 0 | | audit_log_events_filtered | 0 | | audit_log_events_lost | 0 | | audit_log_events_written | 59 | | audit_log_total_size | 28389 | | audit_log_write_waits | 0 | +-------------------------------+-------+ 9 rows in set (0.00 sec) root@localhost [(none)]> show status like 'audit%'; +-------------------------------+-------+ | Variable_name | Value | +-------------------------------+-------+ | audit_log_current_size | 1418 | | audit_log_event_max_drop_size | 0 | | audit_log_events | 1 | | audit_log_events_buffered | 0 | | audit_log_events_filtered | 0 | | audit_log_events_lost | 0 | | audit_log_events_written | 62 | | audit_log_total_size | 29816 | | audit_log_write_waits | 0 | +-------------------------------+-------+ 9 rows in set (0.00 sec) root@localhost [(none)]>