Database audit logs are important when you need to understand who accessed a database, which SQL statements were executed, and whether someone modified sensitive information. They are also useful during security investigations, compliance reviews, and production troubleshooting.
However, logging every database event can create a large amount of data. A busy MySQL server might generate thousands of audit events every minute. Finding a particular query, table, or database operation in that volume can become difficult.
Percona Server for MySQL provides the Audit Log Filter component to control which events are written to the audit log. It supports filtering by database users, event classes, event types, and individual event fields.
Starting with Percona Server for MySQL 8.4.13-13, administrators can also use regular expressions in string-field conditions. This makes it possible to match patterns instead of maintaining long lists of exact values.
For example, you can capture changes to tables whose names contain a particular prefix, monitor queries that reference sensitive columns, or filter activity involving a group of similarly named database objects.
In this article, we'll explore how regular expression filtering works, configure practical audit rules, combine conditions, and monitor regex-related errors in Percona Server for MySQL.
What Is the Audit Log Filter Component?
The Audit Log Filter component records database events according to rules configured by the administrator.
It replaces the older audit log plugin and provides more flexible filtering through JSON-based definitions.
Instead of enabling one broad logging policy for the entire server, you can define different filters and assign them to individual MySQL accounts.
The component can capture events such as:
- User connections and disconnections.
- SQL statement execution.
- Table reads, inserts, updates, and deletes.
- Authentication and account changes when the appropriate event mode is enabled.
- Selected database operations and internal audit messages.
The component evaluates each event against the active filter. Events that match the logging rules are written to the configured audit destination. Events that do not match are skipped before reaching the log writer.
This approach can reduce unnecessary audit output and make security investigations easier.
Where regular expressions fit in
Before Percona Server 8.4.13-13, field conditions primarily used exact-value comparisons.
For example, a filter could capture activity against a table named orders:
{
"field": {
"name": "table_name.str",
"value": "orders"
}
}
This works when you know the exact table name.
However, suppose your application has several tables:
- orders_2025
- orders_2026
- orders_archive
- orders_history
Creating separate conditions for every table becomes difficult to maintain.
Regular expressions allow you to express the matching rule once:
{
"field": {
"name": "table_name.str",
"regex": "^orders_[0-9]+$"
}
}
This pattern matches table names beginning with orders_ followed by one or more digits.
The important difference is that value performs a literal comparison, while regex evaluates a pattern against a string field.
Requirements Before Using Regex Filtering
Regular expression field conditions are supported starting with Percona Server for MySQL 8.4.13-13.
Before configuring them, verify the following:
- Percona Server for MySQL 8.4.13-13 or a later compatible release is installed.
- The Audit Log Filter component is installed and active.
- The account configuring filters has the AUDIT_ADMIN privilege.
- The required audit event classes and fields are available in the selected event mode.
- The server can write to the configured audit log destination.
You can check the server version:
SELECT VERSION();
Check whether the Audit Log Filter component is available:
SELECT
COMPONENT_URN,
COMPONENT_NAME
FROM INFORMATION_SCHEMA.COMPONENTS
WHERE COMPONENT_NAME = 'audit_log_filter';
If the component is not installed, follow the installation procedure for your Percona Server release before creating filters.
The examples in this article assume that the component and its system tables are already installed.
Understanding the Regex Syntax
Percona Server uses ICU regular expression syntax for audit filter conditions.
This is important because ICU expressions are not the same as POSIX extended regular expressions or PCRE.
The regex property replaces the value property inside a field condition.
A basic field condition looks like this:
{
"field": {
"name": "table_name.str",
"regex": "^orders_[0-9]+$"
}
}
The name property identifies the event field. The regex property contains the pattern used to evaluate its string value.
Common regex patterns
The following patterns are useful when building database audit rules.
Pattern | Meaning | Example match | | |
|---|
^orders$
| Exact full-string match | orders | | |
^orders_
| Starts with orders_ | orders_2026 | | |
[0-9]+
| Contains one or more digits | orders123 | | |
`^(insert | update | delete)$` | Matches one of three values | update |
(?i)^admin
| Case-insensitive prefix | AdminUser | | |
^audit_[a-z]+$
| Starts with audit_ followed by lowercase letters | audit_events | | |
By default, the regex engine searches for a match anywhere in the field value. Use anchors when you want to match the complete value.
For example, the pattern orders matches a string containing orders anywhere. The pattern ^orders$ matches only the exact string orders.
The match is case-sensitive unless you use an inline flag such as (?i).
Configuring the Audit Log Filter
The Audit Log Filter component stores filter definitions in the MySQL system database.
You create a filter using the audit_log_filter_set_filter() function and assign it to an account using audit_log_filter_set_user().
After changing the definitions, use audit_log_filter_flush() to reload them into the component.
The following examples use the AUDIT_ADMIN privilege.
Check the current audit configuration
Start by checking whether audit logging is disabled:
SHOW GLOBAL VARIABLES
LIKE 'audit_log_filter.disable';
The component must be enabled to write audit events.
You can also check the event mode:
SHOW GLOBAL VARIABLES
LIKE 'audit_log_filter.event_mode';
The default mode is REDUCED. It supports the general, connection, table_access, and message event classes, with a restricted set of subclasses.
The FULL mode enables additional classes, including query, command, authentication, and stored_program.
For the examples below, REDUCED mode is sufficient because the filters use table_access events.
Create a filter for specific table patterns
Suppose an application uses a database named ecommerce and stores order-related tables with names beginning with orders_.
You want to record insert, update, and delete activity against those tables.
Create a filter:
SELECT audit_log_filter_set_filter(
'orders_table_changes',
'{
"filter": {
"class": {
"name": "table_access",
"event": {
"name": ["insert", "update", "delete"],
"log": {
"and": [
{
"field": {
"name": "table_database.str",
"value": "ecommerce"
}
},
{
"field": {
"name": "table_name.str",
"regex": "^orders_[a-zA-Z0-9_]+$"
}
}
]
}
}
}
}
}'
);
This filter contains two conditions.
The first condition checks that the database is ecommerce.
The second condition uses a regular expression to match table names beginning with orders_ and containing letters, numbers, or underscores afterward.
The and operator requires both conditions to match.
Only insert, update, and delete events against matching tables are selected for logging.
Assign the filter to a database user
Creating a filter does not automatically assign it to every account.
Bind it to the application account:
SELECT audit_log_filter_set_user(
'app_user@%',
'orders_table_changes'
);
This assigns the filter to connections using the app_user account from matching hosts.
The filter assignment and the conditions inside the JSON definition serve different purposes.
The assignment determines which filter a session uses. The conditions determine which events that filter records.
A session uses one assigned filter. Separate filter definitions are not automatically merged.
Reload the filter definitions
After creating and assigning the filter, reload the component:
SELECT audit_log_filter_flush();
The component reloads the filter definitions and account assignments from its system tables.
Starting with Percona Server 8.4.9-9, flushing detaches existing sessions until they reconnect or execute CHANGE_USER. New connections use the updated rules immediately.
For this reason, reconnect application sessions after a filter change and plan the reload during a suitable maintenance window.
Filtering Queries With Regular Expressions
Regular expressions are also useful when you need to monitor SQL statement text rather than table names.
The general event class contains query-related fields, including general_query.str.
For example, suppose you want to capture statements that reference a sensitive table named employee_salary.
Create a filter:
SELECT audit_log_filter_set_filter(
'sensitive_query_monitor',
'{
"filter": {
"class": {
"name": "general",
"event": {
"name": "status",
"log": {
"field": {
"name": "general_query.str",
"regex": "(?i)employee_salary"
}
}
}
}
}
}'
);
The inline (?i) flag makes the pattern case-insensitive.
This allows the filter to match statements containing employee_salary regardless of letter casing.
For example, it can match:
SELECT * FROM employee_salary;
UPDATE employee_salary
SET salary = 75000
WHERE employee_id = 101;
DELETE FROM employee_salary
WHERE employee_id = 105;
The filter evaluates the query text carried by the general event.
Keep in mind that the general/status event is available in REDUCED mode, while more detailed query events require FULL mode.
Also, matching statement text does not mean that the audit component understands the complete SQL syntax. A regex can find text patterns, but it is not a replacement for SQL parsing or database authorization.
Combining Multiple Regex Conditions
Real-world audit requirements often involve more than one pattern.
For example, a security team might want to capture activity against tables beginning with payment_, but only when the operation is an update or delete.
You can combine conditions using and and or.
SELECT audit_log_filter_set_filter(
'payment_sensitive_changes',
'{
"filter": {
"class": {
"name": "table_access",
"event": {
"name": ["update", "delete"],
"log": {
"and": [
{
"field": {
"name": "table_database.str",
"value": "finance"
}
},
{
"field": {
"name": "table_name.str",
"regex": "^payment_[a-z0-9_]+$"
}
}
]
}
}
}
}
}'
);
This rule captures updates and deletes against matching tables in the finance database.
You can also use or when multiple patterns should be accepted.
For example:
{
"or": [
{
"field": {
"name": "table_name.str",
"regex": "^payment_"
}
},
{
"field": {
"name": "table_name.str",
"regex": "^customer_"
}
}
]
}
The condition matches table names beginning with either payment_ or customer_.
The not operator can be used when you want to exclude matching values rather than select them.
For example, a rule can log table-access events except those involving the internal mysql schema.
Use only fields available on the event class being filtered. A field that exists on a general event might not be available on a table_access event.
Using JSON_OBJECT() to Avoid Escaping Problems
Regular expressions often contain backslashes, such as \d, \w, and \s.
When a regex is embedded inside JSON text that is itself passed through an SQL string literal, escaping can become confusing.
For example, a digit pattern might need multiple backslashes:
'{"regex": "\\\\d+"}'
A simpler approach is to construct the filter using JSON_OBJECT().
SELECT audit_log_filter_set_filter(
'digit_table_filter',
JSON_OBJECT(
'filter',
JSON_OBJECT(
'class',
JSON_OBJECT(
'name', 'table_access',
'event',
JSON_OBJECT(
'name', JSON_ARRAY(
'read',
'insert',
'update',
'delete'
),
'log',
JSON_OBJECT(
'field',
JSON_OBJECT(
'name', 'table_name.str',
'regex', '[0-9]+'
)
)
)
)
)
)
);
This example records table-access events when the table name contains one or more digits.
Using [0-9]+ also avoids the need for a backslash escape.
For larger filters, JSON_OBJECT() and JSON_ARRAY() can make the configuration easier to maintain and reduce mistakes caused by nested escaping.
Understanding Regex Limitations
Regular expression filtering provides flexibility, but it also introduces behavior that administrators must understand.
Only string fields support regex
The regex property is accepted only for string fields.
For example, table_name.str and general_query.str support regular expressions.
Integer fields, such as status and fields ending in .length, cannot use regex conditions.
Use value for numeric comparisons.
ICU syntax is required
Percona uses ICU regular expression syntax.
Do not assume that a pattern written for a PCRE-based application will behave identically.
Test patterns with the same ICU-compatible behavior before deploying them.
Matching is case-sensitive by default
A pattern such as ^admin does not automatically match AdminUser.
Use an inline flag when case-insensitive matching is required:
(?i)^admin
Patterns have size and execution limits
A regex pattern cannot exceed 16,384 UTF-8 bytes.
The engine also applies time and backtracking stack limits. A match that exceeds either limit is treated as a non-match.
This is important because a failed regex condition can cause an event to be omitted from the audit log.
Regex matching has a performance cost
Regex comparisons are more expensive than literal value comparisons.
The component compiles conditions when loading filters, but creates a separate matcher for each evaluation.
For better performance, put inexpensive equality checks before regex conditions inside an and expression. The component stops evaluating the expression when an earlier condition is false.
For example, checking the database name before evaluating a complex table-name pattern reduces unnecessary regex work.
Monitoring Regex Errors
Percona Server 8.4.13-13 introduces the Audit_log_filter_regex_match_errors status variable.
It counts failed regex matches, including matches that exceed the configured execution limits.
Check the counter using:
SHOW GLOBAL STATUS
LIKE 'Audit_log_filter_regex_match_errors';
You can also query it through Performance Schema:
SELECT
VARIABLE_NAME,
VARIABLE_VALUE
FROM performance_schema.global_status
WHERE VARIABLE_NAME =
'Audit_log_filter_regex_match_errors';
A growing counter does not automatically mean the regex syntax is invalid. It can also indicate that a pattern exceeded its execution time or stack limit.
Review the server error log for warnings containing the filter name, field name, and pattern.
Percona reports the first failure for a condition and limits repeated warnings for that same condition to avoid flooding the error log.
If a regex match fails inside a log condition, the condition is treated as not matching. This can result in an event being omitted.
That behavior is especially important when designing audit rules for compliance or security investigations.
Troubleshooting Common Problems
The filter is not logging expected events
Check the assigned filter:
SELECT *
FROM mysql.audit_log_user;
Confirm that the expected account is mapped to the correct filter.
Also check whether the filter was flushed successfully and whether existing sessions have reconnected.
The regex pattern is rejected
A malformed ICU expression causes filter validation to fail.
For example, an unmatched parenthesis can produce an error similar to:
invalid regular expression for field
'table_name.str': U_REGEX_MISMATCHED_PAREN
Test the pattern independently and check its parentheses, character classes, and escaping.
The filter uses an unsupported field
Verify that the field exists for the selected event class.
For example, table_access events use table_database.str and table_name.str. Do not assume that every general event field is available in this class.
The filter works for one account but not another
Filter assignments are based on MySQL account and host patterns.
A specific account assignment takes precedence over the default assignment.
Check for an existing account-specific filter that might override the default.
Events disappear after an upgrade
Percona Server 8.4.13-13 performs stricter validation of regex field definitions.
A field object cannot contain both value and regex. Older configurations that included both properties can prevent the filter set from loading.
Review the server error log, correct invalid definitions, and reload the filters before relying on audit output.
Best Practices for Production Environments
- Start with simple regex patterns and expand them only when the audit requirement demands it.
- Use exact value comparisons for known database names, usernames, and other fixed values.
- Use regex only for fields that genuinely require pattern matching.
- Place inexpensive equality conditions before regex expressions in and conditions.
- Use anchors such as ^ and $ when you need to match a complete field value.
- Prefer JSON_OBJECT() and JSON_ARRAY() for complex definitions to reduce escaping mistakes.
- Test every filter against representative database activity before applying it to production accounts.
- Monitor Audit_log_filter_regex_match_errors and investigate unexpected increases.
- Reconnect sessions after flushing updated filter definitions.
- Review the audit event mode before using extended event classes.
- Keep the audit log protected and configure rotation and retention to prevent uncontrolled disk growth.
- Avoid treating regex filters as an authorization mechanism. Use MySQL privileges to control what users can execute.
- Review existing filters before upgrading to 8.4.13-13, especially if they contain both value and regex properties.
- Maintain an independent audit retention and backup strategy for compliance requirements.
Advantages and Limitations
Advantages
- Supports pattern-based filtering of string fields.
- Reduces the need to maintain large lists of exact values.
- Works with log, abort, print, and dynamic filter activation conditions.
- Supports ICU regular expression syntax.
- Can be combined with and, or, and not operators.
- Helps focus audit records on sensitive tables and SQL activity.
- Provides a status counter for failed regex matches.
- Allows administrators to update filters without restarting the database server.
Limitations
- Requires Percona Server for MySQL 8.4.13-13 or later.
- Only string fields support regex conditions.
- Patterns are case-sensitive by default.
- ICU syntax differs from PCRE and POSIX expressions.
- Regex evaluation is more expensive than literal comparison.
- Patterns have size, execution-time, and stack limits.
- A failed regex match is treated as a non-match and can omit an event.
- Existing sessions need to reconnect after filter changes are flushed.
Summary
Regular expression filtering in Percona Server for MySQL 8.4.13-13 gives database administrators more control over which audit events are recorded.
Instead of maintaining many exact-value conditions, you can use ICU regular expressions to match table names, query text, and other supported string fields. These conditions can be combined with database, event-type, and account-specific rules to build more focused audit policies.
The most important implementation details are to use regex only on supported string fields, understand the matching and escaping rules, and monitor failed matches through Audit_log_filter_regex_match_errors.
Regular expressions should complement the existing audit filter design, not replace it. Use literal comparisons wherever possible, keep patterns simple, and validate your rules before deploying them.
With a carefully designed configuration, Percona Server can capture the database activity that matters while reducing unnecessary audit log volume.
Join the conversation! Your thoughts help the community grow.