1. 前言
2. 开启审计
2.1 配置文件载入
- [mysqld]
- plugin-load=audit_log.so
假设希望数据库强制开启审计功能。假设不开启的话server不启动,或者审计功能不能进行时server挂住,增加
3.1 audit_log_buffer_size
3.2 audit_log_connection_policy
Value | Description |
---|---|
ALL | Log all connection events |
ERRORS | Log only failed connection events |
NONE | Do not log connection events |
3.3 audit_log_current_session
audit_log_current_session:标志当前会话是否进入审计,是个仅仅读參数,仅仅能通过 audit_log_exclude_accounts和 audit_log_include_accounts来控制哪儿些进入会话审计。3.4 audit_log_exclude_accounts/audit_log_include_accounts
3.5 audit_log_file
3.6 audit_log_flush
3.7 audit_log_format
当更改格式的时候须要进行3个步骤:
3:更改audit_log_format參数,并重新启动mysql,重新启动后会自己主动生成一个新的audit.log文件
Value | Description |
---|---|
ALL | Log all events |
LOGINS | Log only login events |
QUERIES | Log only query events |
NONE | Log nothing (disable the audit stream |
3.8 audit_log_statement_policy
audit_log_statement_policy:记录了语句的审计策略。可能会被audit_log_policy给覆盖:Value | Description |
---|---|
ALL | Log all statement events |
ERRORS | Log only failed statement events |
NONE | Do not log statement events |
3.9 audit_log_rotate_on_size
当參数大于0的时候,当审计日志超过限制后。会自己主动的重命名为加时间戳后缀的日志文件。同一时候创建新的审计日志。
3.10 audit_log_strategy
Value | Meaning |
---|---|
ASYNCHRONOUS | Log asynchronously, wait for space in output buffer |
PERFORMANCE | Log asynchronously, drop request if insufficient space in output buffer |
SEMISYNCHRONOUS | Log synchronously, permit caching by operating system |
SYNCHRONOUS | Log synchronously, call sync() after each request |
4. 日志格式
- <?xml version="1.0" encoding="UTF-8"?>
- <AUDIT>
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:24 UTC</TIMESTAMP>
- <RECORD_ID>1_2013-09-17T15:03:24</RECORD_ID>
- <NAME>Audit</NAME>
- <SERVER_ID>1</SERVER_ID>
- <VERSION>1</VERSION>
- <STARTUP_OPTIONS>/usr/local/mysql/bin/mysqld
- --socket=/usr/local/mysql/mysql.sock
- --port=3306</STARTUP_OPTIONS>
- <OS_VERSION>x86_64-osx10.6</OS_VERSION>
- <MYSQL_VERSION>5.7.2-m12-log</MYSQL_VERSION>
- </AUDIT_RECORD>
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:40 UTC</TIMESTAMP>
- <RECORD_ID>2_2013-09-17T15:03:24</RECORD_ID>
- <NAME>Connect</NAME>
- <CONNECTION_ID>2</CONNECTION_ID>
- <STATUS>0</STATUS>
- <STATUS_CODE>0</STATUS_CODE>
- <USER>root</USER>
- <OS_LOGIN></OS_LOGIN>
- <HOST>localhost</HOST>
- <IP>127.0.0.1</IP>
- <COMMAND_CLASS>connect</COMMAND_CLASS>
- <PRIV_USER>root</PRIV_USER>
- <PROXY_USER></PROXY_USER>
- <DB>test</DB>
- </AUDIT_RECORD>
-
- ...
-
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:41 UTC</TIMESTAMP>
- <RECORD_ID>4_2013-09-17T15:03:24</RECORD_ID>
- <NAME>Query</NAME>
- <CONNECTION_ID>2</CONNECTION_ID>
- <STATUS>0</STATUS>
- <STATUS_CODE>0</STATUS_CODE>
- <USER>root[root] @ localhost [127.0.0.1]</USER>
- <OS_LOGIN></OS_LOGIN>
- <HOST>localhost</HOST>
- <IP>127.0.0.1</IP>
- <COMMAND_CLASS>drop_table</COMMAND_CLASS>
- <SQLTEXT>DROP TABLE IF EXISTS t</SQLTEXT>
- </AUDIT_RECORD>
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:41 UTC</TIMESTAMP>
- <RECORD_ID>5_2013-09-17T15:03:24</RECORD_ID>
- <NAME>Query</NAME>
- <CONNECTION_ID>2</CONNECTION_ID>
- <STATUS>0</STATUS>
- <STATUS_CODE>0</STATUS_CODE>
- <USER>root[root] @ localhost [127.0.0.1]</USER>
- <OS_LOGIN></OS_LOGIN>
- <HOST>localhost</HOST>
- <IP>127.0.0.1</IP>
- <COMMAND_CLASS>create_table</COMMAND_CLASS>
- <SQLTEXT>CREATE TABLE t (i INT)</SQLTEXT>
- </AUDIT_RECORD>
-
- ...
-
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:41 UTC</TIMESTAMP>
- <RECORD_ID>7_2013-09-17T15:03:24</RECORD_ID>
- <NAME>Quit</NAME>
- <CONNECTION_ID>2</CONNECTION_ID>
- <STATUS>0</STATUS>
- <STATUS_CODE>0</STATUS_CODE>
- <USER></USER>
- <OS_LOGIN></OS_LOGIN>
- <HOST></HOST>
- <IP></IP>
- <COMMAND_CLASS>connect</COMMAND_CLASS>
- </AUDIT_RECORD>
-
- ...
-
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:47 UTC</TIMESTAMP>
- <RECORD_ID>9_2013-09-17T15:03:24</RECORD_ID>
- <NAME>Shutdown</NAME>
- <CONNECTION_ID>3</CONNECTION_ID>
- <STATUS>0</STATUS>
- <STATUS_CODE>0</STATUS_CODE>
- <USER>root[root] @ localhost [127.0.0.1]</USER>
- <OS_LOGIN></OS_LOGIN>
- <HOST>localhost</HOST>
- <IP>127.0.0.1</IP>
- <COMMAND_CLASS></COMMAND_CLASS>
- </AUDIT_RECORD>
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:47 UTC</TIMESTAMP>
- <RECORD_ID>10_2013-09-17T15:03:24</RECORD_ID>
- <NAME>Quit</NAME>
- <CONNECTION_ID>3</CONNECTION_ID>
- <STATUS>0</STATUS>
- <STATUS_CODE>0</STATUS_CODE>
- <USER></USER>
- <OS_LOGIN></OS_LOGIN>
- <HOST></HOST>
- <IP></IP>
- <COMMAND_CLASS>connect</COMMAND_CLASS>
- </AUDIT_RECORD>
- <AUDIT_RECORD>
- <TIMESTAMP>2013-09-17T15:03:49 UTC</TIMESTAMP>
- <RECORD_ID>11_2013-09-17T15:03:24</RECORD_ID>
- <NAME>NoAudit</NAME>
- <SERVER_ID>1</SERVER_ID>
- </AUDIT_RECORD>
- </AUDIT>
<AUDIT_RECORD> :包括一系列的必选标签和可选标签。可选标签是否出现取决于audit record类型。
<NAME>:必选,比如<NAME>Query</NAME>,可能出现的值还包括Audit, Binlog Dump, Change user, Close stmt, Connect Out, Connect, Create DB, Daemon, Debug, Delayed insert, Drop DB, Execute, Fetch, Field List, Init DB, Kill, Long Data, NoAudit, Ping, Prepare, Processlist, Query, Quit, Refresh, Register Slave, Reset stmt, Set option, Shutdown, Sleep, Statistics, Table Dump, Time.
<RECORD_ID>:必选,比如<RECORD_ID>28743_2013-09-18T21:03:24</RECORD_ID>。包括一些列数字和时间戳,数字表示的是记录数。每添加一条记录,数字加1.
<TIMESTAMP>:必选,比如<TIMESTAMP>2013-09-17T15:03:49 UTC</TIMESTAMP>,包括时间戳和时区两部分,记录的是从client接收到的sql运行完时刻的时间。
下面标签audit record类型决定是否出现
<COMMAND_CLASS>:命令的类型。比如<COMMAND_CLASS>drop_table</COMMAND_CLASS>.
<CONNECTION_ID>:比如<CONNECTION_ID>127</CONNECTION_ID>,代表client连接标识符的无符号整型数字。
<DB>:mysql连接的默认数据库名称。该标签仅仅在 <NAME>值是Connect或Change user时出现.
<HOST>:client端的主机名,该标签仅仅在 <NAME>值是Connect,Change user或Query时出现,比如<HOST>localhost</HOST>。
<IP>:client端的IP地址,该标签仅仅在 <NAME>值是Connect,Change user或Query时出现,比如<IP>127.0.0.1</IP>。
<MYSQL_VERSION>:mysql版本,仅仅在 <NAME>值是Audit时出现,比如<MYSQL_VERSION>5.7.1-m11-log</MYSQL_VERSION>
<OS_LOGIN>:外部用户。该标签仅仅在 <NAME>值是Connect。Change user或Query时出现。
<OS_VERSION>:表示执行数据库的server的操作系统。仅仅在 <NAME>值是Audit时出现。比如<OS_VERSION>x86_64-Linux</OS_VERSION>。
<PRIV_USER>:server认证的client名称。该标签仅仅在 <NAME>值是Connect或Change user时出现。
比如<PRIV_USER>root</PRIV_USER>。
<PROXY_USER>:通过proxy连接到mysql的用户。该标签仅仅在 <NAME>值是Connect或Change user时出现。
<SERVER_ID>:mysql数据库server的ID号,该标签仅仅在 <NAME>值是Audit或No Audit时出现。比如<SERVER_ID>1</SERVER_ID>。
<SQLTEXT>:实际运行的SQL语句。该标签仅仅在 <NAME>值是 Query 或 Execute时出现。
比如<SQLTEXT>DELETE FROM t1</SQLTEXT>。
<STARTUP_OPTIONS>:mysql数据库启动选项,该标签仅仅在 <NAME>值是Audit时出现,比如<STARTUP_OPTIONS>/usr/local/mysql/bin/mysqld --port=3306 --log-output=FILE</STARTUP_OPTIONS>
<STATUS>:代表sql命令的运行状态,0表示成功,其余表示有错误。比如<STATUS>1051</STATUS>。
<STATUS_CODE>:代表sql命令的运行状态,0表示成功,1表示有错误。比如<STATUS_CODE>0</STATUS_CODE>。
<USER>:client连接mysqlserver的username。比如<USER>root[root] @ localhost [127.0.0.1]</USER>。
<VERSION>:表示日志文件格式的版本。
该标签仅仅在 <NAME>值是Audit时出现。
比如<VERSION>1</VERSION>。