写点什么

TiDB 快速入门:从零到一 部署初探

  • 2024-01-26
    北京
  • 本文字数:7098 字

    阅读完需:约 23 分钟

原文来源:https://tidb.net/blog/926ee18a

作者:马顺华

从事运维管理工作多年,目前就职于某科技有限公司,熟悉运维自动化、OceanBase 部署运维、MySQL 运维以及各种云平台技术和产品。并已获得 OceanBase 认证 OBCA、OBCP 证书、OpenGauss 社区认证结业证书、崖山 DBCA 证书、亚信 AntDBCA 证书。OceanBase & 墨天轮第二、三、四届技术征文大赛,多次获得 一、二、三 等奖,时常在墨天轮发布原创技术文章,并多次被首页推荐。


                                   ![image.png](https://tidb-blog.oss-cn-beijing.aliyuncs.com/media/image-1706064888585.png)
复制代码


前言 TiDB 是一个开源的分布式关系型数据库,设计目标是提供高可用性、高扩展性和高性能的数据库服务。以下是 TiDB 数据库的快速上手指南:


本指南介绍如何快速上手体验 TiDB 数据库。对于非生产环境,你可以选择以下任意一种方式部署 TiDB 数据库:


部署本地测试集群(支持 macOS 和 Linux)在单机上模拟部署生产环境集群(本次使用 CentOS 7.4 环境测试)


[root@worker2 tidb]# cat /etc/centos-releaseCentOS Linux release 7.4.1708 (Core) [root@worker2 tidb]# uname -aLinux worker2 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux[root@worker2 tidb]# 
复制代码



安装 TiDB:可以从 TiDB 官网下载 TiDB 的安装包,并按照官方文档的指引进行安装。安装 MySQL 客户端:为了方便管理 TiDB 数据库,需要安装 MySQL 客户端。

一、TiDB 快速部署数据库可以按照以下步骤进行:

1. 准备环境:确保你的计算机上已经安装了 TiDB 所需的软件和工具,包括操作系统、内存、磁盘空间等。2. 下载 TiDB 安装包:从 TiDB 官网下载适用于你操作系统的 TiDB 安装包。3. 安装 TiDB:按照 TiDB 官方文档的指引进行安装,根据你的操作系统选择相应的安装方式。4. 配置 TiDB:在安装完成后,需要配置 TiDB 的相关参数,包括内存、线程、网络等。根据你的需求进行相应的配置。5. 启动 TiDB:启动 TiDB 服务,确保服务正常运行。你可以使用命令行工具或图形界面工具来启动和停止 TiDB 服务。6. 连接 TiDB:使用 MySQL 客户端或其他数据库管理工具连接到 TiDB 数据库,并进行相应的操作和管理。7. 测试数据:在 TiDB 上创建一个表格,插入一些测试数据,并进行查询和操作,以确保数据库的正常运行和功能正常。8. 监控和维护:使用 TiDB 自带的监控工具进行性能监控和故障排除,并根据需要进行相应的维护和优化。

二、TiDB 数据库快速上手指南

如需了解 TiUP 的具体使用方法和 TiDB 快速上手具体操作步骤,请以文档内容为准。


部署本地测试集群适用场景:利用本地 macOS 或者单机 Linux 环境快速部署 TiDB 测试集群,体验 TiDB 集群的基本架构,以及 TiDB、TiKV、PD、监控等基础组件的运行。


TiDB 是一个分布式系统。最基础的 TiDB 测试集群通常由 2 个 TiDB 实例、3 个 TiKV 实例、3 个 PD 实例和可选的 TiFlash 实例构成。通过 TiUP Playground,可以快速搭建出上述的一套基础测试集群,步骤如下:

1、下载并安装 TiUP。
[root@worker2 tidb]# curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100 4947k  100 4947k    0     0  7691k      0 --:--:-- --:--:-- --:--:-- 7694k
复制代码



安装完成后会提示如下信息:


Successfully set mirror to https://tiup-mirrors.pingcap.comDetected shell: bashShell profile:  /root/.bash_profile/root/.bash_profile has been modified to add tiup to PATHopen a new terminal or source /root/.bash_profile to use itInstalled path: /root/.tiup/bin/tiup===============================================Have a try:     tiup playground===============================================[root@worker2 tidb]# 
复制代码


2、声明全局环境变量。

注意 TiUP 安装完成后会提示 Shell profile 文件的绝对路径。在执行以下 source 命令前,需要将 ${your_shell_profile} 修改为 Shell profile 文件的实际位置。


[root@worker2 tidb]# source /root/.bash_profile[root@worker2 tidb]# 
复制代码


3、在当前 session 执行以下命令启动集群。

直接运行 tiup playground 命令会运行最新版本的 TiDB 集群,其中 TiDB、TiKV、PD 和 TiFlash 实例各 1 个:


tiup playground
复制代码


也可以指定 TiDB 版本以及各组件实例个数,命令类似于:


[root@worker2 tidb]# tiup playground v7.5.0 --db 2 --pd 3 --kv 3tiup is checking updates for component playground ...timeout(2s)!The component `playground` version  is not installed; downloading from repository.download https://tiup-mirrors.pingcap.com/playground-v1.14.1-linux-amd64.tar.gz 8.13 MiB / 8.13 MiB 100.00% 116.29 MiB/s             Starting component `playground`: /root/.tiup/components/playground/v1.14.1/tiup-playground v7.5.0 --db 2 --pd 3 --kv 3Start pd instance:v7.5.0The component `pd` version v7.5.0 is not installed; downloading from repository.download https://tiup-mirrors.pingcap.com/pd-v7.5.0-linux-amd64.tar.gz 49.97 MiB / 49.97 MiB 100.00% 1.20 MiB/s                      Start pd instance:v7.5.0Start pd instance:v7.5.0Start tikv instance:v7.5.0The component `tikv` version v7.5.0 is not installed; downloading from repository.download https://tiup-mirrors.pingcap.com/tikv-v7.5.0-linux-amd64.tar.gz 290.85 MiB / 290.85 MiB 100.00% 1.18 MiB/s                  Start tikv instance:v7.5.0Start tikv instance:v7.5.0Start tidb instance:v7.5.0The component `tidb` version v7.5.0 is not installed; downloading from repository.download https://tiup-mirrors.pingcap.com/tidb-v7.5.0-linux-amd64.tar.gz 78.51 MiB / 78.51 MiB 100.00% 1.18 MiB/s                    Start tidb instance:v7.5.0Waiting for tidb instances ready127.0.0.1:4000 ... Done127.0.0.1:4001 ... DoneThe component `prometheus` version v7.5.0 is not installed; downloading from repository.download https://tiup-mirrors.pingcap.com/prometheus-v7.5.0-linux-amd64.tar.gz 94.35 MiB / 94.35 MiB 100.00% 22.02 MiB/s             download https://tiup-mirrors.pingcap.com/grafana-v7.5.0-linux-amd64.tar.gz 50.14 MiB / 50.14 MiB 100.00% 33.73 MiB/s                Start tiflash instance:v7.5.0userConfig map[flash:map[proxy:map[config:/root/.tiup/data/U2AM3HZ/tiflash-0/tiflash_proxy.toml]] logger:map[level:debug]]The component `tiflash` version v7.5.0 is not installed; downloading from repository.download https://tiup-mirrors.pingcap.com/tiflash-v7.5.0-linux-amd64.tar.gz 250.76 MiB / 250.76 MiB 100.00% 1.13 MiB/s               Waiting for tiflash instances ready127.0.0.1:3930 ... Done
复制代码



上述命令会在本地下载并启动某个版本的集群(例如 v7.5.0)。最新版本可以通过执行 tiup list tidb 来查看。运行结果将显示集群的访问方式:


TiDB Playground Cluster is started, enjoy!
Connect TiDB: mysql --comments --host 127.0.0.1 --port 4000 -u rootConnect TiDB: mysql --comments --host 127.0.0.1 --port 4001 -u rootTiDB Dashboard: http://127.0.0.1:2379/dashboardGrafana: http://127.0.0.1:3000
复制代码



注意以这种方式执行的 playground,在结束部署测试后 TiUP 会清理掉原集群数据,重新执行该命令后会得到一个全新的集群。若希望持久化数据,可以执行 TiUP 的 –tag 参数:tiup –tag playground …,详情参考 TiUP 参考手册。

4、新开启一个 session 以访问 TiDB 数据库。

使用 TiUP client 连接 TiDB:


[root@worker2 tidb]# tiup clienttiup is checking updates for component client ...A new version of client is available:   The latest version:         v1.14.1   Local installed version:       Update current component:   tiup update client   Update all components:      tiup update --all
The component `client` version is not installed; downloading from repository.download https://tiup-mirrors.pingcap.com/client-v1.14.1-linux-amd64.tar.gz 4.81 MiB / 4.81 MiB 100.00% 276.38 MiB/s Starting component `client`: /root/.tiup/components/client/v1.14.1/tiup-clientConnected with driver mysql (8.0.11-TiDB-v7.5.0)Type "help" for help.
my:root@127.0.0.1:4000=>
复制代码



也可使用 MySQL 客户端连接 TiDB:


[root@worker2 tidb]# mysql --host 127.0.0.1 --port 4000 -u rootWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1933574152Server version: 8.0.11-TiDB-v7.5.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 8.0 compatible
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
复制代码



5、通过 http://127.0.0.1:9090 –host 参数指定监听网卡绑定外部可访问的 IP 访问 TiDB 的 Prometheus 管理界面。



6、通过 http://127.0.0.1:2379/dashboard –host 参数指定监听网卡绑定外部可访问的 IP 访问 TiDB Dashboard 页面,默认用户名为 root,密码为空。



7、通过 http://127.0.0.1:3000 –host 参数指定监听网卡绑定外部可访问的 IP 访问 TiDB 的 Grafana 界面,默认用户名和密码都为 admin。



8、(可选)将数据加载到 TiFlash 进行分析。


9、测试完成之后,可以通过执行以下步骤来清理集群:


按下 Control+C 键停掉上述启用的 TiDB 服务。


Wait prometheus(47927) to quit...Wait ng-monitoring(47928) to quit...Wait grafana(48130) to quit...Grafana quitGot signal interrupt (Component: playground ; PID: 32508)Wait tiflash(57424) to quit...ng-monitoring quitprometheus quittiflash quitWait tidb(47396) to quit...tidb quitWait tidb(47408) to quit...tidb quitWait tikv(44167) to quit...tikv quitWait tikv(44168) to quit...tikv quitWait tikv(44192) to quit...tikv quitWait pd(34223) to quit...pd quitWait pd(34229) to quit...pd quitWait pd(34239) to quit...pd quit[root@worker2 tidb]# 
复制代码



等待服务退出操作完成后,生产环境谨慎使用,执行以下命令:


[root@worker2 data]# tiup clean --all[root@worker2 data]# 
复制代码


注意 TiUP Playground 默认监听 127.0.0.1,服务仅本地可访问。若需要使服务可被外部访问,可使用 –host 参数指定监听网卡绑定外部可访问的 IP。


[root@worker2 tidb]# tiup playground v7.5.0 --db 2 --pd 3 --kv 3 --host 172.10.10.122tiup is checking updates for component playground ...Starting component `playground`: /root/.tiup/components/playground/v1.14.1/tiup-playground v7.5.0 --db 2 --pd 3 --kv 3 --host 172.10.10.122Start pd instance:v7.5.0Start pd instance:v7.5.0Start pd instance:v7.5.0Start tikv instance:v7.5.0Start tikv instance:v7.5.0Start tikv instance:v7.5.0Start tidb instance:v7.5.0Start tidb instance:v7.5.0Waiting for tidb instances ready172.10.10.122:4000 ... Done172.10.10.122:4001 ... DoneStart tiflash instance:v7.5.0userConfig map[flash:map[proxy:map[config:/root/.tiup/data/U2F6PYD/tiflash-0/tiflash_proxy.toml]] logger:map[level:debug]]Waiting for tiflash instances ready172.10.10.122:3930 ... Done
 TiDB Playground Cluster is started, enjoy!
Connect TiDB: mysql --comments --host 172.10.10.122 --port 4001 -u rootConnect TiDB: mysql --comments --host 172.10.10.122 --port 4000 -u rootTiDB Dashboard: http://172.10.10.122:2379/dashboardGrafana: http://172.10.10.122:3000
复制代码


三、TiDB 数据库的增删改查

1、创建数据库:

使用 MySQL 客户端连接到 TiDB 数据库。执行以下 SQL 语句创建一个新的数据库:


mysql> CREATE DATABASE your_tidb;Query OK, 0 rows affected (0.53 sec)
mysql>
复制代码



mysql> SHOW DATABASES;+--------------------+| Database           |+--------------------+| INFORMATION_SCHEMA || METRICS_SCHEMA     || PERFORMANCE_SCHEMA || mysql              || test               || your_tidb          |+--------------------+6 rows in set (0.01 sec)
mysql>
复制代码


2、创建表格:

在新创建的数据库中创建一个表格,用于存储数据。可以使用以下 SQL 语句创建表格:


mysql> use your_tidb;Database changedmysql> CREATE TABLE your_tidb (    ->     id INT(11),    ->     name VARCHAR(255),    ->     birthday DATE    ->     );Query OK, 0 rows affected (0.53 sec)
mysql>
复制代码


3、插入数据:

将数据插入到表格中,可以使用以下 SQL 语句:


mysql> INSERT INTO your_tidb VALUES(1,'tom','20240124');Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO your_tidb(id,name) VALUES('2','bob');Query OK, 1 row affected (0.01 sec)
mysql>
复制代码


4、查询数据:

从表格中查询数据,可以使用以下 SQL 语句:


mysql> SELECT * FROM your_tidb;+------+------+------------+| id   | name | birthday   |+------+------+------------+|    1 | tom  | 2024-01-24 ||    2 | bob  | NULL       |+------+------+------------+2 rows in set (0.01 sec)
mysql> SELECT * FROM your_tidb WHERE id<5;+------+------+------------+| id | name | birthday |+------+------+------------+| 1 | tom | 2024-01-24 || 2 | bob | NULL |+------+------+------------+2 rows in set (0.00 sec)
mysql>
复制代码


5、更新数据:

更新表格中的数据,可以使用以下 SQL 语句:


mysql> UPDATE your_tidb SET birthday='20240123' WHERE id=2;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0
mysql> SELECT * FROM your_tidb;+------+------+------------+| id | name | birthday |+------+------+------------+| 1 | tom | 2024-01-24 || 2 | bob | 2024-01-23 |+------+------+------------+2 rows in set (0.01 sec)
mysql>
复制代码



6、删除数据:


从表格中删除数据,可以使用以下 SQL 语句:


mysql> DELETE FROM your_tidb WHERE id=2;Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM your_tidb;+------+------+------------+| id | name | birthday |+------+------+------------+| 1 | tom | 2024-01-24 |+------+------+------------+1 row in set (0.00 sec)
mysql>
复制代码


7、管理数据库:

进行数据库的备份和恢复。可以使用 TiDB 自带的工具进行备份和恢复,也可以使用第三方工具进行备份和恢复。进行数据库的性能监控和调优。可以使用 TiDB 自带的监控工具进行性能监控,并根据监控结果进行性能调优。



需要注意的是,TiDB 的部署和配置可能会根据你的具体需求和环境有所不同。建议在部署前仔细阅读 TiDB 的官方文档,并根据实际情况进行相应的调整和配置。

四、心得体会

经常在墨天轮学习,看到很多大佬也在用 TiDB, 有空测试了一下。在初体验 TiDB 快速部署数据库的过程中,我学到了很多关于数据库的基本知识和实践经验,非常适合新手小白学习。以下是我的一些心得体会:


1、快速部署的便利:TiDB 的快速部署特性让我印象深刻。通过简单的命令行或图形界面工具,我可以快速地完成 TiDB 的安装和配置。这大大缩短了从环境准备到数据库运行的时间,提高了工作效率。2、与 MySQL 的兼容性:作为一个熟悉 MySQL 的开发人员,我很高兴看到 TiDB 与 MySQL 之间的高度兼容性。这意味着我可以继续使用熟悉的 SQL 语句、客户端工具和大部分已有的应用程序代码。这降低了学习曲线,提高了开发效率。3、监控与维护的重要性:虽然 TiDB 的部署相对简单,但后期的监控和维护同样重要。通过 TiDB 自带的监控工具,我可以实时了解数据库的运行状态、性能瓶颈和潜在问题。这有助于我及时调整配置、优化性能,确保数据库的稳定运行。4、社区支持与文档:数据库也学习了很多种,各个数据库的文档也看过很多。在部署和使用 TiDB 的过程中,我深感其强大的社区支持和丰富的文档资源。遇到问题时,我可以在社区论坛上寻求帮助,或者查阅详细的官方文档。这为我解决问题提供了极大的便利。


总的来说,通过这次初体验,我对 TiDB 有了更深入的了解,并对其在分布式数据库领域的应用前景充满期待。在未来,我计划进一步探索 TiDB 的高级特性,并将其应用于我的项目开发中。


用户头像

TiDB 社区官网:https://tidb.net/ 2021-12-15 加入

TiDB 社区干货传送门是由 TiDB 社区中布道师组委会自发组织的 TiDB 社区优质内容对外宣布的栏目,旨在加深 TiDBer 之间的交流和学习。一起构建有爱、互助、共创共建的 TiDB 社区 https://tidb.net/

评论

发布
暂无评论
TiDB 快速入门:从零到一 部署初探_安装 & 部署_TiDB 社区干货传送门_InfoQ写作社区