Skip to content

TiDB TiUP 工具集详解

TiUP 是 TiDB 官方提供的集群部署和管理工具,它包含了多个组件,用于简化 TiDB 集群的部署、升级、扩容、缩容等操作。本文将详细介绍 TiUP 工具集的各个组件和使用方法。

TiUP 采用组件化架构,主要包括 TiUP 核心、TiUP 组件、TiUP 镜像和 TiUP 配置四个部分。它具有简单易用、组件化设计、版本管理、镜像机制和自动化操作等优势,能够简化 TiDB 集群的管理操作。

TiUP 核心命令

1. 安装与更新

bash
# 安装 TiUP
bash <(curl -s https://tiup-mirrors.pingcap.com/install.sh)

# 更新 TiUP
tiup update self

# 更新所有组件
tiup update --all

2. 组件管理

bash
# 查看所有可用组件
tiup list

# 查看特定组件的版本
tiup list <component>

# 安装特定组件
tiup install <component>

# 卸载特定组件
tiup uninstall <component>

# 查看已安装的组件
tiup installed

3. 组件执行

bash
# 执行组件
tiup <component> [args...]

# 使用特定版本的组件
tiup <component>:<version> [args...]

# 使用最新版本的组件
tiup <component>@latest [args...]

4. 配置管理

bash
# 查看 TiUP 配置
tiup config

# 设置 TiUP 配置
tiup config <key> <value>

# 重置 TiUP 配置
tiup config reset

5. 镜像管理

bash
# 查看当前镜像
tiup mirror show

# 设置镜像
tiup mirror set <mirror-url>

# 重置镜像
tiup mirror reset

TiUP 主要组件详解

1. cluster 组件

cluster 组件是 TiUP 最核心的组件,用于 TiDB 集群的部署和管理。

主要功能

  • 集群部署
  • 集群启动/停止/重启
  • 集群升级
  • 集群扩容/缩容
  • 集群配置更新
  • 集群状态查看
  • 集群日志查看

常用命令

bash
# 查看集群模板
tiup cluster template > topology.yaml

# 部署集群
tiup cluster deploy <cluster-name> <version> ./topology.yaml --user root -p

# 查看集群列表
tiup cluster list

# 查看集群状态
tiup cluster display <cluster-name>

# 启动集群
tiup cluster start <cluster-name>

# 停止集群
tiup cluster stop <cluster-name>

# 重启集群
tiup cluster restart <cluster-name>

# 升级集群
tiup cluster upgrade <cluster-name> <new-version>

# 扩容集群
tiup cluster scale-out <cluster-name> ./scale-out.yaml

# 缩容集群
tiup cluster scale-in <cluster-name> --node <node-id>

# 滚动更新配置
tiup cluster reload <cluster-name> [-N <nodes>] [-R <roles>]

# 查看集群日志
tiup cluster logs <cluster-name> [-N <nodes>] [-R <roles>] [-f]

# 收集集群诊断信息
tiup cluster diag <cluster-name> [-N <nodes>] [-R <roles>]

拓扑文件格式

yaml
# 全局配置
global:
  user: "tidb"
  ssh_port: 22
  deploy_dir: "/tidb-deploy"
  data_dir: "/tidb-data"

# 监控组件
monitoring_servers:
  - host: 10.0.0.1

grafana_servers:
  - host: 10.0.0.1

alertmanager_servers:
  - host: 10.0.0.1

# PD 组件
pd_servers:
  - host: 10.0.0.1
  - host: 10.0.0.2
  - host: 10.0.0.3

# TiKV 组件
tikv_servers:
  - host: 10.0.0.4
  - host: 10.0.0.5
  - host: 10.0.0.6

# TiDB 组件
tidb_servers:
  - host: 10.0.0.7
  - host: 10.0.0.8

# TiFlash 组件
tiflash_servers:
  - host: 10.0.0.9

2. bench 组件

bench 组件用于 TiDB 集群的性能测试,支持 YCSB 和 Sysbench 两种基准测试。

主要功能

  • YCSB 基准测试
  • Sysbench 基准测试
  • 自定义测试配置
  • 测试结果输出

常用命令

bash
# YCSB 测试
tiup bench ycsb run --db tidb --threads 100 --workload a --time 300s --db.config ./tidb.yaml

# Sysbench 测试
tiup bench sysbench --host <tidb-host> --port 4000 --user root --password '' --db test --tables 10 --table-size 1000000 --threads 100 --time 300s oltp_read_write run

# 准备测试数据
tiup bench sysbench --host <tidb-host> --port 4000 --user root --password '' --db test --tables 10 --table-size 1000000 oltp_read_write prepare

# 清理测试数据
tiup bench sysbench --host <tidb-host> --port 4000 --user root --password '' --db test --tables 10 --table-size 1000000 oltp_read_write cleanup

配置文件示例

yaml
# tidb.yaml
host: "127.0.0.1"
port: 4000
user: "root"
password: ""
db: "test"

3. ctl 组件

ctl 组件用于访问 TiDB、TiKV、PD 等组件的控制接口,进行集群管理和诊断。

主要功能

  • PD 集群管理
  • TiKV 节点管理
  • TiDB 节点管理
  • 集群状态查看
  • 诊断信息收集

常用命令

bash
# PD 控制

tiup ctl pd -u http://<pd-host>:2379 cluster
tiup ctl pd -u http://<pd-host>:2379 store
tiup ctl pd -u http://<pd-host>:2379 member
tiup ctl pd -u http://<pd-host>:2379 hot read
tiup ctl pd -u http://<pd-host>:2379 hot write
tiup ctl pd -u http://<pd-host>:2379 region <region-id>

# TiKV 控制

tiup ctl tikv --host <tikv-host>:20160 status
tiup ctl tikv --host <tikv-host>:20160 raft status
tiup ctl tikv --host <tikv-host>:20160 logs
tiup ctl tikv --host <tikv-host>:20160 threads
tiup ctl tikv --host <tikv-host>:20160 memory

# TiDB 控制

tiup ctl tidb --host <tidb-host> --port 10080 status
tiup ctl tidb --host <tidb-host> --port 10080 schema
tiup ctl tidb --host <tidb-host> --port 10080 stats

4. dm 组件

dm 组件用于管理 DM (Data Migration) 集群,用于从 MySQL/PostgreSQL 迁移数据到 TiDB。

主要功能

  • DM 集群部署
  • DM 集群管理
  • 迁移任务管理
  • 迁移状态监控

常用命令

bash
# 部署 DM 集群
tiup dm deploy <dm-cluster-name> <version> ./dm-topology.yaml --user root -p

# 查看 DM 集群列表
tiup dm list

# 查看 DM 集群状态
tiup dm display <dm-cluster-name>

# 启动 DM 集群
tiup dm start <dm-cluster-name>

# 停止 DM 集群
tiup dm stop <dm-cluster-name>

# 重启 DM 集群
tiup dm restart <dm-cluster-name>

# 创建数据源
tiup dmctl --master-addr <dm-master-host>:8261 operate-source create ./source.yaml

# 创建迁移任务
tiup dmctl --master-addr <dm-master-host>:8261 start-task ./task.yaml

# 查看迁移任务状态
tiup dmctl --master-addr <dm-master-host>:8261 query-status <task-name>

5. playground 组件

playground 组件用于快速部署本地 TiDB 测试集群,方便开发和测试。

主要功能

  • 快速部署本地 TiDB 集群
  • 支持多版本切换
  • 支持自定义集群规模
  • 支持浏览器访问

常用命令

bash
# 启动 playground
tiup playground

# 启动指定版本的 playground
tiup playground <version>

# 启动指定规模的 playground
tiup playground --db 2 --pd 3 --tikv 3

# 启动包含 TiFlash 的 playground
tiup playground --tiflash 1

# 后台启动 playground
tiup playground --daemon

# 停止 playground
tiup playground stop

6. br 组件

br 组件用于 TiDB 集群的备份和恢复,支持全量备份、增量备份和时间点恢复。

主要功能

  • 全量备份和恢复
  • 增量备份和恢复
  • 时间点恢复
  • 单库/单表备份和恢复
  • 支持多种存储介质

常用命令

bash
# 全量备份到本地
tiup br backup full --pd <pd-host>:<pd-port> --storage local:///path/to/backup

# 全量备份到 S3
tiup br backup full --pd <pd-host>:<pd-port> --storage s3://bucket/backup --s3.endpoint https://s3.amazonaws.com

# 增量备份
tiup br backup inc --pd <pd-host>:<pd-port> --storage local:///path/to/backup --lastbackupts <last-backup-ts>

# 全量恢复
tiup br restore full --pd <pd-host>:<pd-port> --storage local:///path/to/backup

# 时间点恢复
tiup br restore point --pd <pd-host>:<pd-port> --storage local:///path/to/backup --restored-ts <timestamp>

# 单库恢复
tiup br restore db --pd <pd-host>:<pd-port> --db <db-name> --storage local:///path/to/backup

# 单表恢复
tiup br restore table --pd <pd-host>:<pd-port> --db <db-name> --table <table-name> --storage local:///path/to/backup

7. dumpling 组件

dumpling 组件用于从 MySQL/TiDB 导出数据,支持多种输出格式。

主要功能

  • 全量数据导出
  • 支持多种输出格式(SQL, CSV, Parquet)
  • 支持并行导出
  • 支持按时间点导出
  • 支持压缩导出

常用命令

bash
# 导出全量数据
tiup dumpling -h <host> -P 4000 -u <user> -p <password> -o /path/to/output

# 导出指定数据库
tiup dumpling -h <host> -P 4000 -u <user> -p <password> -B <db-name> -o /path/to/output

# 导出指定表
tiup dumpling -h <host> -P 4000 -u <user> -p <password> -B <db-name> -T <table1>,<table2> -o /path/to/output

# 按时间点导出
tiup dumpling -h <host> -P 4000 -u <user> -p <password> --time <timestamp> -o /path/to/output

# 压缩导出
tiup dumpling -h <host> -P 4000 -u <user> -p <password> -o /path/to/output --compress gzip

# 使用并行导出
tiup dumpling -h <host> -P 4000 -u <user> -p <password> -o /path/to/output --threads 32

8. lightning 组件

lightning 组件用于向 TiDB 导入大规模数据,支持多种数据源。

主要功能

  • 大规模数据导入
  • 支持多种数据源(Dumpling, CSV, Parquet)
  • 支持高并发导入
  • 支持多种导入模式

常用命令

bash
# 使用配置文件导入
tiup tidb-lightning -config tidb-lightning.toml

# 查看帮助信息
tiup tidb-lightning --help

配置文件示例

toml
# tidb-lightning.toml
[lightning]
level = "info"
table-concurrency = 16
index-concurrency = 16

[tidb]
host = "<tidb-host>"
port = 4000
user = "root"
password = ""
db-name = "test_db"

[mydumper]
data-source-dir = "/path/to/data"

[tikv-importer]
backend = "local"
sorted-kv-dir = "/path/to/sorted-kv-dir"

TiUP 配置管理

1. 全局配置

TiUP 的全局配置文件位于 ~/.tiup/tiup.toml,用于配置 TiUP 的全局行为。

toml
# ~/.tiup/tiup.toml 示例
[global]
# 默认镜像地址
mirror = "https://tiup-mirrors.pingcap.com"
# 日志级别
log-level = "info"
# 并发数
concurrency = 5
# 是否启用诊断信息收集
enable-diag = true

2. 组件配置

每个组件都有自己的配置文件,用于配置组件的行为。组件配置文件通常位于 ~/.tiup/components/[component]/[version]/config.toml

3. 环境变量

TiUP 支持通过环境变量来配置其行为:

bash
# 设置 TiUP 日志级别
export TIUP_LOG_LEVEL=debug

# 设置 TiUP 镜像
export TIUP_MIRROR=https://tiup-mirrors.pingcap.com

# 设置 TiUP 数据目录
export TIUP_HOME=/path/to/tiup-home

TiUP 最佳实践

1. 生产环境使用建议

  • 使用固定版本:在生产环境中,建议使用固定版本的 TiUP 组件,避免版本变更带来的风险
  • 定期备份配置:定期备份 TiUP 的配置文件和拓扑文件
  • 使用私有镜像:对于安全性要求高的环境,建议部署私有镜像
  • 限制权限:TiUP 操作需要 root 权限,建议限制 TiUP 的使用权限
  • 记录操作日志:重要操作前,建议记录操作日志,便于后续审计和故障排查

2. 性能优化

  • 调整并发数:根据服务器性能,调整 TiUP 的并发数
  • 使用本地存储:对于大规模部署,建议使用本地存储来存储 TiUP 的数据
  • 优化网络连接:确保 TiUP 所在服务器与集群节点之间的网络连接稳定

3. 故障排查

  • 查看日志:使用 tiup [component] --log-file [file] 保存日志
  • 检查版本:确保 TiUP 组件版本与集群版本匹配
  • 验证配置:使用 tiup cluster check [cluster-name] --config 验证配置
  • 收集诊断信息:使用 tiup cluster diag [cluster-name] 收集诊断信息

TiUP 常见问题

1. TiUP 无法连接到集群节点

问题描述

TiUP 无法通过 SSH 连接到集群节点,提示连接超时或权限错误。

解决方案

  • 检查 SSH 服务是否正常运行
  • 检查 SSH 端口是否开放
  • 检查用户名和密码是否正确
  • 检查 SSH 密钥是否配置正确
  • 检查防火墙规则是否允许连接

2. 组件安装失败

问题描述

TiUP 无法安装组件,提示下载失败或校验失败。

解决方案

  • 检查网络连接是否正常
  • 切换到官方镜像:tiup mirror set https://tiup-mirrors.pingcap.com
  • 清理缓存:rm -rf ~/.tiup/cache
  • 使用代理:export HTTPS_PROXY=http://proxy:port

3. 集群部署失败

问题描述

使用 TiUP 部署集群失败,提示各种错误。

解决方案

  • 检查拓扑文件是否正确
  • 检查节点之间的网络连接
  • 检查节点的磁盘空间和内存
  • 查看详细日志:tiup cluster deploy [cluster-name] [version] ./topology.yaml --user root -p --verbose
  • 参考官方文档调整配置

4. TiUP 版本不兼容

问题描述

TiUP 组件版本与集群版本不兼容,导致操作失败。

解决方案

  • 使用与集群版本匹配的 TiUP 组件版本
  • 使用 tiup [component]:[version] 指定版本
  • 更新 TiUP 到最新版本:tiup update --all

常见问题(FAQ)

Q1: TiUP 和 TiDB Ansible 有什么区别?

A1: TiUP 是 TiDB 官方推荐的集群部署和管理工具,相比 TiDB Ansible,它具有以下优势:

  • 组件化设计,按需安装
  • 支持多版本管理
  • 更简单易用的命令行接口
  • 更好的错误处理和日志记录
  • 支持私有镜像

Q2: 如何升级 TiUP 组件?

A2: 可以使用以下命令升级 TiUP 组件:

bash
# 升级所有组件
tiup update --all

# 升级特定组件
tiup update <component>

Q3: 如何使用 TiUP 部署多版本集群?

A3: 可以使用以下命令部署特定版本的 TiDB 集群:

bash
tiup cluster deploy <cluster-name> <version> ./topology.yaml --user root -p

其中 [version] 可以指定具体的版本号,如 v6.5.0

Q4: 如何备份 TiUP 配置?

A4: TiUP 的配置和数据存储在 ~/.tiup 目录下,可以通过备份该目录来备份 TiUP 配置:

bash
tar -czf tiup-backup.tar.gz ~/.tiup

Q5: 如何使用 TiUP 管理多个集群?

A5: TiUP 支持管理多个集群,每个集群有独立的名称和配置:

bash
# 部署第一个集群
tiup cluster deploy cluster1 v6.5.0 ./topology1.yaml --user root -p

# 部署第二个集群
tiup cluster deploy cluster2 v6.5.0 ./topology2.yaml --user root -p

# 查看所有集群
tiup cluster list

# 管理特定集群
tiup cluster display cluster1
tiup cluster start cluster2