外观
TiUP 工具集使用指南
核心组件
| 组件 | 功能描述 | 主要用途 |
|---|---|---|
| tiup cluster | 集群生命周期管理 | 部署、启动、停止、升级、扩容、缩容集群 |
| tiup bench | 性能测试工具 | 运行基准测试,评估集群性能 |
| tiup br | 备份恢复工具 | 执行全量备份、增量备份和恢复操作 |
| tiup cdc | CDC 管理工具 | 管理 TiCDC 集群和同步任务 |
| tiup playground | 本地测试集群 | 快速部署本地测试 TiDB 集群 |
| tiup pd-ctl | PD 管理工具 | 管理和监控 PD 集群 |
| tiup tikv-ctl | TiKV 管理工具 | 管理和监控 TiKV 集群 |
| tiup tidb-ctl | TiDB 管理工具 | 管理和监控 TiDB 集群 |
| tiup mirror | 镜像管理工具 | 管理 TiUP 组件镜像 |
架构设计
TiUP 采用客户端-服务器架构:
- 客户端:用户直接交互的命令行工具
- 镜像服务器:存储 TiUP 组件和 TiDB 版本包
- 本地存储:缓存下载的组件和版本包
TiUP 安装与配置
安装 TiUP
bash
# 下载并安装 TiUP
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
# 刷新环境变量
source ~/.bash_profile
# 验证安装
which tiup
tiup --version基本配置
bash
# 设置镜像源(默认使用官方镜像)
tiup mirror set https://tiup-mirrors.pingcap.com
# 查看当前镜像源
tiup mirror show
# 设置超时时间
export TIUP_TIMEOUT=600
# 设置日志级别
export TIUP_LOG_LEVEL=info升级 TiUP
bash
# 升级 TiUP 自身
tiup update --self
# 升级所有组件
tiup update --all
# 升级指定组件
tiup update cluster核心组件使用
tiup cluster - 集群管理
集群部署
bash
# 1. 生成拓扑文件
cat > topology.yaml << EOF
global:
user: "tidb"
ssh_port: 22
deploy_dir: "/tidb-deploy"
data_dir: "/tidb-data"
pd_servers:
- host: 192.168.1.101
- host: 192.168.1.102
- host: 192.168.1.103
tikv_servers:
- host: 192.168.1.104
- host: 192.168.1.105
- host: 192.168.1.106
tidb_servers:
- host: 192.168.1.107
- host: 192.168.1.108
monitoring_servers:
- host: 192.168.1.109
grafana_servers:
- host: 192.168.1.109
EOF
# 2. 检查拓扑文件
tiup cluster check topology.yaml --apply --user tidb
# 3. 部署集群
tiup cluster deploy <cluster-name> v7.5.0 topology.yaml --user tidb
# 4. 启动集群
tiup cluster start <cluster-name>
# 5. 查看集群状态
tiup cluster display <cluster-name>集群管理
bash
# 停止集群
tiup cluster stop <cluster-name>
# 重启集群
tiup cluster restart <cluster-name>
# 滚动重启集群
tiup cluster reload <cluster-name>
# 查看集群日志
tiup cluster logs <cluster-name> -R tidb
# 查看集群配置
tiup cluster config <cluster-name> --md5
# 修改集群配置
tiup cluster edit-config <cluster-name>
# 重新加载配置
tiup cluster reload <cluster-name> -R tikv集群扩容
bash
# 1. 修改拓扑文件,添加新节点
# 2. 执行扩容
tiup cluster scale-out <cluster-name> topology.yaml --user tidb集群缩容
bash
# 缩容指定节点
tiup cluster scale-in <cluster-name> --node 192.168.1.104:20160
# 强制缩容(危险操作,谨慎使用)
tiup cluster scale-in <cluster-name> --node 192.168.1.104:20160 --force集群升级
bash
# 升级集群
tiup cluster upgrade <cluster-name> v7.5.1
# 滚动升级
tiup cluster upgrade <cluster-name> v7.5.1 --rollingtiup bench - 性能测试
支持的测试类型
- oltp:在线事务处理测试
- tpcc:TPC-C 基准测试
- ycsb:YCSB 基准测试
- ch:ClickHouse 基准测试
执行 OLTP 测试
bash
# 准备测试数据
tiup bench oltp --host <tidb-ip> --port 4000 --user root --db test --tables 10 --table-size 1000000 prepare
# 执行只读测试
tiup bench oltp read-only --host <tidb-ip> --port 4000 --user root --db test --tables 10 --table-size 1000000 --threads 16 --time 60 run
# 执行读写测试
tiup bench oltp read-write --host <tidb-ip> --port 4000 --user root --db test --tables 10 --table-size 1000000 --threads 16 --time 60 run
# 执行写入测试
tiup bench oltp write-only --host <tidb-ip> --port 4000 --user root --db test --tables 10 --table-size 1000000 --threads 16 --time 60 run
# 清理测试数据
tiup bench oltp --host <tidb-ip> --port 4000 --user root --db test --tables 10 cleanup执行 TPC-C 测试
bash
# 准备测试数据
tiup bench tpcc --warehouses 10 --threads 4 prepare -D test -h <tidb-ip> -P 4000 -u root
# 执行测试
tiup bench tpcc --warehouses 10 --threads 16 run -D test -h <tidb-ip> -P 4000 -u root
# 检查测试结果
tiup bench tpcc --warehouses 10 check -D test -h <tidb-ip> -P 4000 -u roottiup br - 备份恢复
全量备份与恢复
bash
# 全量备份到本地
tiup br backup full --pd <pd-ip>:2379 --storage local:///path/to/backup
# 全量备份到 S3
tiup br backup full --pd <pd-ip>:2379 --storage s3://<bucket>/<backup-path> --s3.endpoint <endpoint> --s3.access-key <access-key> --s3.secret-access-key <secret-key>
# 从本地恢复
tiup br restore full --pd <pd-ip>:2379 --storage local:///path/to/backup
# 从 S3 恢复
tiup br restore full --pd <pd-ip>:2379 --storage s3://<bucket>/<backup-path> --s3.endpoint <endpoint> --s3.access-key <access-key> --s3.secret-access-key <secret-key>增量备份与恢复
bash
# 增量备份(基于时间范围)
tiup br backup incr --pd <pd-ip>:2379 --storage local:///path/to/incr-backup --lastbackupts <last-backup-ts>
# 增量恢复
tiup br restore incr --pd <pd-ip>:2379 --storage local:///path/to/incr-backup表级备份与恢复
bash
# 备份单个表
tiup br backup table --pd `pd-ip`:2379 --storage local:///path/to/table-backup --db `database` --table `table`
# 恢复单个表
tiup br restore table --pd `pd-ip`:2379 --storage local:///path/to/table-backup --db `database` --table `table`
# 备份多个表
tiup br backup table --pd `pd-ip`:2379 --storage local:///path/to/tables-backup --db `database` --table `table1` --table `table2`查看备份状态
bash
# 查看备份历史
tiup br list --pd <pd-ip>:2379 --storage local:///path/to/backup
# 查看备份详情
tiup br status --pd <pd-ip>:2379 --storage local:///path/to/backup --backup-id <backup-id>tiup cdc - CDC 管理
管理 CDC 集群
bash
# 列出 CDC 捕获实例
tiup cdc cli capture list --server=http://<cdc-ip>:8300
# 查看 CDC 集群状态
tiup cdc cli server status --server=http://<cdc-ip>:8300管理同步任务
bash
# 创建同步任务
tiup cdc cli changefeed create --server=http://<cdc-ip>:8300 --sink-uri="mysql://root@<target-tidb-ip>:4000/" --changefeed-id="replication-task"
# 列出同步任务
tiup cdc cli changefeed list --server=http://<cdc-ip>:8300
# 查看同步任务状态
tiup cdc cli changefeed query --server=http://<cdc-ip>:8300 --changefeed-id="replication-task"
# 暂停同步任务
tiup cdc cli changefeed pause --server=http://<cdc-ip>:8300 --changefeed-id="replication-task"
# 恢复同步任务
tiup cdc cli changefeed resume --server=http://<cdc-ip>:8300 --changefeed-id="replication-task"
# 删除同步任务
tiup cdc cli changefeed remove --server=http://<cdc-ip>:8300 --changefeed-id="replication-task"tiup playground - 本地测试集群
bash
# 启动默认配置的本地集群
tiup playground
# 启动指定版本的集群
tiup playground v7.5.0
# 启动包含 TiFlash 的集群
tiup playground --tiflash 1
# 启动指定组件数量的集群
tiup playground --pd 3 --tikv 3 --tidb 2 --tiflash 1
# 停止本地集群
# 在启动集群的终端按 Ctrl+C其他工具组件
tiup pd-ctl
bash
# 连接 PD 集群
tiup pd-ctl -u http://<pd-ip>:2379
# 查看集群状态
pd-ctl> cluster
# 查看成员状态
pd-ctl> member
# 查看 Region 分布
pd-ctl> store
# 查看调度状态
pd-ctl> operator showtiup tikv-ctl
bash
# 查看 TiKV 状态
tiup tikv-ctl --host <tikv-ip>:20160 status
# 查看 Region 信息
tiup tikv-ctl --host <tikv-ip>:20160 region --region-id <region-id>
# 查看 RocksDB 状态
tiup tikv-ctl --host <tikv-ip>:20160 rocksdb statstiup tidb-ctl
bash
# 查看 TiDB 状态
tiup tidb-ctl status --host <tidb-ip> --port 10080
# 查看 TiDB 配置
tiup tidb-ctl config --host <tidb-ip> --port 10080高级功能
镜像管理
创建私有镜像
bash
# 1. 初始化私有镜像
tiup mirror init <mirror-dir>
# 2. 修改镜像配置
# 编辑 <mirror-dir>/mirror.json 文件
# 3. 同步官方镜像到私有镜像
tiup mirror sync --from https://tiup-mirrors.pingcap.com <mirror-dir>
# 4. 启动镜像服务
tiup mirror server --listen 0.0.0.0:8080 --root <mirror-dir>
# 5. 使用私有镜像
tiup mirror set http://<mirror-ip>:8080离线部署
bash
# 1. 在有网络的机器上下载所需版本
tiup mirror clone <offline-mirror-dir> --os=linux --arch=amd64 tidb v7.5.0
# 2. 将离线镜像目录复制到目标机器
# 3. 在目标机器上设置离线镜像
tiup mirror set <offline-mirror-dir>配置管理
配置模板
bash
# 查看配置模板
tiup cluster template
# 生成指定版本的配置模板
tiup cluster template --version v7.5.0配置检查
bash
# 检查配置语法
tiup cluster check-config <config-file>
# 验证配置是否适用于指定版本
tiup cluster check-config <config-file> --version v7.5.0日志管理
查看日志
bash
# 查看集群日志
tiup cluster logs <cluster-name>
# 查看指定组件的日志
tiup cluster logs <cluster-name> -R tidb
# 查看指定节点的日志
tiup cluster logs <cluster-name> -N 192.168.1.107:4000
# 查看最近的日志
tiup cluster logs <cluster-name> -R tidb --tail 100
# 查看指定时间范围的日志
tiup cluster logs <cluster-name> -R tikv --since "2023-01-01 00:00:00" --until "2023-01-01 01:00:00"日志收集
bash
# 收集集群日志
tiup cluster collect <cluster-name> --start-time="2023-01-01 00:00:00" --end-time="2023-01-01 01:00:00" --include-system-logs最佳实践
部署最佳实践
规划拓扑结构
- 根据业务需求选择合适的拓扑结构
- 遵循 PD、TiKV、TiDB 节点分离的原则
- 为监控组件分配独立节点
配置优化
- 根据硬件配置调整组件参数
- 启用压缩以减少存储空间
- 配置合适的日志级别和保留策略
安全配置
- 启用 SSH 密钥认证
- 配置防火墙规则,限制访问范围
- 启用 TiDB 密码认证
运维最佳实践
定期备份
- 制定合理的备份策略
- 定期测试备份恢复流程
- 多副本存储备份数据
监控与告警
- 配置完善的监控告警规则
- 定期检查监控数据
- 及时处理告警信息
升级策略
- 在测试环境验证新版本
- 选择业务低峰期进行升级
- 制定回滚计划
容量规划
- 监控存储空间使用情况
- 提前规划扩容方案
- 定期清理无用数据
性能优化
TiUP Bench 测试
- 定期运行基准测试
- 对比不同版本的性能差异
- 根据测试结果调整配置
资源调度
- 调整 PD 调度参数
- 优化 Region 分布
- 处理热点问题
故障处理
快速定位问题
- 查看集群状态
- 分析日志信息
- 使用诊断工具
故障恢复
- 根据故障类型选择恢复方案
- 执行恢复操作
- 验证恢复结果
事后分析
- 记录故障原因和处理过程
- 总结经验教训
- 优化运维流程
常见问题(FAQ)
Q1: TiUP 安装失败,提示网络连接错误怎么办?
A1: 可以尝试以下解决方案:
- 检查网络连接是否正常
- 尝试使用代理安装:
export https_proxy=http://proxy:port && curl ... - 手动下载安装包并安装
- 使用离线安装方式
Q2: 部署集群时提示 SSH 连接失败怎么办?
A2: 请检查以下内容:
- SSH 服务是否正常运行:
systemctl status sshd - 防火墙是否允许 SSH 访问:
firewall-cmd --list-ports - SSH 端口是否正确:默认 22
- 用户名和密码是否正确
- 是否配置了 SSH 免密登录:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@host
Q3: 执行备份时提示权限不足怎么办?
A3: 请确保:
- 备份目录存在且有写入权限:
mkdir -p /path/to/backup && chmod 777 /path/to/backup - TiDB 用户有访问备份目录的权限
- 如果使用 S3 备份,检查访问密钥是否正确
Q4: 如何查看 TiUP 支持的 TiDB 版本?
A4: 可以使用以下命令查看:
bash
tiup list tidbQ5: 如何清理 TiUP 缓存?
A5: 可以清理以下目录:
bash
# 清理 TiUP 缓存
rm -rf ~/.tiup/cache/*
# 清理 TiUP 镜像缓存
rm -rf ~/.tiup/mirrors/*Q6: TiUP 命令执行缓慢怎么办?
A6: 可以尝试以下优化:
- 增加超时时间:
export TIUP_TIMEOUT=600 - 调整日志级别:
export TIUP_LOG_LEVEL=warn - 检查网络连接
- 升级 TiUP 到最新版本
Q7: 如何回滚 TiUP 集群升级?
A7: 可以使用以下命令回滚:
bash
tiup cluster revert <cluster-name>Q8: 如何迁移 TiUP 管理的集群?
A8: 迁移步骤:
- 在新机器上安装 TiUP
- 复制集群元数据:
scp -r ~/.tiup/storage/cluster/clusters/cluster-name new-machine:~/.tiup/storage/cluster/clusters/ - 在新机器上验证集群状态:
tiup cluster display cluster-name
命令参考
tiup cluster 常用命令
| 命令 | 功能描述 |
|---|---|
tiup cluster deploy | 部署集群 |
tiup cluster start | 启动集群 |
tiup cluster stop | 停止集群 |
tiup cluster restart | 重启集群 |
tiup cluster reload | 滚动重启集群 |
tiup cluster upgrade | 升级集群 |
tiup cluster scale-out | 扩容集群 |
tiup cluster scale-in | 缩容集群 |
tiup cluster display | 查看集群状态 |
tiup cluster logs | 查看集群日志 |
tiup cluster collect | 收集集群日志 |
tiup cluster edit-config | 编辑集群配置 |
tiup cluster check | 检查集群状态 |
tiup br 常用命令
| 命令 | 功能描述 |
|---|---|
tiup br backup full | 执行全量备份 |
tiup br backup incr | 执行增量备份 |
tiup br backup table | 执行表级备份 |
tiup br restore full | 执行全量恢复 |
tiup br restore incr | 执行增量恢复 |
tiup br restore table | 执行表级恢复 |
tiup br list | 查看备份列表 |
tiup br status | 查看备份状态 |
tiup bench 常用命令
| 命令 | 功能描述 |
|---|---|
tiup bench oltp | 运行 OLTP 测试 |
tiup bench tpcc | 运行 TPC-C 测试 |
tiup bench ycsb | 运行 YCSB 测试 |
tiup bench ch | 运行 ClickHouse 测试 |
