The newPercona Server 5.5.37-35.0andPercona Server 5.6.17-65.0-56, announced yesterday (May 6), both include the open source version of the MySQL Audit Plugin. The MySQL Audit Plugin is used to log all queries or connections (“audit” MySQL usage). Until yesterday’s release, the MySQL Audit Plugin was only available inMySQL Enterprise.
Logging all MySQL usage is very important for a number of applications, for example:
- Required: applications which deals with sensitive data (credit cards, medical records, etc); required for security compliances (i.e. HIPAA)
- Very helpful: multi-tenants applications or MySQL as a service; MySQL administrators can audit the MySQL usage from the security and performance standpoint
- Very helpful: investigating and troubleshooting; it is great to have a full log ofall queries, which can help a lot for troubleshooting of MySQL and even for performance audit.
Originally, the only “easy” option was toenable general log. (Other options included using binary logs which does not include select queries or enabling queries “trace” in the application or MySQL connector). However, logging all queries using a general log may dramatically decrease performance in the highly loaded MySQL applications: Aleksandr Kuzminsky published a benchmark in 2009 to showthe overhead of MySQL general and slow log. The main benefit of MySQL Log Audit plugin is that it logs all queriesasynchronously(can be changed in the config). I’ve decided to try the new audit plugin in Percona Server and measure the performance impact of the new plugin compared to enabling the general log for the CPU bound applications.
How to start with MySQL Audit Plugin
First, we will need to enable (or “install”) MySQL audit plugin asdecribed in the doc:
mysql> select version();+-------------+| version() |+-------------+| 5.5.37-35.0 |+-------------+1 row in set (0.00 sec)mysql> INSTALL PLUGIN audit_log SONAME 'audit_log.so';Query OK, 0 rows affected (0.00 sec)
mysql>selectversion(); +-------------+ |version() |+-------------+ |5.5.37-35.0+-------------+ 1rowinset(0.00 mysql>INSTALL PLUGINaudit_logSONAME'audit_log.so'; QueryOK,0rowsaffected |
Now can see all MySQL audit plugin options:
mysql> show global variables like '%audit%';+--------------------------+--------------+| Variable_name| Value|+--------------------------+--------------+| audit_log_buffer_size| 1048576|| audit_log_file | audit.log|| audit_log_flush| OFF|| audit_log_format | OLD|| audit_log_policy | ALL|| audit_log_rotate_on_size | 0|| audit_log_rotations| 0|| audit_log_strategy | ASYNCHRONOUS |+--------------------------+--------------+8 rows in set (0.00 sec)
mysql>showglobalvariableslike'%audit%'; +--------------------------+--------------+ |Variable_name |Value | +--------------------------+--------------+ |audit_log_buffer_size |1048576 | |audit_log_file |audit.log | |audit_log_flush |OFF | |audit_log_format |OLD | |audit_log_policy |ALL | |audit_log_rotate_on_size|0 | |audit_log_rotations |0 | |audit_log_strategy | +--------------------------+--------------+ 8rowsinset(0.00 |
There are a bunch of options we can tweak here, the most important for MySQL performance are:
- audit_log_buffer_size; this buffer is used to cache the queries (for asynchronous operation).
- audit_log_strategy; All options are listed in the documentation page:
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 |
The most useful option in my mind is ASYNCHRONOUS, providing us with good balance between performance and not loosing transactions if the output buffer is not large enough.
- audit_log_policy; we can log all queries or MySQL logins only (very useful if we only need to audit MySQL connections)
Open Source Audit Plugin in MySQL Community server
You can also use Percona Open Source version of Audit Plugin in MySQL community version (5.5.37 and 5.6.17). Simply download the linux tarball of Percona Server and copy the audit_log.so to your MySQL plugin dir.
Find plugin dir:
mysql> show global variables like '%plugin%';+---------------+------------------------------+| Variable_name | Value|+---------------+------------------------------+| plugin_dir| /usr/local/mysql/lib/plugin/ |+---------------+------------------------------+1 row in set (0.00 sec)
mysql>showglobalvariableslike'%plugin%'; +---------------+------------------------------+ |Variable_name|Value | +---------------+------------------------------+ |plugin_dir |/usr/local/mysql/lib/plugin/ +---------------+------------------------------+ 1rowinset(0.00 |
Copy the file:
# cp audit_log.so /usr/local/mysql/lib/plugin/
# cp audit_log.so /usr/local/mysql/lib/plugin/ |
Install plugin:
Server version: 5.5.37 MySQL Community Server (GPL)mysql> INSTALL PLUGIN audit_log SONAME 'audit_log.so';Query OK, 0 rows affected (0.00 sec)Server version: 5.6.17 MySQL Community Server (GPL)mysql> INSTALL PLUGIN audit_log SONAME 'audit_log.so';Query OK, 0 rows affected (0.00 sec)
Serverversion:5.5.37MySQLCommunityServer(GPL) mysql>INSTALLPLUGINaudit_logSONAME'audit_log.so'; QueryOK,0rowsaffected(0.00sec) Serverversion:5.6.17MySQLCommunityServer(GPL) mysql>INSTALLPLUGINaudit_logSONAME'audit_log.so'; QueryOK,0rowsaffected(0.00sec) |
Using MySQL audit plugin
When plugin is enabled, it will log entries in audit.log file in XML format. Example:
<audit_record utc></audit_record><audit_record utc plugin audit_log soname localhost></audit_record><audit_record utc global variables like localhost></audit_record>
"RECORD"="1_2014-04-30T00:04:42" "TIMESTAMP"="2014-04-30T00:04:42 UTC" "MYSQL_VERSION"="5.5.37-35.0" "STARTUP_OPTIONS"="--basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/lib/mysql/localhost.localdomain.pid --socket=/var/lib/mysql/mysql.sock" "OS_VERSION"="x86_64-Linux", />"RECORD"="2_2014-04-30T00:04:42" "TIMESTAMP"="2014-04-30T00:04:42 UTC" "COMMAND_CLASS"="install_plugin" "CONNECTION_ID"="1" "STATUS"="0""SQLTEXT"="INSTALL PLUGIN audit_log SONAME 'audit_log.so'" "USER"="root[root] @ localhost []" "HOST"="localhost" "OS_USER"="" "IP"=""/>"RECORD"="3_2014-04-30T00:04:42" "TIMESTAMP"="2014-04-30T00:05:07 UTC" "COMMAND_CLASS"="show_variables" "CONNECTION_ID"="1" "STATUS"="0""SQLTEXT"="show global variables like '%audit%'" "USER"="root[root] @ localhost []" "HOST"="localhost" "OS_USER"="" "IP"=""/> |
…
<audit_record utc all on sbtest. to sb identified by localhost></audit_record><audit_record utc></audit_record><audit_record utc pad from sbtest8 where id='5036031"' localhost></audit_record>
"RECORD"="10_2014-04-30T00:04:42" "TIMESTAMP"="2014-04-30T12:33:20 UTC" "COMMAND_CLASS"="grant" "CONNECTION_ID"="2" "STATUS"="0""SQLTEXT"="grant all on sbtest.* to sb@localhost identified by 'sb'" "USER"="root[root] @ localhost []" "HOST"="localhost" "OS_USER"="" "IP"=""/>"RECORD"="11_2014-04-30T00:04:42" "TIMESTAMP"="2014-04-30T12:34:53 UTC" "CONNECTION_ID"="3" "STATUS"="0" "USER"="sb""PRIV_USER"="sb" "OS_LOGIN"="" "PROXY_USER"="" "HOST"="localhost" "IP"="""DB"="sbtest" />"TIMESTAMP"="2014-04-30T12:45:07 UTC" "COMMAND_CLASS"="select" "CONNECTION_ID"="32" "STATUS"="1146" "SQLTEXT"="SELECT pad FROM sbtest8 WHERE id=5036031" "USER"="sb[sb] @ localhost []" "HOST"="localhost" "OS_USER"="""IP"=""/> |
Important notes:
- As all queries will be logged here, the passwords from “GRANT” will also be saved in clear text (as you can see above). It is very important to secure the file on disk.
- The file can grow very large on disk:
ls -lah /var/lib/mysql/audit.log-rw-rw---- 1 mysql mysql 7.1G May 4 07:30 /var/lib/mysql/audit.log
ls-lah/var/lib/mysql/audit.log -rw-rw----1mysqlmysql7.1GMay407:30/var/lib/mysql/audit.log |
Searching the Audit Log entries
MySQL utilities provide a useful tool, mysqlauditgrep, to search / grep the logs file. Unfortunately, I was not able to make it work (tried both v. 1.3 and v 1.4) with audit plugin format created by Percona server. According tothis bug it can’t parse the “new” audit format. In my case, mysqlauditgrep will return a parsing error when I use the default format and returned no results when I set the “audit_log_format=NEW”. It will be nice to use the mysqlauditgrep as it looks like a very powerful tool, but for now our searching options are limited to conventional linux grep (which is not very easy for XML documents) or custom application to parse/search XML.
Performance overhead of Audit Log Plugin and General Log
Finally, I wanted to measure the overhead of the Audit Log Plugin compared to General Log. I did a quick benchmark withsysbenchOLTP test (CPU bound workload) with 4 modes:
- Audit Plugin disabled (to measure baseline)
- Audit Plugin enabled and logs all queries
- Audit Plugin enabled and logs only logins
- General Log enabled, Audit Plugin disabled
Here are the results:
Test | Overhead |
---|---|
Plugin + audit_log_policy = ALL | ~15% overhead |
Plugin + audit_log_policy = LOGINS | ~0% overhead (sysbench only connects once, so there may be bigger overhead here) |
General_log | ~62% overhead |
As we can see here, audit log is not free from overhead, however, it is much smaller than enabling general_log to log all and every query. Those are quick benchmark results and more tests are need for more accurate measurements. Also, as always, your milage can vary.
Nice to have features
What I would love to have for audit plugin is the ability to log only some specific actions. For example, only log activity from a specific user or access to a specific table (i.e. a table with a sensitive data), etc. This will give more control and less overhead (=better performance).
Conclusion
The MySQL Audit Plugin is a great feature – it is a valuable tool for MySQL security and performance audits. The performance overhead may be a concern for a highly loaded systems, however, it looks reasonable and is much better than using general log to log all queries.
If you use general log or any other audit plugins, please share your experience in the comments.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function