Enabling SQL Audit
Scenarios
After you enable the SQL audit function, all SQL operations will be recorded in log files. You can download audit logs to view log details.
By default, SQL audit is disabled for RDS for MySQL instances. Enabling this feature increases database load (consuming the instance's CPUs, memory, storage, and disk IOPS), but the performance impact is typically no more than 5%. This section describes how to enable, modify, and disable SQL audit.
Supported Database Versions
- RDS for MySQL 5.6 instances using cloud disks: 5.6.43 and later versions
- RDS for MySQL 5.7 instances using cloud disks: 5.7.23 and later versions
- RDS for MySQL 8.0
Constraints
| Audit Log Type | Constraints |
|---|---|
| SQL audit logs |
|
| Audit logs reported to LTS |
|
Procedure
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name.
- In the navigation pane on the left, choose Logs. On the displayed page, click the SQL Audit Logs tab.
- On the displayed tab page, click Set SQL Audit. In the displayed dialog box, specify parameters and click OK. Enabling or setting SQL audit
- To enable SQL audit, toggle on the switch (from
to
). - Audit logs can be retained from 1 to 732 days and are retained for 7 days by default.
- Once Rapid Audit Log Rotation is enabled, the service polls audit logs every 5 minutes and uploads newly generated audit logs to OBS or LTS.
Figure 1 Setting SQL audit
The Operations option is available only in the CN North-Beijing4, CN South-Guangzhou, and CN-Hong Kong regions. To use this option in other regions, submit a service ticket.
The SQL statements executed by PreparedStatement and scheduled tasks through a MySQL client will be treated as PREPARED_STATEMENT and CREATE, respectively. However, the SQL statements executed by PreparedStatement through JDBC will not be recorded.
After SQL audit is enabled, Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and other operation types are supported. The details are as follows:
Table 2 DDL types and operations Type
Operation
Remarks
CREATE
create_db, create_event, create_function, create_index, create_procedure, create_table, create_trigger, create_udf, create_view
-
ALTER
alter_db, alter_db_upgrade, alter_event, alter_function, alter_instance, alter_procedure, alter_table, alter_tablespace
The alter_user operation is no longer supported from January 26, 2024.
DROP
drop_db, drop_event, drop_function, drop_index, drop_procedure, drop_table, drop_trigger, drop_view
-
RENAME
rename_table
-
TRUNCATE
truncate
-
REPAIR
repair
Added on January 26, 2024
OPTIMIZE
optimize
Added on January 26, 2024
Table 3 DML types and operations Type
Operation
Remarks
INSERT
insert, insert_select
-
DELETE
delete, delete_multi
-
UPDATE
update, update_multi
The update_multi operation was added on January 26, 2024.
REPLACE
replace, replace_select
-
SELECT
select
-
Table 4 DCL types and operations Type
Operation
Remarks
CREATE_USER
create_user
-
DROP_USER
drop_user
-
RENAME_USER
rename_user
-
GRANT
grant_roles, grant
The grant_roles operation was added on January 26, 2024.
REVOKE
revoke, revoke_all, revoke_roles
The revoke_roles operation was added on January 26, 2024.
ALTER_USER
alter_user
Added on January 26, 2024
ALTER_USER_DEFAULT_ROLE
alter_user_default_role
Added on January 26, 2024
Table 5 Other types and operations Type
Operation
Remarks
BEGIN/COMMIT/ROLLBACK
begin, commit, release_savepoint, rollback, rollback_to_savepoint, savepoint
-
PREPARED_STATEMENT
execute_sql, prepare_sql, dealloc_sql
The dealloc_sql operation was added on January 26, 2024.
CALL_PROCEDURE
call_procedure
Added on January 26, 2024
KILL
kill
Added on January 26, 2024
SET_OPTION
set_option
Added on January 26, 2024
CHANGE_DB
change_db
Added on January 26, 2024
UNINSTALL_PLUGIN
uninstall_plugin
Added on January 26, 2024
INSTALL_PLUGIN
install_plugin
Added on January 26, 2024
SHUTDOWN
shutdown
Added on January 26, 2024
SLAVE_START
slave_start
Added on January 26, 2024
SLAVE_STOP
slave_stop
Added on January 26, 2024
LOCK_TABLES
lock_tables
Added on January 26, 2024
UNLOCK_TABLES
unlock_tables
Added on January 26, 2024
FLUSH
flush
Added on January 26, 2024
XA
xa_commit,xa_end,xa_prepare,xa_recover,xa_rollback,xa_start
Added on January 26, 2024
Disabling SQL audit
To disable SQL audit, toggle
(enabled) to
(disabled).If you select the check box "I acknowledge that after audit log is disabled, all audit logs are deleted." and click OK, all audit logs will be deleted.
Deleted audit logs cannot be recovered. Exercise caution when performing this operation.
- To enable SQL audit, toggle on the switch (from
- Log in to the RDS console.
- Click
in the upper left corner and select a region. - On the Instances page, click the target instance name.
- In the navigation pane, choose Logs.
- On the SQL Audit Logs tab page, click Ingest Logs to LTS in the upper right corner.
- In the displayed dialog box, select Ingest Logs to LTS, select Audit log under Log Types, select a log group and log stream, and click OK. Figure 2 Enabling audit log ingestion to LTS
- If audit log ingestion to LTS is enabled, you can switch to the analysis view in the upper right corner of the page to see more details. Figure 3 List view
Figure 4 Analysis view
- On the Log Search tab page, you can view structured fields and index configuration fields under Quick Analysis as well as view and download log contents. For more information, see Searching and Analyzing Logs.
For explanations of the fields on the Raw Logs page in the analysis view, see Table 6.
Table 6 LTS audit log field description Parameter
Description
logType
Log type. Fixed value: audit_log.
instanceId
Instance ID.
nodeId
Node ID within an instance.
_resource_id
Resource ID. Fixed value: DBS_AUDIT_LOG. Ignore this parameter.
_resource_name
Resource name. Fixed value: DBS_AUDIT_LOG. Ignore this parameter.
_service_type
Service type. Fixed value: MySQL.
record_id
ID of a record, which is the unique global ID of each SQL statement recorded in the audit log.
connection_id
ID of the session executed for the record, which is the same as the ID in the show processlist command output.
connection_status
Session status, which is usually the returned error code of a statement. If a statement is successfully executed, the value 0 is returned.
name
Name of the record type. Generally, DML and DDL operations are QUERY, connection and disconnection operations are CONNECT and QUIT, respectively.
timestamp
UTC time when the SQL statement is executed.
command_class
SQL command type. The value is the parsed SQL type, for example, select or update. (This field does not exist if the connection is disconnected.)
sqltext
Executed SQL statement content. (This field does not exist if the connection is disconnected.)
user
Login account.
host
Login host. The value is localhost for local login and is empty for remote login.
external_user
External username.
ip
IP address of the remotely-connected client. For local connection, the field is empty.
default_db
Default database on which SQL statements are executed.
trx_id
ID of the transaction in which the SQL statement is executed.
execute_time
Time required for executing the SQL statement, in milliseconds.
- On the Charts tab page, you can view scenario-specific data in multiple chart types, including tables, bar charts, and line charts. For more information, see Statistical Charts.
- On the Log Analysis tab page, you can search for and analyze collected log data. For more information, see Log Search and Analysis.
- On the Real-Time Logs tab page, you can view logs reported in real time to quickly search for and analyze log data. For more information, see Viewing Real-Time Logs.
- On the Log Search tab page, you can view structured fields and index configuration fields under Quick Analysis as well as view and download log contents. For more information, see Searching and Analyzing Logs.
FAQ
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot