11.TiDB集群部署

本文介绍了最小规模 TiDB 集群拓扑的搭建步骤,包括下载安装 TiUP、安装其 cluster 组件、更新软件版本、调大 sshd 服务连接数限制、创建并启动集群、执行部署命令、启动集群,还说明了访问 TiDB 数据库、Grafana 监控和 Dashboard 的方法。

最小规模的 TiDB 集群拓扑:

实例

个数

IP

配置

TiKV

3

10.0.1.1

10.0.1.1

10.0.1.1

避免端口和目录冲突

TiDB

1

10.0.1.1

默认端口

全局目录配置

PD

1

10.0.1.1

默认端口

全局目录配置

TiFlash

1

10.0.1.1

默认端口

全局目录配置

Monitor

1

10.0.1.1

默认端口

全局目录配置

1.下载并安装 TiUP:

curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh

 

2. 安装 TiUP 的 cluster 组件:

tiup cluster

 

3. 如果机器已经安装 TiUP cluster,需要更新软件版本:

tiup update --self && tiup update cluster

 

4. 如果使用单机模拟多机部署,需要通过 root 用户调大 sshd 服务的连接数限制:

     i.修改 /etc/ssh/sshd_config 将 MaxSessions 调至 20。

     ii. 重启 sshd 服务:

             service sshd restart

5. 创建并启动集群

按下面的配置模板,编辑配置文件,命名为 topo.yaml,其中:

  • user: "tidb":表示通过 tidb 系统用户(部署会自动创建)来做集群的内部管理,默认使用 22 端口通过 ssh 登录目标机器

  • replication.enable-placement-rules:设置这个 PD 参数来确保 TiFlash 正常运行

  • host:设置为本部署主机的 IP

配置模板如下:   

# # Global variables are applied to all deployments and used as the default value of
# # the deployments if a specific deployment value is missing.
global:
 user: "tidb"
 ssh_port: 22
 deploy_dir: "/usr/local/yunji/tidb"
 data_dir: "/usr/local/yunji/tidb/data"

# # Monitored variables are applied to all the machines.
monitored:
 node_exporter_port: 9100
 blackbox_exporter_port: 9115

server_configs:
 tidb:
   log.slow-threshold: 300
 tikv:
   readpool.storage.use-unified-pool: false
   readpool.coprocessor.use-unified-pool: true
 pd:
   replication.enable-placement-rules: true
   replication.location-labels: ["host"]
 tiflash:
   logger.level: "info"

pd_servers:
 - host: 172.16.0.82

tidb_servers:
 - host: 172.16.0.82

tikv_servers:
 - host: 172.16.0.82
   port: 20160
   status_port: 20180
   config:
     server.labels: { host: "logic-host-1" }

 - host: 172.16.0.82
   port: 20161
   status_port: 20181
   config:
     server.labels: { host: "logic-host-2" }

 - host: 172.16.0.82
   port: 20162
   status_port: 20182
   config:
     server.labels: { host: "logic-host-3" }

tiflash_servers:
 - host: 172.16.0.82

monitoring_servers:
 - host: 172.16.0.82

grafana_servers:
 - host: 172.16.0.82

 

6.执行集群部署命令:

tiup cluster deploy <cluster-name> <tidb-version> ./topo.yaml --user root -p

 

  • 参数 <cluster-name> 表示设置集群名称

  • 参数 <tidb-version> 表示设置集群版本,可以通过 tiup list tidb 命令来查看当前支持部署的 TiDB 版本

按照引导,输入”y”及 root 密码,来完成部署:

 

7. 启动集群:

tiup cluster start <cluster-name>

 

8.访问集群

  • 访问 TiDB 数据库,密码为空:mysql -h {ip} -P 4000 -u root

  • 访问 TiDB 的 Grafana 监控: 通过 http://{grafana-ip}:3000 访问集群 Grafana 监控页面,默认用户名和密码均为 admin。

  • 访问 TiDB 的 Dashboard: 通过 http://{pd-ip}:2379/dashboard 访问集群 TiDB Dashboard 监控页面,默认用户名为 root,密码为空。

### 如何部署 TiDB 集群 #### 使用 TiUP 工具部署 TiDB 集群 TiUP 是一种现代化的工具集合管理器,用于简化 TiDB 的安装和运维过程。以下是基于 TiUP 部署 TiDB 集群的关键步骤: 1. **安装 TiUP 和 Cluster 组件** 安装 TiUP 并初始化环境变量后,可以通过以下命令安装 `tiup-cluster` 组件[^1]: ```bash curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh tiup install cluster ``` 2. **创建集群拓扑文件** 创建一个 JSON 格式的拓扑文件来定义节点角色及其对应的机器地址。例如: ```json { "pd": [ {"host": "192.168.0.1"}, {"host": "192.168.0.2"} ], "tikv": [ {"host": "192.168.0.3"}, {"host": "192.168.0.4"} ], "tidb": [ {"host": "192.168.0.5"}, {"host": "192.168.0.6"} ] } ``` 这里分别指定了 PD(Placement Driver)、TiKV 和 TiDB 节点的角色分布。 3. **初始化并部署集群** 初始化集群配置并通过指定拓扑文件完成部署操作: ```bash tiup cluster deploy tidb-test v6.5.0 /path/to/topology.yaml --user root -p tiup cluster start tidb-test ``` 上述命令会根据版本号 `v6.5.0` 和路径中的拓扑文件 `/path/to/topology.yaml` 来部署名为 `tidb-test` 的集群,并启动它。 4. **验证集群状态** 验证已部署TiDB 集群是否正常工作: ```bash tiup cluster display tidb-test tiup cluster health tidb-test mysql -h 192.168.0.5 -P 4000 -u root ``` --- #### 结合 Kubernetes (KubeSphere) 部署 TiDB 对于云原生场景下的分布式数据库需求,可以利用 KubeSphere 提供的功能快速部署 TiDB 集群[^2]。 1. **启用多集群联邦功能** 如果希望实现跨区域高可用,则可以在 KubeSphere 中开启多集群支持,并将 TiDB 的组件副本分发至不同的物理或逻辑集群上。 2. **使用 Helm Chart 或 Operator 自动化部署** 借助官方提供的 TiDB Operator,在 Kubernetes 环境下能够轻松实现声明式管理和动态扩展能力。具体流程如下: - 添加仓库源; - 编辑 values 文件调整参数设置; - 执行安装指令。 示例代码片段展示如何引入资源清单文件: ```yaml apiVersion: pingcap.com/v1alpha1 kind: TidbCluster metadata: name: basic-tidb-cluster spec: pd: replicas: 3 tikv: replicas: 3 tidb: replicas: 2 ``` 3. **监控与维护** 利用 Prometheus/Grafana 对整个系统的性能指标进行持续跟踪分析;同时借助 Backup & Restore 插件保障数据安全备份恢复机制的有效性。 --- #### 公有云/私有云上的自动化解决方案 为了进一步降低复杂度提升效率,PingCAP 推出了专门针对云端优化的产品——TiDB Cloud[^3] 。用户无需关心底层基础设施细节即可享受到完全托管的服务体验,只需简单几步就能构建起生产级别的 HTAP 数据平台实例。 ```python import pymysql connection = pymysql.connect( host='your-host', port=4000, user='root', password='', database='' ) cursor = connection.cursor() cursor.execute('SHOW DATABASES;') print(cursor.fetchall()) ``` 上述 Python 脚本演示了连接到远程 TiDB Server 后查询现有库列表的过程。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值