TiDB 应急运维脚本,更加方便的管理TiDB集群

这篇具有很好参考价值的文章主要介绍了TiDB 应急运维脚本,更加方便的管理TiDB集群。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

TiDB 应急运维脚本,更加方便的管理TiDB集群

使用方法

使用方法:

[tidb@localhost ~]$ which tiup
~/.tiup/bin/tiup

编辑脚本,MYSQL_PASSWD 和 PORT 根据实际替换 
[tidb@localhost ~]$ vi ~/.tiup/bin/ti


#version 1.1
#@author guanguanglei
##########################################################
#date 2023/06/14 version 1.0


#!/bin/sh

MYSQL_USER=root
MYSQL_PASSWD=root 
PORT=4000          
TIDB_HOST=127.0.0.1
TIDB_CONNSTR="mysql -u$MYSQL_USER -p$MYSQL_PASSWD -h$TIDB_HOST -P$PORT"
CLUSTER_NAME=gsc-quotation
case $1 in 
  si)
  ${TIDB_CONNSTR}
  ;;
  info)
  ${TIDB_CONNSTR} -e "select * from information_schema.CLUSTER_INFO;"
  ;;
  user)
  ${TIDB_CONNSTR} -e "select user,host from mysql.user;"
  ;;
  tidb)
  tiup cluster display  $CLUSTER_NAME
  ;;
  edit)
  tiup cluster edit-config  $CLUSTER_NAME
  ;;
  var)
  if [ "$2"x != x ]; then
  ${TIDB_CONNSTR} -e "show variables like '%$2%';"
  else 
    echo "please input second paramter with variable name"
  fi
  ;;
  config)
  if [ "$2"x != x ]; then
  ${TIDB_CONNSTR} -e "show config where name  like '%$2%';"
  else
    echo "please input config "
  fi
  ;;
  list)
  ${TIDB_CONNSTR} -e "select * from information_schema.cluster_processlist;"
  ;;
  listt)
  ${TIDB_CONNSTR} -e "select * from information_schema.cluster_processlist order by time desc ;"
  ;;
  active)
  ${TIDB_CONNSTR} -e "select * from information_schema.cluster_processlist where info is not null order by time desc \G;"
  ;;
  kill)
  ${TIDB_CONNSTR} -e "select concat(concat('mysql -u$MYSQL_USER -p$MYSQL_PASSWD -h ',substring_index(a.INSTANCE,':',1), ' -P',substring_index(a.INSTANCE,':',-1) ,' -e '  ),concat('\"kill tidb ',b.id,';\"')) from information_schema.CLUSTER_info a join information_schema.cluster_processlist b on a.STATUS_ADDRESS=b.instance;"
  ;;
  kstat)
  if [ "$2"x != x ]; then
  ${TIDB_CONNSTR} -e "select concat(concat('mysql -u$MYSQL_USER -p$MYSQL_PASSWD -h ',substring_index(a.INSTANCE,':',1), ' -P',substring_index(a.INSTANCE,':',-1) ,' -e '  ),concat('\"kill tidb ',b.id,';\"')) from information_schema.CLUSTER_info a join information_schema.cluster_processlist b on a.STATUS_ADDRESS=b.instance where state='$2';"
  else 
    echo "please input second paramter with session state"
  fi
  ;;
  ksql)
  if [ "$2"x != x ]; then
  ${TIDB_CONNSTR} -e "select concat(concat('mysql -u$MYSQL_USER -p$MYSQL_PASSWD -h ',substring_index(a.INSTANCE,':',1), ' -P',substring_index(a.INSTANCE,':',-1) ,' -e '  ),concat('\"kill tidb ',b.id,';\"')) from information_schema.CLUSTER_info a join information_schema.cluster_processlist b on a.STATUS_ADDRESS=b.instance where digest='$2';"
  else
    echo "please input second paramter with sql digest"
  fi
  ;;
  kid)
  if [ "$2"x != x ]; then
  ${TIDB_CONNSTR} -e "select concat(concat('mysql -u$MYSQL_USER -p$MYSQL_PASSWD -h ',substring_index(a.INSTANCE,':',1), ' -P',substring_index(a.INSTANCE,':',-1) ,' -e '  ),concat('\"kill tidb ',b.id,';\"')) from information_schema.CLUSTER_info a join information_schema.cluster_processlist b on a.STATUS_ADDRESS=b.instance  where id='$2';"
  else
    echo "please input second paramter with connect id"
  fi
  ;;
  klike)
  if [ "$2"x != x ]; then
  ${TIDB_CONNSTR} -e "select concat(concat('mysql -u$MYSQL_USER -p$MYSQL_PASSWD -h ',substring_index(a.INSTANCE,':',1), ' -P',substring_index(a.INSTANCE,':',-1) ,' -e '  ),concat('\"kill tidb ',b.id,';\"')) from information_schema.CLUSTER_info a join information_schema.cluster_processlist b on a.STATUS_ADDRESS=b.instance  where info like '%$2%';"
  else
    echo "please input info like "
  fi
  ;;
  plan)
  if [ "$2"x != x ]; then
  ${TIDB_CONNSTR} -e  "select SUMMARY_BEGIN_TIME,SUMMARY_END_TIME,STMT_TYPE,SCHEMA_NAME,DIGEST,DIGEST_TEXT,TABLE_NAMES,INDEX_NAMES,QUERY_SAMPLE_TEXT,PLAN_DIGEST,PLAN from information_schema.statements_summary where digest='$2'\G;"
  else
    echo "please input second paramter with sql digest"
  fi
  ;;
  size)
  if [ "$2"x != x ] && [ "$3"x != x ]; then
  ${TIDB_CONNSTR} -e  "SELECT TABLE_NAME,(DATA_LENGTH+INDEX_LENGTH)/1024/1024 mb,TABLE_ROWS FROM information_schema.TABLES WHERE TABLE_SCHEMA='$2' AND TABLE_NAME='$3';"
  else
    echo "please input second paramter with table owner and table_name"
  fi
  ;;
  index)
  if [ "$2"x != x ] && [ "$3"x != x ]; then
  ${TIDB_CONNSTR} -e  "show indexes from $2.$3;"
  else
    echo "please input second paramter table_owner table_name"
  fi
  ;;  
  stats_meta)
  if [ "$2"x != x ] && [ "$3"x != x ]; then
  ${TIDB_CONNSTR} -e  "show stats_meta where db_name='$2' table_name='$3';"
  else
    echo "please input second paramter table_owner table_name"
  fi
  ;; 
  stats_histograms)
  if [ "$2"x != x ] && [ "$3"x != x ]; then
  ${TIDB_CONNSTR} -e  "show stats_histograms where  db_name='$2' and db_name='$3';"
  else
    echo "please input second paramter table_owner table_name"
  fi
  ;;
  dump)
    if [ "$2"x != x ] && [ "$3"x != x ]; then
  ${TIDB_CONNSTR} -e  "select concat(' tiup dumpling -uroot -p   -h --filetype csv -t 8 -o /tmp/backup -f   ', '''', table_schema,'.',table_name,'''' ' --sql ''select * from ',table_name,'''') from tables where  table_schema='$2' and table_name='$3';"
  else
    echo "please input second paramter table_owner table_name"
  fi
  ;;
 ctl)
  ${TIDB_CONNSTR} -e  "select concat('tiup ctl:',(select substring(version(),13,19)),'  pd  -u  http://',  (select INSTANCE from information_schema.cluster_info where TYPE='pd' limit 1),'  -i') from dual;
"
  ;;
  *)
   echo
   echo "Usage: mib keyword [value1] ";
   echo 
   echo "  -----------------------------------------------------------------";
   echo "  si                          -- login tidb database";
   echo "  info                        -- show cluster info";
   echo "  tidb                        -- tiup cluster display";
   echo "  edit                        -- show cluster edit";
   echo "  user                        -- list all user";
   echo "  var                         -- show variables like ";
   echo "  config                      -- show config where name like ";
   echo "  list                        -- show  processlist";
   echo "  listt                       -- processlist order by time";  
   echo "  active                       -- processlist order by time limit 10";
   echo "  kill                        -- kill all session ";
   echo "  kstat                       -- kill session state";
   echo "  ksql                        -- kill sql session;";    
   echo "  kid                         -- kill session id";  
   echo "  klike                       -- kill info like "; 
   echo "  plan                        -- list sql plan";  
   echo "  size                        -- get table size ";
   echo "  index                       -- get  on table index";
   echo "  stats_meta                  -- getstats_meta on table";
   echo "  stats_histograms            -- get cloumn statistics on table";
   echo "  ctl                         -- print tiup ctl command";    
;;
esac


修改权限

[tidb@localhost ~]$ chmod 775 ~/.tiup/bin/ti

使用示例 文章来源地址https://www.toymoban.com/news/detail-651354.html


1. 查看集群状态
ti  tidb 

2. 登录数据库
[tidb@localhost ~]$ ti si
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 820992
Server version: 5.7.25-TiDB-v4.0.13-1-g7eeeda700 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

3.查看活跃会话
[tidb@localhost ~]$ ti active 
*************************** 1. row ***************************
INSTANCE: xxxxxxxx :10080
      ID: 820997
    USER: root
    HOST: 127.0.0.1
      DB: NULL
 COMMAND: Query
    TIME: 0
   STATE: 2
    INFO: select * from information_schema.cluster_processlist where info is not null order by time desc
  DIGEST: 6140c4093c70debc9c729807414f36c32fad049daecf2d5655afd10a2374fae9
     MEM: 6
TxnStart: 08-10 15:22:29.866(443456461174472707)

4. 登录pd 
[root@vm10-2-103-65 ~]# ti ctl
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| concat('tiup ctl:',(select substring(version(),13,19)),'  pd  -u  http://',  (select INSTANCE from information_schema.cluster_info where TYPE='pd' limit 1),'  -i') |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tiup ctl:v7.1.0  pd  -u  http://10.2.103.65:2379  -i                                                                                                                |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
[root@vm10-2-103-65 ~]# tiup ctl:v7.1.0  pd  -u  http://10.2.103.65:2379  -i 
Starting component `ctl`: /root/.tiup/components/ctl/v7.1.0/ctl pd -u http://10.2.103.65:2379 -i
»  

5. 查看参数
[root@vm10-2-103-65 ~]# ti var oom
+--------------------------------+--------+
| Variable_name                  | Value  |
+--------------------------------+--------+
| tidb_enable_tmp_storage_on_oom | ON     |
| tidb_mem_oom_action            | CANCEL |
+--------------------------------+--------+

查看config参数 
root@vm10-2-103-65 ~]# ti config block-cache
+------+--------------------+-------------------------------------------+----------+
| Type | Instance           | Name                                      | Value    |
+------+--------------------+-------------------------------------------+----------+
| tikv | 10.2.103.199:20160 | raftdb.defaultcf.block-cache-size         | 317MiB   |
| tikv | 10.2.103.199:20160 | raftdb.defaultcf.disable-block-cache      | false    |
| tikv | 10.2.103.199:20160 | raftdb.defaultcf.prepopulate-block-cache  | disabled |
| tikv | 10.2.103.199:20160 | rocksdb.defaultcf.block-cache-size        | 3971MiB  |
| tikv | 10.2.103.199:20160 | rocksdb.defaultcf.disable-block-cache     | false    |
| tikv | 10.2.103.199:20160 | rocksdb.defaultcf.prepopulate-block-cache | disabled |

6.查看执行计划
[root@vm10-2-103-65 ~]# ti plan 76069b6abc74fc855272108f656d4d1132948a55a3b12f66eaa4373648380d81
*************************** 1. row ***************************
SUMMARY_BEGIN_TIME: 2023-08-10 15:30:00
  SUMMARY_END_TIME: 2023-08-10 16:00:00
         STMT_TYPE: Select
       SCHEMA_NAME: NULL
            DIGEST: 76069b6abc74fc855272108f656d4d1132948a55a3b12f66eaa4373648380d81
       DIGEST_TEXT: select * from `information_schema` . `cluster_processlist` where `info` is not ? order by time desc
       TABLE_NAMES: information_schema.cluster_processlist
       INDEX_NAMES: NULL
 QUERY_SAMPLE_TEXT: select * from information_schema.cluster_processlist where info is not null order by time desc
       PLAN_DIGEST: deabf06ff5a37585b637b6bc4ddf0bb2affc0aa92148953d10f678359c7cda69
              PLAN:     id                      task            estRows operator info                                                   actRows execution info                                                                                                                                                                      memory          disk
        Sort_5                  root            9990    information_schema.cluster_processlist.time:desc                1       time:41.9ms, loops:2                                                                                                                                                                7.70 KB         0 Bytes
        └─TableReader_10        root            9990    data:Selection_9                                                1       time:41.9ms, loops:2, cop_task: {num: 2, max: 41.8ms, min: 41.5ms, avg: 41.6ms, p95: 41.8ms, rpc_num: 2, rpc_time: 83.2ms, copr_cache_hit_ratio: 0.00, max_distsql_concurrency: 2}  727 Bytes       N/A
          └─Selection_9         cop[tidb]       9990    not(isnull(information_schema.cluster_processlist.info))        0                                                                                                                                                                                           N/A             N/A
            └─TableFullScan_8   cop[tidb]       10000   table:CLUSTER_PROCESSLIST, keep order:false, stats:pseudo       0                                                                                                                                                                                           N/A             N/A


7. kill 会话 
[root@vm10-2-103-65 ~]# ti kid 716063544658887111
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| concat(concat('mysql -uroot -pM4Ab_d03%pQ27^1Xa# -h ',substring_index(a.INSTANCE,':',1), ' -P',substring_index(a.INSTANCE,':',-1) ,' -e '  ),concat('"kill tidb ',b.id,';"')) |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| mysql -uroot -pM4Ab_d03%pQ27^1Xa# -h 10.2.103.65 -P4000 -e "kill tidb 716063544658887111;"                                                                                    |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
[root@vm10-2-103-65 ~]#  mysql -uroot -pM4Ab_d03%pQ27^1Xa# -h 10.2.103.65 -P4000 -e "kill tidb 716063544658887111;"  


8.常用命令
[root@vm10-2-103-65 ~]# ti 

Usage: mib keyword [value1] 

  -----------------------------------------------------------------
  si                          -- login tidb database
  info                        -- show cluster info
  user                        -- list all user
  var                         -- show variables like 
  config                      -- show config where name like 
  list                        -- show  processlist
  listt                       -- processlist order by time
  active                       -- processlist order by time limit 10
  kill                        -- kill all session 
  kstat                       -- kill session state
  ksql                        -- kill sql session;
  kid                         -- kill session id
  klike                       -- kill info like 
  plan                        -- list sql plan
  size                        -- get table size 
  index                       -- get  on table index
  stats_meta                  -- getstats_meta on table
  stats_histograms            -- get cloumn statistics on table
  ctl                         -- print tiup ctl command

到了这里,关于TiDB 应急运维脚本,更加方便的管理TiDB集群的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 香橙派4和树莓派4B构建K8S集群实践之八: TiDB

    目录 1. 说明 2. 准备工作 3. 安装 3.1 参考Tidb官方 v1.5安装说明  3.2 准备存储类 3.3 创建crd 3.4 执行operator 3.5 创建cluster/dashboard/monitor容器组 3.6 设置访问入口(Ingress Port) 4. 装好后的容器状况 5. 遇到的问题 6. 参考 建立TiDB集群,实现一个基于k8s的云原生分布式数据库方案 应用i

    2024年02月13日
    浏览(29)
  • TiDB(2):TiDB架构特性

    1 TiDB 整体架构 TiDB 集群主要包括三个核心组件:TiDB Server,PD Server 和 TiKV Server。此外,还有用于解决用户复杂 OLAP 需求的 TiSpark 组件和简化云上部署管理的 TiDB Operator 组件。 架构图解   1.1 TiDB Server TiDB Server 负责接收 SQL 请求,处理 SQL 相关的逻辑,并通过 PD 找到存储计算所

    2024年02月12日
    浏览(29)
  • TiDB(6):数据迁移-TiDB Lightning

    1 TiDB Lightning介绍 TiDB Lightning 是一个将全量数据高速导入到 TiDB 集群的工具,目前支持 Mydumper 或 CSV 输出格式的数据源。你可以在以下两种场景下使用 Lightning: 迅速导入大量新数据。 备份恢复所有数据。 TiDB Lightning 主要包含两个部分: (1)tidb-lightning(“前端”):主要完

    2024年02月13日
    浏览(51)
  • TiDB(5):TiDB-读取历史数据

    接下来介绍 TiDB 如何读取历史版本数据,包括具体的操作流程以及历史数据的保存策略。 1 功能说明 TiDB 实现了通过标准 SQL 接口读取历史数据功能,无需特殊的 client 或者 driver。当数据被更新、删除后,依然可以通过 SQL 接口将更新/删除前的数据读取出来。 另外即使在更新

    2024年02月12日
    浏览(40)
  • TiDB(4):TiDB-SQL操作

    1 创建、查看和删除数据库 (1)要创建一个名为 samp_db 的数据库,可使用以下语句: (2)使用 SHOW DATABASES 语句查看数据库: (3)使用 DROP DATABASE 语句删除数据库,例如: (4)再次查看数据库: 2 创建、查看和删除表 (1)先创建一个库 (2)使用 SHOW TABLES 语句查看数据库

    2024年02月12日
    浏览(26)
  • TiDB实战篇-TiDB Lightning 导入数据

    使用TiDB Lightning 导入数据。 它是使用物理导入的模式,将SQL文件直接导入到TiKV中,它是一种初始化的导入,也就是说目标的数据库和表都是不能够存在的(注意事项,在这种方式导入的时候TiKV要切换到导入模式才行) 。      先导入数据,然后在导入索性。   Logical Impo

    2024年02月04日
    浏览(29)
  • TIDB简介及TIDB部署、原理和使用介绍

    数据库分类 ​ 介绍TiDB数据库之前,先引入使用场景。如今的数据库种类繁多,RDBMS(关系型数据库)、NoSQL(Not Only SQL)、NewSQL,在数据库领域均有一席之地,可谓百家争鸣之势。那么我们为什么要学习使用TiDB呢?接下来就从我们最熟悉的MySQL的使用说起。 MySQL痛点 ​ 假设

    2023年04月20日
    浏览(20)
  • TiDB 源码编译之 PD/TiDB Dashboard 篇

    作者: ShawnYan 原文来源: https://tidb.net/blog/a16b1d46 TiDB 是 PingCAP 公司自主设计、研发的开源分布式关系型数据库,是一款同时支持在线事务处理与在线分析处理 (Hybrid Transactional and Analytical Processing, HTAP) 的融合型分布式数据库产品,具备水平扩容或者缩容、金融级高可用、实时

    2024年02月14日
    浏览(29)
  • BenchmarkSQL 支持 TiDB 驱动以及 tidb-loadbalance

    作者: GangShen 原文来源: https://tidb.net/blog/3c274180 TiDB-JDBC **是基于 MySQL 8.0.29 的定制版本。TiDB-JDBC 基于 MySQL 官方 8.0.29 版本编译,修复了原 JDBC 在 prepare 模式下多参数、多字段 EOF 的错误,并新增 TiCDC snapshot 自动维护和 SM3 认证插件等功能。 tidb-loadbalance 是应用端的负载均衡

    2024年02月14日
    浏览(29)
  • [tidb] tiup升级tidb的版本到 v7.1.1

    为了避免数据丢失,升级前需要备份当前tidb集群的数据,参考 TiDB 备份与恢复概述 | PingCAP 文档中心 说明 由于新版本的tidb的tiflash需要cpui支持avx2,所有升级前先验证当前升级的服务器是否支持avx2。升级的文档可以参考 使用 TiUP 升级 TiDB | PingCAP 文档中心 ### 验证cpu是否支持

    2024年02月13日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包