当前位置:   article > 正文

数据库|TiDB-Server API的高效应用指南

数据库|TiDB-Server API的高效应用指南

一、API介绍

1.Status

显示TiDB 连接数、版本和git_hash 信息

tidb-server_ip:status_port/status

  1. {
  2. "connections": 0,
  3. "version": "5.7.25-TiDB-v6.1.1",
  4. "git_hash": "5263a0abda61f102122735049fd0dfadc7b7f822"
  5. }

2.Status

显示tidb所有的监控指标信息

tidb-server_ip:status_port/metrics

  1. # HELP br_raw_backup_region_seconds Backup region latency distributions.
  2. # TYPE br_raw_backup_region_seconds histogram
  3. br_raw_backup_region_seconds_bucket{le="0.05"} 0
  4. br_raw_backup_region_seconds_bucket{le="0.1"} 0
  5. br_raw_backup_region_seconds_bucket{le="0.2"} 0
  6. br_raw_backup_region_seconds_bucket{le="0.4"} 0
  7. br_raw_backup_region_seconds_bucket{le="0.8"} 0
  8. br_raw_backup_region_seconds_bucket{le="1.6"} 0
  9. br_raw_backup_region_seconds_bucket{le="3.2"} 0
  10. br_raw_backup_region_seconds_bucket{le="6.4"} 0

3.StatsDump

获取指定表的统计信息

直接访问会返回表不存在,原因是他的语法为/stats/dump/{db}/{table}

[schema:1146]Table '{db}.{table}'doesn't exist

e.g:tidb-server_ip:status_port/stats/dump/tpcc/item

  1. {
  2. "database_name": "tpcc",
  3. "table_name": "item",
  4. "columns": {
  5. "i_data": {
  6. "histogram": {
  7. "ndv": 99072,
  8. "buckets": [
  9. {
  10. "count": 390,
  11. "lower_bound": "MDAxaHFvVW14YW9iUVAxSVRYWHlLMXc4TXJxwjA4RG1BaA==",
  12. "upper_bound": "MEZWb0V6a2RiNmNuNFJoTmFZVkRqbHZ4b0hhUjN6WktuaFVFUkl6cHZlSUh1eGg=",
  13. "repeats": 1,
  14. "ndv": 0
  15. },
  16. ......

4.StatsHistoryDump

获取指定表的历史统计信息

直接访问返回

[types:1292]Incorrect datetime value: '{snapshot}'

正确的语法为

/stats/dump/{db}/{table}/{yyyy-MM-dd HH:mm:ss}

/stats/dump/{db}/{table}/{yyyyMMddHHmmss}

e.g:tidb-server_ip:status_port/stats/dump/tpcc/item/2023-12-19 10:00:39

  1. {
  2. "database_name": "tpcc",
  3. "table_name": "item",
  4. "columns": {
  5. "i_data": {
  6. "histogram": {
  7. "ndv": 99072,
  8. "buckets": [
  9. {
  10. "count": 390,
  11. "lower_bound": "MDAxaHFvVW14YW9iUVAxSVRYWHlLMXc4TXJxSjA4RG1BaA==",
  12. "upper_bound": "MEZWb0V6a2RiNmNuNFJoTmFZVkRqbHZ4b0hhUjN6WktuaFVFUkl6cHZlSUh1eGg=",
  13. "repeats": 1,
  14. "ndv": 0
  15. },
  16. {
  17. "count": 781,
  18. "lower_bound": "MEZmRTV5VDdaZkNZMktTOTFCZ3FKTmhzZFdOTlZHNjhw",
  19. "upper_bound": "MFVaTnc4UHdPOWlFMk8zbkdHZUdUbmZxcVNWVldqdFl5VE1lVmpXN2FWeG4=",
  20. "repeats": 1,
  21. "ndv": 0
  22. },
  23. ......

5.PlanReplayerDump

保存和恢复集群现场信息

PLAN REPLAYER DUMP EXPLAIN [ANALYZE] sql-statement;

TiDB 根据 sql-statement 整理出以下集群现场信息:

TiDB 版本信息

TiDB 配置信息

TiDB Session 系统变量

TiDB 执行计划绑定信息(SQL Binding)

sql-statement 中所包含的表结构

sql-statement 中所包含表的统计信息

EXPLAIN [ANALYZE] sql-statement 的结果

PLAN REPLAYER 不会导出表中数据

tidb_last_plan_replayer_token 这个会话变量可以获取上一次 PLAN REPLAYER dump 执行的结果。(v6.5 以上版本有此变量)

  1. MySQL [test]> plan replayer dump explain select * fromt1;
  2. +-----------------------------------------------------------+
  3. | File_token |
  4. +-----------------------------------------------------------+
  5. | replayer_xyUNbajB-Kx_rmD_Norcnw==_1702952402658503693.zip |
  6. +-----------------------------------------------------------+
  7. 1row inset(0.06sec)
  8. MySQL [test]> SELECT @@tidb_last_plan_replayer_token;
  9. +-----------------------------------------------------------+
  10. | @@tidb_last_plan_replayer_token |
  11. +-----------------------------------------------------------+
  12. | replayer_xyUNbajB-Kx_rmD_Norcnw==_1702952402658503693.zip |
  13. +-----------------------------------------------------------+
  14. 1row inset(0.00sec)

ZIP 文件最多会在 TiDB 集群中保存一个小时,超时后 TiDB 会将其删除。

plan replayer dump explain 'tidbsqls.txt';

多条 SQL 语句可以写在文件中以 ; 进行分隔。

通过http下载文件

http://${tidb-server-ip}:${tidb-server-status-port}/plan_replayer/dump/${file_token}

将导出的文件导入的另一个集群,导入完毕后,该 TiDB 集群就载入了所需要的表结构、统计信息等其他影响构造 Plan 所需要的信息。

PLAN REPLAYER LOAD 'plan_replayer.zip';

6.Settings

tidb-server 的所有配置信息

tidb-server_ip:status_port/settings

7.BinlogRecover

恢复 Pump 后恢复 binlog 写入。

返回值:

超时,返回状态码:400,消息:timeout

如果正常返回,状态码:200

默认情况下,API 将在等待所有跳过的二进制日志事务提交后返回。如果此值大于 0,则表示需要等到它们提交完毕。

消息示例:false表示当前binlog不处于跳过状态,否则为跳过状态:表示当前处于跳过状态的事务数。

  1. {
  2. "Skipped": false,
  3. "SkippedCommitterCounter": 0
  4. }

8.Schema

获取集群所有databases的详细信息

tidb-server_ip:status_port/schema

  1. [
  2. {
  3. "id": 1,
  4. "db_name": {
  5. "O": "test",
  6. "L": "test"
  7. },
  8. "charset": "utf8mb4",
  9. "collate": "utf8mb4_bin",
  10. "state": 5,
  11. "policy_ref_info": null
  12. },
  13. {
  14. "id": 3,
  15. "db_name": {
  16. "O": "mysql",
  17. "L": "mysql"
  18. },
  19. "charset": "utf8mb4",
  20. "collate": "utf8mb4_bin",
  21. "state": 5,
  22. "policy_ref_info": null
  23. },
  24. ......

tidb-server_ip:status_port/schema/{db}

查看指定database的详细信息,如该db下所有的table详细信息。

  1. [
  2. {
  3. "id": 147,
  4. "name": {
  5. "O": "t2",
  6. "L": "t2"
  7. },
  8. "charset": "utf8mb4",
  9. "collate": "utf8mb4_bin",
  10. "cols": [
  11. {
  12. "id": 1,
  13. "name": {
  14. "O": "id",
  15. "L": "id"
  16. },
  17. "offset": 0,
  18. "origin_default": null,
  19. "origin_default_bit": null,
  20. "default": null,
  21. "default_bit": null,
  22. "default_is_expr": false,
  23. "generated_expr_string": "",
  24. "generated_stored": false,
  25. "dependences": null,
  26. "type": {
  27. "Tp": 3,
  28. "Flag": 4099,
  29. "Flen": 11,
  30. "Decimal": 0,
  31. "Charset": "binary",
  32. "Collate": "binary",
  33. "Elems": null,
  34. "ElemsIsBinaryLit": null,
  35. "Array": false
  36. },
  37. "state": 5,
  38. "comment": "",
  39. "hidden": false,
  40. "change_state_info": null,
  41. "version": 2
  42. },
  43. ......

通过table_name 来获取table的详细信息

tidb-server_ip:status_port/schema/{db}/{table}

可以通过 tableID 获取 schema 信息(tableID 是 Table 在 TiDB 中的唯一标识符)

tidb-server_ip:status_port/schema?table_id={tableID}

  1. {
  2. "id": 147,
  3. "name": {
  4. "O": "t2",
  5. "L": "t2"
  6. },
  7. "charset": "utf8mb4",
  8. "collate": "utf8mb4_bin",
  9. "cols": [
  10. {
  11. "id": 1,
  12. "name": {
  13. "O": "id",
  14. "L": "id"
  15. },
  16. ......

通过 tableID 获取db_info、table_info和tidb info schema version

tidb-server_ip:status_port/db-table/{tableID}

与上面的获取table的详细信息相比多了db_info和schema version信息。

  1. {
  2. "db_info": {
  3. "id": 1,
  4. "db_name": {
  5. "O": "test",
  6. "L": "test"
  7. },
  8. "charset": "utf8mb4",
  9. "collate": "utf8mb4_bin",
  10. "state": 5,
  11. "policy_ref_info": null
  12. },
  13. ......
  14. ......
  15. "schema_version": 119
  16. }

9.Schema Storage

获取集群所有table的信息

tidb-server_ip:status_port/schema_storage

  1. [
  2. {
  3. "table_schema": "INFORMATION_SCHEMA",
  4. "table_name": "CLIENT_ERRORS_SUMMARY_GLOBAL",
  5. "table_rows": 0,
  6. "avg_row_length": 0,
  7. "data_length": 0,
  8. "max_data_length": 0,
  9. "index_length": 0,
  10. "data_free": 0
  11. },
  12. ......

也可以指定获取指定database或者table的信息

获取db下所有table的信息

tidb-server_ip:status_port/schema_storage/{db}

获取指定table的信息

tidb-server_ip:status_port/schema_storage/{db}/{table}

  1. {
  2. "table_schema": "test",
  3. "table_name": "t1",
  4. "table_rows": 4,
  5. "avg_row_length": 8,
  6. "data_length": 32,
  7. "max_data_length": 0,
  8. "index_length": 0,
  9. "data_free": 0
  10. }

10.DDL_History

获取所有 TiDB DDL 历史记录

tidb-server_ip:status_port/ddl/history

  1. [
  2. {
  3. "id": 2,
  4. "type": 1,
  5. "schema_id": 1,
  6. "table_id": 0,
  7. "schema_name": "test",
  8. "table_name": "",
  9. "state": 6,
  10. "warning": null,
  11. "err": null,
  12. "err_count": 0,
  13. "row_count": 0,
  14. "raw_args": null,
  15. "schema_state": 5,
  16. "snapshot_ver": 0,
  17. "real_start_ts": 0,
  18. "start_ts": 444336503938613250,
  19. "dependency_id": 0,
  20. "query": "CREATE DATABASE IF NOT EXISTS test",
  21. "binlog": {
  22. "SchemaVersion": 1,
  23. "DBInfo": {
  24. "id": 1,
  25. "db_name": {
  26. "O": "test",
  27. "L": "test"
  28. },
  29. "charset": "utf8mb4",
  30. "collate": "utf8mb4_bin",
  31. "state": 5,
  32. "policy_ref_info": null
  33. },
  34. "TableInfo": null,
  35. "FinishedTS": 444336503965089800,
  36. "MultipleTableInfos": null
  37. },
  38. "version": 1,
  39. "reorg_meta": null,
  40. "multi_schema_info": null,
  41. "priority": 0,
  42. "seq_num": 0,
  43. "charset": "",
  44. "collate": "",
  45. "admin_operator": 0
  46. },
  47. ......

11.DDL_Owner_Resign

辞去 ddl owner 的职务,让 tidb 开始新的 ddl owner 选举

curl -X POST http://{TiDBIP}:10080/ddl/owner/resign

注意:如果你请求的 TiDB 不是 ddl owner,则响应为This node is not a ddl owner, can't be resigned.

http://12.Info

tidb-server_ip:status_port/info

获取tidb-server的相关信息

  1. {
  2. "is_owner": true,
  3. "max_procs": 4,
  4. "gogc": 500,
  5. "version": "5.7.25-TiDB-v7.1.0",
  6. "git_hash": "635a4362235e8a3c0043542e62953e3c7bb2756",
  7. "ddl_id": "39844a19-b9d7-4f39-b21a-9beb5ffec18",
  8. "ip": "10.0.0.1",
  9. "listening_port": 5000,
  10. "status_port": 20080,
  11. "lease": "45s",
  12. "binlog_status": "On",
  13. "start_timestamp": 1695089378,
  14. "labels": {},
  15. "server_id": 937142
  16. }

13.InfoALL

tidb-server_ip:status_port/info/all

获取tidb集群所有tidb-server的信息

  1. {
  2. "servers_num": 1,
  3. "owner_id": "9112417d-2609-404a-8a8b-c10b5cebfe2b",
  4. "is_all_server_version_consistent": true,
  5. "all_servers_info": {
  6. "9112417d-2609-404a-8a8b-c10b5cebfe2b": {
  7. "version": "5.7.25-TiDB-v6.5.2",
  8. "git_hash": "29116c0256c52b224da2b34d712c1063d171c0ad",
  9. "ddl_id": "9112417d-2609-404a-8a8b-c10b5cebfe2b",
  10. "ip": "10.3.65.136",
  11. "listening_port": 4900,
  12. "status_port": 19080,
  13. "lease": "45s",
  14. "binlog_status": "On",
  15. "start_timestamp": 1703554804,
  16. "labels": {},
  17. "server_id": 4042427
  18. }
  19. }
  20. }

14.RegionsMeta

tidb-server_ip:status_port/regions/meta

获取所有region的元数据信息

  1. [
  2. {
  3. "region_id": 10,
  4. "leader": {
  5. "id": 128,
  6. "store_id": 2
  7. },
  8. "peers": [
  9. {
  10. "id": 11,
  11. "store_id": 1
  12. },
  13. {
  14. "id": 128,
  15. "store_id": 2
  16. },
  17. {
  18. "id": 178,
  19. "store_id": 4
  20. }
  21. ],
  22. "region_epoch": {
  23. "conf_ver": 5,
  24. "version": 156
  25. }
  26. }
  27. ]

15.RegionHot

tidb-server_ip:status_port/regions/hot

获取热点region的表/索引信息

  1. {
  2. "read": [
  3. {
  4. "region_id": 180,
  5. "region_metric": {
  6. "flow_bytes": 38560,
  7. "max_hot_degree": 2564,
  8. "region_count": 0
  9. },
  10. "db_name": "mysql",
  11. "table_name": "tidb_ddl_job",
  12. "table_id": 281474976710654,
  13. "index_name": "",
  14. "index_id": 0
  15. }
  16. ],
  17. "write": []
  18. }

16.Trace Viewer

tidb-server_ip:status_port/web/trace

以图形界面的方式显示执行sql的详细信息,参考 TRACE | PingCAP 文档中心 使用。

这个我实验无法显示出图形信息 https://asktug.com/t/topic/1014681,尝试了6.1、6.5、7.1环境都无法显示出来。

17.Debug

tidb-server_ip:status_port/debug/pprof/

tidb debug信息,例如heap、goroutine、trace等常用信息。

  1. Types of profiles available:
  2. Count Profile
  3. 12038allocs
  4. 0block
  5. 0cmdline
  6. 249goroutine
  7. 12038heap
  8. 85mutex
  9. 0profile
  10. 12threadcreate
  11. 0trace
  12. full goroutine stack dump
  13. Profile Descriptions:
  14. allocs:A sampling of allpast memory allocations
  15. block: Stack traces that led toblocking onsynchronization primitives
  16. cmdline: The commandlineinvocation of the current program
  17. goroutine: Stack traces of allcurrent goroutines
  18. heap: A sampling of memory allocations of live objects. You can specify the gc GET parameter torun GC before taking the heap sample.
  19. mutex: Stack traces of holders of contended mutexes
  20. profile: CPU profile. You can specify the duration in the seconds GET parameter. After you getthe profilefile, use the gotool pprof commandtoinvestigate the profile.
  21. threadcreate: Stack traces that led tothe creation of newOS threads
  22. trace: A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you getthe trace file, use the gotool trace commandtoinvestigate the trace.

二、结尾

以上API在一些运维场景中仍然相当实用。通过在运维中灵活运用,可以使运维工作更加便捷,增强系统管理的灵活性。尤其是在运维开发的场景中熟练使用,有助于高效地进行开发工作。

作者:王思强| 数据库运维工程师

版权声明:本文由神州数码云基地团队整理撰写,若转载请注明出处。

公众号搜索神州数码云基地,了解更多技术干货!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/512570
推荐阅读
相关标签
  

闽ICP备14008679号