You can call the DescribeSQLLogRecords operation to query the logs that are generated by the SQL Explorer (SQL Audit) feature for an instance.

Before you call this operation, make sure that the instance runs one of the following database engines:

  • MySQL
  • SQL Server
  • PostgreSQL
Note
  • You can call this operation up to 1,000 times per minute per account. The calls initiated by using both your Alibaba Cloud account and RAM users within your Alibaba Cloud account are counted.
  • This operation cannot be used to query the logs that are generated by SQL Explorer Trial Edition for an ApsaraDB RDS for MySQL instance.
  • When you call this operation and set the Form parameter to File to generate an audit file, a maximum of 1 million log entries can be recorded in the audit file, and you cannot filter log entries by keyword.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

Request parameters

Parameter Type Required Example Description
Action String Yes DescribeSQLLogRecords

The operation that you want to perform. Set the value to DescribeSQLLogRecords.

ClientToken String No ETnLKlblzczshOTUbOCzxxxxxxx

The client token that is used to ensure the idempotence of the request. You can use the client to generate the value, but you must make sure that the generated token is unique among different requests. The token can only contain ASCII characters and cannot exceed 64 characters in length.

DBInstanceId String Yes rm-uf6wjk5xxxxxxx

The ID of the instance.

SQLId Long No 25623548

The unique ID of the SQL statement.

QueryKeywords String No table_name

The keyword that is used for the query.

  • When you call this operation and set the Form parameter to File to generate an audit file, you cannot filter log entries by keyword.
  • You can specify up to 10 keywords. The keywords use a logical AND. Separate multiple keywords with spaces.
  • If a field name in the specified SQL statement is enclosed in backquotes (`) and you want to use the field name as a keyword, you must enter the backquotes (`) as part of the field name. For example, if the field name is `id`, you must enter `id` rather than id.
Note After you enter a keyword, the system matches the keyword based on the Database, User, and QueryKeywords parameters. The parameters use a logical OR.
StartTime String Yes 2011-06-01T15:00:00Z

The beginning of the time range to query. You can query the logs that are generated within the most recent 30 days. Specify the time in the ISO 8601 standard in the yyyy-MM-ddTHH:mm:ssZ format. The time must be in UTC.

Database String No Database

The name of the database. You can enter only one database name. If you specify this parameter, this operation returns only the logs that are generated for the specified database. If you do not specify this parameter, this operation returns the logs that are generated for all databases on the instance.

User String No user

The username of the account. You can enter only one username. If you specify this parameter, this operation returns only the logs that are generated for the specified account. If you do not specify this parameter, this operation returns the logs that are generated for all accounts on the instance.

Form String No Stream

Specifies whether to generate an SQL audit log file or return SQL audit log entries. Valid values:

  • File: If you set this parameter to File, this operation generates an SQL audit log file and returns only common response parameters. After you call this operation, you must call the DescribeSQLLogFiles operation to obtain the download URL of the SQL audit log file.
  • Stream: If you set this parameter to Stream, this operation returns SQL audit log entries. This is the default value.
Note If you set this parameter to File, only ApsaraDB RDS for MySQL instances that use local SSDs and ApsaraDB RDS for SQL Server instances are supported, and a maximum of 1 million log entries are returned.
EndTime String Yes 2011-06-11T15:00:00Z

The end of the time range to query. The end time must be later than the start time. The time span between the start time and the end time must be less than 30 days. Specify the time in the ISO 8601 standard in the yyyy-MM-ddTHH:mm:ssZ format. The time must be in UTC.

PageSize Integer No 30

The number of entries to return on each page. Valid values: 30 to 100. Default value: 30.

PageNumber Integer No 1

The number of the page to return. Valid values: any non-zero positive integer.

Default value: 1.

Response parameters

Parameter Type Example Description
RequestId String 08A3B71B-FE08-4B03-974F-CC7EA6DB1828

The ID of the request.

PageNumber Integer 1

The page number of the returned page.

PageRecordCount Integer 30

The number of SQL audit log entries on the current page.

TotalRecordCount Long 100

The total number of returned records.

Items Array of SQLRecord

An array that consists of the details of each SQL audit log entry.

SQLRecord
HostAddress String 192.168.0.121

The IP address of the client that is connected to the instance.

SQLText String update test.zxb set id=0 limit 1

The SQL statement that is executed in the query.

ReturnRowCounts Long 30

The number of SQL audit log entries that are returned.

DBName String testDB

The name of the database.

ExecuteTime String 2011-06-11T15:00:23Z

The time at which the SQL statement was executed. The time follows the yyyy-MM-ddTHH:mm:ssZ format. The time is displayed in UTC.

ThreadID String 1025865428

The ID of the thread.

TotalExecutionTimes Long 600

The execution duration of the SQL statement. Unit: microseconds.

AccountName String accounttest

The username of the account that is recorded in the SQL audit log entry.

Examples

Sample requests

http(s)://rds.aliyuncs.com/?Action=DescribeSQLLogRecords
&DBInstanceId=rm-uf6wjk5xxxxxx
&StartTime=2011-06-01T15:00:00Z
&EndTime=2011-06-11T15:00:00Z
&<Common request parameters>

Sample success responses

XML format

HTTP/1.1 200 OK
Content-Type:application/xml

<?xml version="1.0" encoding="UTF-8" ?>
<DescribeSQLLogRecordsResponse>
	<PageNumber>1</PageNumber>
	<TotalRecordCounts>1</TotalRecordCounts>
	<ItemsCounts>1</ItemsCounts>
	<SQLItems>
		<SQLItem>
			<DBName>test</DBName>
			<AccountName>accounttest</AccountName>
			<HostAddress>192.168.0.121</HostAddress>
			<SQLText>update test.zxb set id=0 limit 1</SQLText>
			<TotalExecutionTimes>12</TotalExecutionTimes>
			<ReturnRowCounts>34</ReturnRowCounts>
			<ExecuteTime>2011-06-11T15:00:23Z</ExecuteTime>
		</SQLItem>
	</SQLItems>
	<RequestId>08A3B71B-FE08-4B03-974F-CC7EA6DB1828</RequestId>
</DescribeSQLLogRecordsResponse>

JSON format

HTTP/1.1 200 OK
Content-Type:application/json

{
  "PageNumber" : 1,
  "TotalRecordCounts" : 1,
  "ItemsCounts" : 1,
  "SQLItems" : {
    "SQLItem" : [ {
      "DBName" : "test",
      "AccountName" : "accounttest",
      "HostAddress" : "192.168.0.121",
      "SQLText" : "update test.zxb set id=0 limit 1",
      "TotalExecutionTimes" : 12,
      "ReturnRowCounts" : 34,
      "ExecuteTime" : "2011-06-11T15:00:23Z"
    } ]
  },
  "RequestId" : "08A3B71B-FE08-4B03-974F-CC7EA6DB1828"
}

Error codes

For a list of error codes, see Service error codes.