查看实例日志
本页介绍如何查找和使用 Cloud Logging 以查看和查询 Cloud SQL 实例的日志。
Cloud SQL 使用 Cloud Logging。如需了解完整信息,请参阅 Cloud Logging 文档并查看 Cloud SQL 示例查询。
查看日志
通过
启用 general_log
标志并将
log_output
标志设置为
FILE
,您可以在常规日志文件中查看对数据库实例执行的 SQL 查询,包括由最终用户直接执行的 SQL 查询。
如需查看 Cloud SQL 实例日志条目的日志,请执行以下操作:
控制台
-
在 Trusted Cloud 控制台中,前往 Cloud Logging 页面。
转到 Cloud Logging
- 在页面顶部选择一个现有 Cloud SQL 项目。
- 在查询构建器中,添加以下内容:
- 资源:选择 Cloud SQL 数据库。在该对话框中,选择一个 Cloud SQL 实例。
- 日志名称:滚动到 Cloud SQL 部分,并为您的实例选择相应的日志文件。例如:
- cloudsql.googleapis.com/mysql-general.log
- cloudsql.googleapis.com/mysql.err
- 严重程度:选择一个日志级别。
- 时间范围:选择预设范围或创建自定义范围。
gcloud
使用 gcloud logging
命令查看日志条目。在下面的示例中,替换 PROJECT_ID
。
limit
标志是一个可选参数,用于指示要返回的最大条目数。
gcloud logging read "resource.type=cloudsql_database" \
--project=PROJECT-ID \
--limit=10 \
--format=json
查看实例操作日志
您可以在操作窗格中查看实例的日志。操作窗格会将对实例执行的所有操作记录到日志中,并包含以下信息:
- 操作完成的时间,按照您的本地时区报告。
- 操作的类型。
- 操作的状态。
- 说明操作结果的消息。
如果操作失败,您可以使用该消息来排查问题。
如需查看实例操作日志,请执行以下操作:
-
在 Trusted Cloud 控制台中,前往 Cloud SQL 实例页面。
转到“Cloud SQL 实例”
-
如需打开实例的概览页面,请点击实例名称。
- 点击操作切换到显示操作日志的窗格。
注意:操作日志不包含使用外部管理工具(如 mysql 客户端)执行的操作。只有使用 Trusted Cloud 控制台、
gcloud
命令行工具或 Cloud SQL Admin API 执行的用户管理和密码更改操作才会出现在操作日志中。
查看应用日志
连接到 Cloud SQL 的应用会将其日志存储在不同位置。
App Engine(柔性环境)
在计算 > App Engine > 服务中:
- 在服务列表中,找到您的服务。
- 点击工具下拉菜单。
- 选择日志
在 Trusted Cloud 控制台的操作 > Logging > 日志浏览器部分中,使用以下查询:
resource.type="gae_app"
resource.labels.module_id="default"
Cloud Run
在Trusted Cloud 控制台的 Cloud Run 日志浏览器部分中查看日志。请注意,Cloud Run 仅报告来自 Cloud SQL 身份验证代理的错误消息。使用如下所示的查询:
resource.type="cloud_run_revision"
resource.labels.service_name="$SERVICE_NAME"
resource.labels.revision_name="$REVISION_NAME"
Cloud SQL Auth 代理
在操作 > Logging > 日志浏览器中,使用以下查询:
log_id("appengine.googleapis.com/cloud-sql-proxy")
查看审核日志
您可以查看 Cloud SQL 实例的以下类型的审核日志:
- 管理员活动:包括写入元数据或配置信息的管理员操作。您无法停用这些日志。
- 数据访问:包括读取元数据或配置信息的管理员操作。这些日志还包括读取或写入用户提供的数据的操作。如需接收数据访问审核日志,您必须明确启用这些日志。
- 系统事件:标识 Trusted Cloud by S3NS 中修改资源配置的自动操作。您无法停用这些日志。
如需详细了解如何查看管理员活动、数据访问和系统事件审核日志,请参阅查看日志。
价格
如需详细了解 Cloud Logging 价格,请参阅 Cloud Logging 价格摘要。
问题排查
问题 |
问题排查 |
未找到审核日志。 |
只有当操作是经过身份验证的用户进行的 API 调用(用于创建、修改或读取用户创建的数据),或者操作访问配置文件或资源元数据时,才会写入数据访问日志。 |
在日志中找不到操作信息。 |
您想要详细了解某项操作。
例如,用户已被删除,但您找不到谁执行了此操作。日志显示操作已开始,但未提供任何更多信息。您必须为要记录的详细个人身份信息 (PII) 等启用审核日志记录。
|
Logging 占用了大量磁盘空间。 |
有三种类型的日志文件占用磁盘空间:重做日志、常规日志和二进制日志。 连接到数据库并运行以下命令以获取每种类型的详细信息:
SHOW VARIABLES LIKE 'innodb_log_file%';
SELECT ROUND(SUM(LENGTH(argument)/POW(1024,2),2)
AS GB from mysql.general_log;
SHOW BINARY LOGS;
|
日志文件难以读取。 |
最好以 json 或文本形式查看日志。您可以使用 gcloud logging read 命令以及 Linux 后处理命令来下载日志。如需将日志下载为 JSON,请执行以下操作:
gcloud logging read \
"resource.type=cloudsql_database \
AND logName=projects/PROJECT_ID \
/logs/cloudsql.googleapis.com%2FLOG_NAME" \
--format json \
--project=PROJECT_ID \
--freshness="1d" \
> downloaded-log.json
如需将日志下载为 TEXT,请执行以下操作:
gcloud logging read \
"resource.type=cloudsql_database \
AND logName=projects/PROJECT_ID \
/logs/cloudsql.googleapis.com%2FLOG_NAME" \
--format json \
--project=PROJECT_ID \
--freshness="1d"| jq -rnc --stream 'fromstream(1|truncate_stream(inputs)) \
| .textPayload' \
--order=asc
> downloaded-log.txt
|
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-03。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-03。"],[],[],null,["# View instance logs\n\n\u003cbr /\u003e\n\nMySQL \\| [PostgreSQL](/sql/docs/postgres/logging \"View this page for the PostgreSQL database engine\") \\| [SQL Server](/sql/docs/sqlserver/logging \"View this page for the SQL Server database engine\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis page describes how to find and use Cloud Logging to view and query logs\nfor your Cloud SQL instance.\n\nCloud SQL uses Cloud Logging. See the [cloud logging documentation](/logging/docs)\nfor complete information and review the\n[Cloud SQL sample queries](/logging/docs/view/query-library-preview#sql-filters).\n\nView logs\n---------\n\nYou can view SQL queries executed on the database instance, including SQL queries executed directly by end users, in the general log file by [enabling the `general_log`\nflag](/sql/docs/mysql/flags#tips) and setting the `log_output` flag to `FILE`.\n\nTo view logs for your Cloud SQL instance log entries: \n\n### Console\n\n1. In the Google Cloud console, go to the **Cloud Logging** page.\n\n [Go to Cloud Logging](https://console.cloud.google.com/logs/query)\n2. Select an existing Cloud SQL project at the top of the page.\n3. In the Query builder, add the following:\n - Resource: select **Cloud SQL Database**. In the dialog, select a Cloud SQL instance.\n - Log names: scroll to the Cloud SQL section and select appropriate log files for your instance. For example:\n - cloudsql.googleapis.com/mysql-general.log\n - cloudsql.googleapis.com/mysql.err\n - Severity: select a log level.\n - Time range: select a preset or create a custom range.\n\n### gcloud\n\nUse the [`gcloud logging`](/sdk/gcloud/reference/logging/read)\ncommand to view log entries. In the example below, replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e.\nThe [`limit`](/sdk/gcloud/reference/logging/read#--limit)\nflag is an optional parameter that indicates the maximum number of entries to\nreturn. \n\n```bash\ngcloud logging read \"resource.type=cloudsql_database\" \\\n--project=PROJECT-ID \\\n--limit=10 \\\n--format=json\n```\n\nView instance operations log\n----------------------------\n\nYou can view the logs for an instance in the **Operations** pane.\nThe **Operations** pane logs every operation performed on the\ninstance with the following information:\n\n- The time the operation completed, reported in your local time zone.\n- The type of operation.\n- The status of the operation.\n- A message describing the outcome the operation.\n\nIf the operation fails, you can use the message to troubleshoot the problem.\n\n**To view an instance operations log:**\n\n1. In the Google Cloud console, go to the **Cloud SQL Instances** page.\n\n [Go to Cloud SQL Instances](https://console.cloud.google.com/sql)\n2. To open the **Overview** page of an instance, click the instance name.\n3. Click **Operations** to change to the pane showing the operation log.\n\n**Note:** The operations log does not include operations performed using external management tools, such as the mysql client. Only user management and password change operations performed using the Google Cloud console, `gcloud` command-line tool, or the Cloud SQL Admin API appear in the operations log.\n\n\u003cbr /\u003e\n\nView application logs\n---------------------\n\nApplications that connect to Cloud SQL store their logs in different\nlocations.\n\n### App Engine (flexible environment)\n\nIn **Compute \\\u003e App Engine \\\u003e Services**:\n\n- In the list of services, find your service.\n- Click on the **Tools** dropdown.\n- Select **logs**\n\nIn the **Operations \\\u003e Logging \\\u003e Logs explorer** section of Google Cloud console,\nuse the following query: \n\n resource.type=\"gae_app\"\n resource.labels.module_id=\"default\"\n\n### Cloud Run\n\nView the logs in the Cloud Run **Logs Explorer** section of the\nGoogle Cloud console. Note that Cloud Run reports only error messages from\nthe Cloud SQL Auth Proxy. Use a query like the following: \n\n resource.type=\"cloud_run_revision\"\n resource.labels.service_name=\"$SERVICE_NAME\"\n resource.labels.revision_name=\"$REVISION_NAME\"\n\n### Cloud SQL Auth Proxy\n\nIn **Operations \\\u003e Logging \\\u003e Logs explorer**, use the following query: \n\n log_id(\"appengine.googleapis.com/cloud-sql-proxy\")\n\nView audit logs\n---------------\n\nYou can view the following types of audit logs for your Cloud SQL instances:\n\n- Admin Activity: include administrator operations that write metadata or configuration information. You can't deactivate these logs.\n- Data Access: include administrator operations that read metadata or configuration information. These logs also include operations that read or write user-provided data. To receive Data Access audit logs, you must [enable them explicitly](/logging/docs/audit/configure-data-access#config-console-enable).\n- System Event: identify automated actions in Google Cloud that modify the configuration of resources. You can't deactivate these logs.\n\nFor more information about viewing Admin Activity, Data Access, and System Event audit logs, see [View logs](/sql/docs/mysql/audit-logging#viewing_logs).\n\nPricing\n-------\n\nFor more information about Cloud Logging pricing, see\n[Cloud Logging pricing summary](/stackdriver/pricing#logs-costs).\n\nTroubleshoot\n------------"]]