{
    "componentChunkName": "component---src-templates-blog-blog-detail-tsx",
    "path": "/blog/tiup-a-tidbaer-must-have-tool",
    "result": {"pageContext":{"blog":{"id":"Blogs_375","title":"TiUP：TiDBAer 必备利器","tags":["TiUP"],"category":{"name":"案例实践"},"summary":"本文主要介绍构成 TiDB 可管理性的重要组件之一：TiUP，一款从 TiDB 4.0 版本开始投入使用的 TiDB 部署工具。","body":"> 本篇文章为 TiDB 社区专栏征文大赛的优秀作品，想要学习更多 TiDB 知识、获得更多 TiDB 周边，请关注 [TiDB 专栏](https://tidb.net/blog)\n\n对于企业级和云数据库，除了性能、可用性和功能等常规维度外，一个重要维度就是可管理性，可管理性维度会很深地影响用户实际使用数据库的隐性成本。在 TiDB 的[最新版本中](https://pingcap.com/zh/blog/tidb-6.0-release)，TiDB 引入了数据放置框架（Placement Rules In SQL），增加了企业级集群管理组件 TiDB Enterprise Manager ，开放了智能诊断服务 PingCAP Clinic 的预览，大幅度加强了作为企业级产品的可管理性，与此同时也加入了诸多云原生数据库所需的基础设施。\n\n温故而知新，本文主要介绍构成 TiDB 可管理性的重要组件之一：TiUP，一款从 TiDB 4.0 版本开始投入使用的 TiDB 部署工具。\nTiUP 对于 TiDBer 来说是日常必备工具，如果您刚接触 TiDB，请先参阅这篇文章：[《从马车到电动车，TiDB 部署工具变形记》](https://pingcap.com/zh/blog/tiup-introduction)。\n\n## 环境说明\n\n本文所涉及到的环境、组件版本信息如下：\n\n> TiDB v5.4.0\n>\n> TiUP v1.9.3 (2022-03-24 Release)\n>\n> CentOS 7.9\n\n## TiUP 简介\n\n在各种系统软件和应用软件的安装管理中，包管理器均有着广泛的应用，包管理工具的出现大大简化了软件的安装和升级维护工作。例如，几乎所有使用 RPM 的 Linux 都会使用 Yum 来进行包管理，而 Anaconda 则可以非常方便地管理 python 的环境和相关软件包。\n\n从 TiDB 4.0 版本开始，TiUP 作为新的工具，承担着包管理器的角色，管理着 TiDB 生态下众多的组件，如 TiDB、PD、TiKV 等。用户想要运行 TiDB 生态中任何组件时，只需要执行 TiUP 一行命令即可，相比以前，极大地降低了管理难度。\n\n![1.jpg](https://tva1.sinaimg.cn/large/e6c9d24ely1h192kqxaogj21ga0et75v.jpg)\n\n图 1-[TiUP GitHub Commits 趋势](https://github.com/pingcap/tiup/graphs/contributors)\n\n![2.jpg](https://tva1.sinaimg.cn/large/e6c9d24ely1h192kr9y52j20gr09h74t.jpg)\n\n图 2-tiup 源码行数统计(2022-03-24)\n\nTiUP 已发布两年有余，版本迭代若干次，代码总量翻倍，由上图可以看出代码更新已放缓，TiDBer 可放心地在生产环境使用。\n\n## 再论 TiUP 组件\n\n作为 Ti 利器，TiUP 可是日常工作必备。下面再来讨论下 TiUP 的经典组件和常用命令。先将 tiup 重要命令列举如下，然后再着重讨论。\n\n- tiup\n  - main/cmd/root\n    - tiup env\n    - **tiup status**\n    - tiup mirror\n    - tiup list --all --verbose\n    - tiup install hello\n    - tiup update\n    - **tiup playground**\n    - tiup client\n    - **tiup cluster**\n    - **tiup bench** **ch**(CH-benCHmark)/**TPCC**(TPC-C)/**TPCH**(TPC-H)/**YCSB**(Yahoo! Cloud Serving Benchmark)\n    - tiup dm\n    - tiup clean\n\n### tiup mirror\n\n不是每个公司都会把数据库部在公有云上的，即便是在公有云，为了便于版本统一管理，大都会选择自建一个仓库，是为生产库基准版本管理。而对于金融业务更不用多说，那么，如何在内网快速、简洁、有效地搭建和维护仓库，下面做个简单的示例。\n\n首先需要在能连接外网的机器安装 TiUP，并克隆官方 TiUP 仓库：\n\n- 下载 tiup 文件，并添加环境变量\n\n```\nmkdir -pv ~/.tiup/bin\nwget https://tiup-mirrors.pingcap.com/tiup-linux-amd64.tar.gz\ntar zxf tiup-linux-amd64.tar.gz -C ~/.tiup/bin/\necho 'export PATH=~/.tiup/bin:$PATH' >> ~/.bash_profile\nsource ~/.bash_profile\ntiup -v\n```\n\n输出的 TiUP 版本信息：\n\n```\n1.9.3 tiup\nGo Version: go1.17.7\nGit Ref: v1.9.3\nGitHash: f523cd5e051d0001e25d5a8b2c0d5d3ff058a5d5\n```\n\n- 克隆官方库\n\n先将仓库镜像指向官方库：\n\n```\ntiup mirror set https://tiup-mirrors.pingcap.com\n# 屏幕输出日志> Successfully set mirror to https://tiup-mirrors.pingcap.com\n```\n\n只克隆适合当前操作系统的最新版本，这里只需指定`TiDB v5.4.0`版本，其他组件会自动识别最新版本，并下载。\n\n```\ntiup mirror clone ~/.tiup/package -a amd64 -o linux v5.4.0\n```\n\n- 将 package 文件夹打包复制到内网机器：\n\n```\n# current server\ntar zcf package.tgz package/\n# new server\ncd ~/.tiup\ntar zxvf package.tgz\n./package/local_install.sh\nsource ~/.bash_profile\ntiup list\n```\n\n此时，新的本地仓已建好，创建一个`hello`组件进行测试：\n\n```\n# test mirror\nCMP_TMP_DIR=`mktemp -d -p ~/.tiup`\ncat > $CMP_TMP_DIR/hello.sh << EOF\n#! /bin/sh\necho -e \"\\033[0;36m<<< Hello, TiDB! >>>\\033[0m\"\nEOF\nchmod 755 $CMP_TMP_DIR/hello.sh\ntar -C $CMP_TMP_DIR -czf $CMP_TMP_DIR/hello.tar.gz hello.sh\n```\n\n- 将 `hello` 组件发布到本地仓：\n\n```\ntiup mirror genkey\ntiup mirror grant admin\ntiup mirror publish hello v0.0.1 $CMP_TMP_DIR/hello.tar.gz hello.sh --desc 'Hello, TiDB'\n```\n\n查看已发布的组件，并运行组件：\n\n```\ntiup list hello\ntiup hello\n```\n\n![3.jpg](https://tva1.sinaimg.cn/large/e6c9d24ely1h192krs2ioj20br07l0tk.jpg)\n\n图 3-hello 组件运行输出\n\n此时的本地仓以可以管理自发布组件，但仍无法对外提供服务，下面用`tiup server`一键搭建私有库：\n\n```\n# 运行tiup server\ntiup server ~/.tiup/package --addr 0.0.0.0:8000 --upstream=\"\"\n# 修改镜像指向\ntiup mirror set 'http://127.0.0.1:8000'\n```\n\n注：由于版本差异，当前版本已不建议使用环境变量`TIUP_MIRRORS`，而是建议使用命令`tiup mirror set <mirror-addr>`。\n\n### tiup playground\n\n对于分布式数据库来说，如何在本地快速搭建原型，以进行基本功能验证、测试，这是作为 DBA 的基本能力。于是，`tiup playground`应运而生，一键搭建最小可用集群，并可以指定初始 TiDB 每个组件启动的个数，以及扩缩容。\n\n例如，启动一个标签为`mydb1`的集群，包含一个 TiDB 实例，一个 TiKV 实例，一个 PD 实例，一个 TiFlash 实例，不启动监控组件：\n\n```\n$ tiup playground v5.4.0 --host 127.0.0.1 --tag mydb1 --db 1 --kv 1 --pd 1 --tiflash 1 --without-monitor\n127.0.0.1:4000 ... Done\n127.0.0.1:3930 ... Done\nCLUSTER START SUCCESSFULLY, Enjoy it ^-^\nTo connect TiDB: mysql --comments --host 127.0.0.1 --port 4000 -u root -p (no password)\nTo view the dashboard: http://127.0.0.1:2379/dashboard\nPD client endpoints: [127.0.0.1:2379]\n```\n\n查看各组件进程 id:\n\n```\n$ tiup playground display\nPid   Role     Uptime\n---   ----     ------\n4321  pd       10m39.092616075s\n4322  tikv     10m39.087748551s\n4353  tidb     10m37.765844216s\n4527  tiflash  9m50.16054123s\n```\n\n连接 tidb server，并查询版本：\n\n```\n$ mysql -uroot -h127.1 -P4000 -e 'select version()\\G'\n*************************** 1. row ***************************\nversion(): 5.7.25-TiDB-v5.4.0\n```\n\n再例如，启动一个标签为`mydb2`的集群，只启动 TiKV 实例，和 3 个 PD 实例：\n\n```\n$ tiup playground v5.4.0 --host 127.0.0.1 --tag mydb2 --mode tikv-slim --pd 3 --without-monitor\nPlayground Bootstrapping...\nStart pd instance:v5.4.0\nStart pd instance:v5.4.0\nStart pd instance:v5.4.0\nStart tikv instance:v5.4.0\nPD client endpoints: [127.0.0.1:2379 127.0.0.1:2382 127.0.0.1:2384]\n```\n\n通过 PD API 查看当前 PD 有几个成员，以及 TiKV 实例信息：\n\n```\n$ curl -s http://127.0.0.1:2379/pd/api/v1/members | jq .members[].name\n\"pd-1\"\n\"pd-0\"\n\"pd-2\"\n\n$ curl -s http://127.0.0.1:2379/pd/api/v1/stores | jq .stores[].store\n{\n  \"id\": 1,\n  \"address\": \"127.0.0.1:20160\",\n  \"version\": \"5.4.0\",\n  \"status_address\": \"127.0.0.1:20180\",\n  \"git_hash\": \"b5262299604df88711d9ed4b84d43e9c507749a2\",\n  \"start_timestamp\": 1648110516,\n  \"deploy_path\": \"/data/tiup/components/tikv/v5.4.0\",\n  \"last_heartbeat\": 1648112496884914000,\n  \"state_name\": \"Up\"\n}\n```\n\n### Misc\n\n性能测试也是必要环节，所以看到 TiUP 已经集成了 tpcc,tpch,ycsh,ch 四种测试工具集。可通过下面命令进行一键测试。\n\n```\ntiup bench ch/tpcc/tpch/ycsb\n```\n\n一键清理数据的命令如下：\n\n```\ntiup clean --all\n```\n\n这里需要强调的是，在生产环境需要慎重执行下面的指令，除非你知道你在做什么：\n\n```\ntiup cluster clean mydb3 --all\ntiup cluster destroy mydb3\ntiup cluster prune mydb3\n```\n\n![4.jpg](https://tva1.sinaimg.cn/large/e6c9d24ely1h192ksevybj20zv0i1n20.jpg)\n\n图 4-查看所有可用组件\n\n其他组件另行讨论，或请先参阅官方文档。\n\n## TiUP v1.9.3 Release\n\n2022-03-24，TiUP 发布了 v1.9.3 版本，从变更日志中我们可以清晰地了解到，本次更新修复了 5 个 bug，做了 2 点改进。\n\n> 修复：\n\n- 修复了主机名存在`-`时，`tiup-cluster`的`exec`子命令无法使用的错误。 ([#1794](https://github.com/pingcap/tiup/pull/1794), [@nexustar](https://github.com/nexustar))\n- 修复了使用`tiup-cluster`命令时，TiFlash 实例的端口（服务端口、代理端口、代理状态端口）冲突检测问题。 ([#1805](https://github.com/pingcap/tiup/pull/1805), [@AstroProfundis](https://github.com/AstroProfundis))\n- 修复了下一代监控(`ng-monitor`)在 Prometheus 不可用的问题。 ([#1806](https://github.com/pingcap/tiup/pull/1806), [@nexustar](https://github.com/nexustar))\n- 修复了在主机只部署 Prometheus 的情况下，node_exporter 度量指标无法收集的问题。（与上个问题一同修复。） ([#1806](https://github.com/pingcap/tiup/pull/1806), [@nexustar](https://github.com/nexustar))\n- 修复了`tiup-playground`在指定`--host 0.0.0.0`时，无法工作的问题。 ([#1811](https://github.com/pingcap/tiup/pull/1811), [@nexustar](https://github.com/nexustar))\n\n> 改进：\n\n- 支持`tiup-cluster`和`tiup-dm`命令清理审计日志。([#1780](https://github.com/pingcap/tiup/pull/1780), [@srstack](https://github.com/srstack))\n\n```\ntiup cluster audit cleanup\ntiup dm audit cleanup\n```\n\n- 在 Grafana 配置模板中增加匿名登陆示例。（这里需要确保 DM 组件版本在 v1.9.0 之上 => `tiup dm -v`） ([#1785](https://github.com/pingcap/tiup/pull/1785)[@sunzhaoyang](https://github.com/sunzhaoyang))\n\n\n\n## 引申思考\n\n云数据库时代，或者说分布式数据库时代下，DBA 角色该如何进行自我调整？我们是否还需要一个只会某种数据库运维的 DBA，比如只会 DB2、Oracle、MySQL、PostgreSQL 等传统关系型数据库，或者进阶的将，是否需要懂业务，有开发功底的业务型 DBA。其实，现在这些都没有过时，且不应该被抛弃，而应该作为基础功，变成 DBA 知识结构的底层模块，DBA 需要以此为基，向更高阶段进化，犹如诸位前辈经历那般，写得了数据库源码，懂得前端客户真正所需，才能开发、调校出适合业务场景的高效能数据库，以及一套好上手、易管理的数据库生态工具。\n\n毋庸置疑，TiUP 就是符合这种特质的工具。一键搭建私有库、一键运行最小集群、一键管理整个 TiDB 集群，并可对 TiDB 集群进行一键扩、缩容。不过，看似简单的背后也是有功能取舍的，比如，`tiup mirror`只能是在命令行进行操作，而没有类似 Nexus 这种可在浏览器进行发布、删除包的界面工具。又如，TiUP 最常用的场景还是运行在普通机器上，对于 k8s 环境有`TiDB Operator`工具，但是对于批量操作 ECS 的功能或案例，还是很少的。总之，希望 TiUP 在保持实用的前提下，功能更强劲。\n\n> 延展阅读：了解更多 TiUP 内容，请查阅[ TiUP 文档地图](https://docs.pingcap.com/zh/tidb/stable/tiup-documentation-guide)","date":"2022-04-14","author":"Shawn Yan","fillInMethod":"writeDirectly","customUrl":"tiup-a-tidbaer-must-have-tool","file":null,"relatedBlogs":[]}}},
    "staticQueryHashes": ["1327623483","1820662718","3081853212","3430003955","3649515864","4265596160","63159454"]}