简体   繁体   中英

MySQL query audit log w/ metrics

My company has a very specific need to audit queries run against the DB, more specifically DML statements less selects. Ideally we want retrieve the specific metrics

  • Who ran the query
  • When was the query run
  • What was the query
  • Table(s) affected by the query (can probably be parsed out of the query itself)
  • How many rows were affected by the query (consultants won't budge on this)

I've tried the general log, the slow log and the mariadb audit plugin with no avail. Are there any solutions out there aside from table trigger that I can explore?

We would like to export the logs if possible for outside parsing, however this is not a hard requirement.

Percona Server is a branch of MySQL that supports more detail in the slow query log, including rows affected. See examples in this documentation page: https://www.percona.com/doc/percona-server/8.0/diagnostics/slow_extended.html

That would give you the who, when, what, and rows_affected .

The tables affected can be extracted from each log event using some options of pt-query-digest , a tool included in the free Percona Toolkit. You could also use filter options of this tool to limit the results to DML statements.

Percona Server is also needed if you want to log statements run inside stored procedures (see the option log_slow_sp_statements on the page I linked to above).

However, none of the solutions logs statements run inside triggers. So if you want to make your audit complete, you must prohibit use of database triggers in your organization.

You tagged your question . I don't know if MariaDB supports the same extensions to the slow-query log, you'll have to research that in the MariaDB documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM