Database Audit Logging (Beta) for TiDB Cloud Essential
TiDB Cloud Essential provides an audit logging feature that records user access activities of your database, such as executed SQL statements.
To evaluate the effectiveness of user access policies and other information security measures of your organization, it is a security best practice to periodically analyze database audit logs.
The audit logging feature is disabled by default. To audit a TiDB cluster, you need to enable audit logging for it.
Audit logging configurations
Data redaction
By default, TiDB Cloud Essential redacts sensitive data in audit logs. Take the following SQL statement as an example:
INSERT INTO `test`.`users` (`id`, `name`, `password`) VALUES (1, 'Alice', '123456');
It is redacted as follows:
INSERT INTO `test`.`users` (`id`, `name`, `password`) VALUES ( ... );
Log file rotation
TiDB Cloud Essential generates a new audit log file when either of the following conditions is met:
- The current log file reaches the rotation size (100 MiB by default).
- The rotation interval (one hour by default) has passed since the previous log generation. Depending on the internal scheduling mechanism, log generation might be delayed by a few minutes.
Audit logging locations
You can store audit logs in the following locations:
TiDB Cloud
You can store audit logs in TiDB Cloud and download them to your local machine. Audit logs expire and are deleted after 365 days. To request a longer retention period, contact TiDB Cloud Support.
Amazon S3
To store audit logs in Amazon S3, you need to provide the following information:
- URI:
s3://<bucket-name>/<folder-path>/ - Access credentials: choose one of the following:
- An access key with the
s3:PutObjectpermission. - A role ARN with the
s3:PutObjectpermission. Only clusters hosted on AWS support using a role ARN.
- An access key with the
For more information, see Configure Amazon S3 access.
Google Cloud Storage
To store audit logs in Google Cloud Storage, you need to provide the following information:
- URI:
gs://<bucket-name>/<folder-path>/ - Access credential: a service account key with the
storage.objects.createandstorage.objects.deletepermissions.
For more information, see Configure GCS access.
Azure Blob Storage
To store audit logs in Azure Blob Storage, you need to provide the following information:
- URI:
azure://<account-name>.blob.core.windows.net/<container-name>/<folder-path>/orhttps://<account-name>.blob.core.windows.net/<container-name>/<folder-path>/ - Access credential: a shared access signature (SAS) token with
ReadandWritepermissions on theContainerandObjectresources.
For more information, see Configure Azure Blob Storage access.
Alibaba Cloud OSS
To store audit logs in Alibaba Cloud OSS, you need to provide the following information:
- URI:
oss://<bucket-name>/<folder-path>/ - Access credential: an AccessKey pair with the
oss:PutObjectandoss:GetBucketInfopermissions to allow data export to the OSS bucket.
For more information, see Configure Alibaba Cloud Object Storage Service (OSS) access.
Audit logging filter rules
To filter audit logs, you need to create a filter rule to specify which events to log.
The filter rule contains the following fields:
users: A list of user names to filter audit events. You can use the wildcard%to match any user name.filters: A list of filter objects. Each filter object contains the following fields:classes: A list of event classes to filter audit events. For example,["QUERY", "EXECUTE"].tables: A list of table filters. For more information, see Table Filter.statusCodes: A list of status codes to filter audit events.1means success, and0means failure.
The following table shows all event classes in database audit logging:
Configure audit logging
You can enable, edit, and disable audit logging.
Enable audit logging
You can enable audit logging for a TiDB Cloud Essential cluster using the TiDB Cloud console or the TiDB Cloud CLI.
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, click Enable.
Select a storage location for the audit logs and fill in the required information. Then click Test Connection and Next or Next. For more information about available storage locations, see Audit logging locations.
In the Database Audit Logging Settings dialog, fill in the log file rotation and log redaction settings, and then click Save.
Take Amazon S3 storage as an example. To enable audit logging and store audit logs in Amazon S3, run the following command:
ticloud serverless audit-log config update -c <cluster-id> --enabled --cloud-storage S3 --s3.uri <s3-url> --s3.access-key-id <s3-access-key-id> --s3.secret-access-key <s3-secret-access-key> --rotation-size-mib <size-in-mb> --rotation-interval-minutes <interval-in-minutes> --unredacted=<true|false>
The --rotation-size-mib, --rotation-interval-minutes, and --unredacted parameters are optional. If you do not specify them, the default values are used.
Edit audit logging
You can edit the audit logging for a TiDB Cloud Essential cluster after enabling it.
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, click Settings.
In the Database Audit Logging Settings dialog, update the log file rotation or log redaction settings, and then click Save.
To update the audit logging settings using the TiDB Cloud CLI, run the following command:
ticloud serverless audit-log config update -c <cluster-id> --rotation-size-mib <size-in-mb> --rotation-interval-minutes <interval-in-minutes> --unredacted=<true|false>
Disable audit logging
You can disable audit logging for a TiDB Cloud Essential cluster.
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, click ... in the upper-right corner, and then click Disable.
In the Disable DB Audit Logging dialog, click Disable.
To disable audit logging using the TiDB Cloud CLI, run the following command:
ticloud serverless audit-log config update -c <cluster-id> --disabled=true
Manage audit logging filter rules
You can create, edit, disable, and delete an audit logging filter rule.
Create a filter rule
To create a filter rule, define which users and events you want to capture in the audit logs. You can specify users, event classes, tables, and status codes to tailor the logging to your needs.
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, click Add Filter Rule.
In the Add Filter Rule dialog, fill in the Filter Name, SQL Users, and Filter Rule fields, and then click Confirm. For more information about these fields, see Audit logging filter rules.
To create a filter rule using the TiDB Cloud CLI, run the following command:
ticloud serverless audit-log filter create --cluster-id <cluster-id> --display-name <rule-name> --rule '{"users":["%@%"],"filters":[{}]}'
Edit a filter rule
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, locate the filter rule you want to edit, click ... in its row, and then click Edit.
In the Edit Filter Rule dialog, update the Filter Name or Filter Rule field, and then click Confirm.
To edit a filter rule using the TiDB Cloud CLI, run the following command:
ticloud serverless audit-log filter update --cluster-id <cluster-id> --filter-rule-id <rule-id> --rule '{"users":["%@%"],"filters":[{"classes":["QUERY"],"tables":["test.t"]}]}'
Disable a filter rule
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, locate the filter rule you want to disable, and turn off the toggle to disable the filter rule.
To disable a filter rule using the TiDB Cloud CLI, run the following command:
ticloud serverless audit-log filter update --cluster-id <cluster-id> --filter-rule-id <rule-id> --enabled=false
Delete a filter rule
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, locate the filter rule you want to delete and click ....
Click Delete, and then click I understand. Delete it to confirm.
ticloud serverless audit-log filter delete --cluster-id <cluster-id> --filter-rule-id <rule-id>
Access audit logging with TiDB Cloud Storage
When you store audit logs in TiDB Cloud, TiDB Cloud Essential saves them as readable text files named YYYY-MM-DD-<index>.log. You can view and download these files from the TiDB Cloud console or using the TiDB Cloud CLI.
Log in to the TiDB Cloud console and navigate to the Clusters page of your project.
Click the name of your target cluster to go to its overview page, and then click Settings > DB Audit Logging in the left navigation pane.
On the DB Audit Logging page, you can view the list of audit logs under TiDB Cloud Storage.
To download audit logs, select one or more logs from the list and then click Download.
To download audit logs using the TiDB Cloud CLI, run the following command:
ticloud serverless audit-log download --cluster-id <cluster-id> --output-path <output-path> --start-date <start-date> --end-date <end-date>
start-date: the start date of the audit logs to download, in the format ofYYYY-MM-DD, for example,2025-01-01.end-date: the end date of the audit logs to download, in the format ofYYYY-MM-DD, for example,2025-01-01.
Audit logging fields
For each database event record in audit logs, TiDB Cloud provides the following fields:
General information
All classes of audit logs contain the following information:
SQL statement information
When the event class is QUERY or a subclass of QUERY, the audit logs contain the following information:
Connection information
When the event class is CONNECTION or a subclass of CONNECTION, the audit logs contain the following information:
Audit operation information
When the event class is AUDIT or a subclass of AUDIT, the audit logs contain the following information:
Audit logging limitations
TiDB Cloud Essential does not guarantee the sequential order of audit logs, which means that you might have to review all log files find the most recent events. To sort the logs chronologically, you can use the TIME field in the audit logs.