首頁 資料庫 mysql教程 MySQL Audit Plugin now available in Percona Server 5.5 and 5_MySQL

MySQL Audit Plugin now available in Percona Server 5.5 and 5_MySQL

Jun 01, 2016 pm 01:15 PM

The MySQL Audit Plugin is now available for free in Percona ServerThe 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>
登入後複製
  "NAME"="Audit"

  "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",

/>  "NAME"="Query"

  "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"=""/>  "NAME"="Query"

  "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>
登入後複製
  "NAME"="Query"

  "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"=""/>  "NAME"="Connect"

  "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"

/>"RECORD"="1292_2014-04-30T00:04:42"

"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:

  1. Audit Plugin disabled (to measure baseline)
  2. Audit Plugin enabled and logs all queries
  3. Audit Plugin enabled and logs only logins
  4. 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.

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1317
25
PHP教程
1268
29
C# 教程
1246
24
mysql:簡單的概念,用於輕鬆學習 mysql:簡單的概念,用於輕鬆學習 Apr 10, 2025 am 09:29 AM

MySQL是一個開源的關係型數據庫管理系統。 1)創建數據庫和表:使用CREATEDATABASE和CREATETABLE命令。 2)基本操作:INSERT、UPDATE、DELETE和SELECT。 3)高級操作:JOIN、子查詢和事務處理。 4)調試技巧:檢查語法、數據類型和權限。 5)優化建議:使用索引、避免SELECT*和使用事務。

MySQL的角色:Web應用程序中的數據庫 MySQL的角色:Web應用程序中的數據庫 Apr 17, 2025 am 12:23 AM

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

說明InnoDB重做日誌和撤消日誌的作用。 說明InnoDB重做日誌和撤消日誌的作用。 Apr 15, 2025 am 12:16 AM

InnoDB使用redologs和undologs確保數據一致性和可靠性。 1.redologs記錄數據頁修改,確保崩潰恢復和事務持久性。 2.undologs記錄數據原始值,支持事務回滾和MVCC。

MySQL:世界上最受歡迎的數據庫的簡介 MySQL:世界上最受歡迎的數據庫的簡介 Apr 12, 2025 am 12:18 AM

MySQL是一種開源的關係型數據庫管理系統,主要用於快速、可靠地存儲和檢索數據。其工作原理包括客戶端請求、查詢解析、執行查詢和返回結果。使用示例包括創建表、插入和查詢數據,以及高級功能如JOIN操作。常見錯誤涉及SQL語法、數據類型和權限問題,優化建議包括使用索引、優化查詢和分錶分區。

MySQL的位置:數據庫和編程 MySQL的位置:數據庫和編程 Apr 13, 2025 am 12:18 AM

MySQL在數據庫和編程中的地位非常重要,它是一個開源的關係型數據庫管理系統,廣泛應用於各種應用場景。 1)MySQL提供高效的數據存儲、組織和檢索功能,支持Web、移動和企業級系統。 2)它使用客戶端-服務器架構,支持多種存儲引擎和索引優化。 3)基本用法包括創建表和插入數據,高級用法涉及多表JOIN和復雜查詢。 4)常見問題如SQL語法錯誤和性能問題可以通過EXPLAIN命令和慢查詢日誌調試。 5)性能優化方法包括合理使用索引、優化查詢和使用緩存,最佳實踐包括使用事務和PreparedStatemen

為什麼要使用mysql?利益和優勢 為什麼要使用mysql?利益和優勢 Apr 12, 2025 am 12:17 AM

選擇MySQL的原因是其性能、可靠性、易用性和社區支持。 1.MySQL提供高效的數據存儲和檢索功能,支持多種數據類型和高級查詢操作。 2.採用客戶端-服務器架構和多種存儲引擎,支持事務和查詢優化。 3.易於使用,支持多種操作系統和編程語言。 4.擁有強大的社區支持,提供豐富的資源和解決方案。

MySQL與其他編程語言:一種比較 MySQL與其他編程語言:一種比較 Apr 19, 2025 am 12:22 AM

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。MySQL以其高性能、可扩展性和跨平台支持著称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

MySQL索引基數如何影響查詢性能? MySQL索引基數如何影響查詢性能? Apr 14, 2025 am 12:18 AM

MySQL索引基数对查询性能有显著影响:1.高基数索引能更有效地缩小数据范围,提高查询效率;2.低基数索引可能导致全表扫描,降低查询性能;3.在联合索引中,应将高基数列放在前面以优化查询。

See all articles