8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

22.5.7 监控 X 插件22.5.7 Monitoring X Plugin

对于一般的 X 插件监控,请使用其公开的状态变量。参见 第 22.5.6.3 节,“X 插件状态变量”。有关专门监控消息压缩效果的信息,请参见 监控 X 插件的连接压缩。For general X Plugin monitoring, use the status variables that it exposes. See Section 22.5.6.3, “X Plugin Status Variables”. For information specifically about monitoring the effects of message compression, see Monitoring Connection Compression for X Plugin.

监控 X 插件生成的 SQLMonitoring SQL Generated by X Plugin

本节介绍如何监控 X 插件在执行 X DevAPI 操作时生成的 SQL 语句。当你执行 CRUD 语句时,它会被转换为 SQL 并在服务器上执行。为了能够监控生成的 SQL,必须启用性能模式(Performance Schema)表。SQL 会注册在 performance_schema.events_statements_current、performance_schema.events_statements_history 以及 performance_schema.events_statements_history_long 表中。以下示例使用 world_x 模式,该模式是本节快速入门教程的一部分。我们在 Python 模式下使用 MySQL Shell,并使用 \sql 命令,这可以在不切换到 SQL 模式的情况下发出 SQL 语句。这一点很重要,因为如果你试图切换到 SQL 模式,该过程将显示此操作的结果,而不是 X DevAPI 操作的结果。如果你在 JavaScript 模式下使用 MySQL Shell,\sql 命令的使用方式相同。This section describes how to monitor the SQL statements which X Plugin generates when you run X DevAPI operations. When you execute a CRUD statement, it is translated into SQL and executed against the server. To be able to monitor the generated SQL, the Performance Schema tables must be enabled. The SQL is registered under the performance_schema.events_statements_current, performance_schema.events_statements_history, and performance_schema.events_statements_history_long tables. The following example uses the world_x schema, imported as part of the quickstart tutorials in this section. We use MySQL Shell in Python mode, and the \sql command which enables you to issue SQL statements without changing to SQL mode. This is important, because if you instead try to switch to SQL mode, the procedure shows the result of this operation rather than the X DevAPI operation. The \sql command is used in the same way if you are using MySQL Shell in JavaScript mode.

  1. 检查 events_statements_history 消费者是否已启用。问题:Check if the events_statements_history consumer is enabled. Issue:

    mysql-py> \sql SELECT enabled FROM performance_schema.setup_consumers WHERE NAME = 'events_statements_history'
    +---------+
    | enabled |
    +---------+
    | YES     |
    +---------+
    
  2. 检查所有仪器是否向消费者报告数据。问题:Check if all instruments report data to the consumer. Issue:

    mysql-py> \sql SELECT NAME, ENABLED, TIMED FROM performance_schema.setup_instruments WHERE NAME LIKE 'statement/%' AND NOT (ENABLED and TIMED)

    如果此语句至少报告一行,则需要启用仪器。请参阅 第 29.4 节,“性能模式运行时配置”。If this statement reports at least one row, you need to enable the instruments. See Section 29.4, “Performance Schema Runtime Configuration”.

  3. 获取当前连接的线程 ID。问题:Get the thread ID of the current connection. Issue:

    mysql-py> \sql SELECT thread_id INTO @id FROM performance_schema.threads WHERE processlist_id=connection_id()
  4. 执行您想查看生成的 SQL 的 X DevAPI CRUD 操作。例如,问题:Execute the X DevAPI CRUD operation for which you want to see the generated SQL. For example, issue:

    mysql-py> db.CountryInfo.find("Name = :country").bind("country", "Italy")

    在下一步显示正确结果之前,您不能执行任何其他操作。You must not issue any further operations for the next step to show the correct result.

  5. 显示此线程 ID 执行的最后一个 SQL 查询。问题:Show the last SQL query made by this thread ID. Issue:

    mysql-py> \sql SELECT THREAD_ID, MYSQL_ERRNO,SQL_TEXT FROM performance_schema.events_statements_history WHERE THREAD_ID=@id ORDER BY TIMER_START DESC LIMIT 1;
    +-----------+-------------+--------------------------------------------------------------------------------------+
    | THREAD_ID | MYSQL_ERRNO | SQL_TEXT                                                                             |
    +-----------+-------------+--------------------------------------------------------------------------------------+
    |        29 |           0 | SELECT doc FROM `world_x`.`CountryInfo` WHERE (JSON_EXTRACT(doc,'$.Name') = 'Italy') |
    +-----------+-------------+--------------------------------------------------------------------------------------+
    

    结果显示 X 插件根据最近的语句生成的 SQL,在这种情况下是上一步的 X DevAPI CRUD 操作。The result shows the SQL generated by X Plugin based on the most recent statement, in this case the X DevAPI CRUD operation from the previous step.