一、基本思路
本次 TiDB 的部署遵循官方推荐的 TiUP 工具离线部署方式,整体流程与关键点如下:
- 采用 TiUP 进行离线安装与部署按照 TiDB 官方最佳实践,使用 TiUP 工具在无公网环境下完成组件安装与集群部署,确保部署过程可控、可靠。
- 规划集群拓扑标签(Label)为实现合理的数据副本隔离与容灾能力,提前规划以下拓扑标签:
dc
(Data Center):数据中心/机房标识zone
:逻辑可用区,用于控制副本在不同可用区之间隔离rack
:机架,用于进一步细化副本物理隔离host
:主机标识
- 编写拓扑配置文件(topology.yaml)根据实际主机资源、端口规划、标签分布等,手动编写 TiDB 集群拓扑 YAML 文件,定义各组件的部署位置与参数。
- 部署与启动 TiDB 集群使用 TiUP 按拓扑文件完成集群部署、初始化与启动过程,实现 TiDB 各核心组件的落地运行。
- 以 tidb 用户执行部署操作所有部署和操作过程均使用专用系统用户
tidb
执行,以保证权限隔离和安全性。
二、环境规划
最小拓扑架构
IP |
实例 |
服务器配置 |
192.168.131.200 |
Monitoring & Grafana、PD |
4VCore 16 GiB 50 GiB存储 |
192.168.131.201 |
TiDB、PD |
4VCore 16 GiB 50 GiB存储 |
192.168.131.202 |
TiDB、PD |
4VCore 16 GiB 50 GiB存储 |
192.168.131.203 |
TiKV |
2VCore 8GiB 200 GiB (SSD) 用于存储 |
192.168.131.204 |
TiKV |
2VCore 8GiB 200 GiB (SSD) 用于存储 |
192.168.131.205 |
TiKV |
2VCore 8GiB 200 GiB (SSD) 用于存储 |
三、操作系统优化
- 修改内核参数
vi /etc/sysctl.conf
fs.file-max = 1000000
net.core.somaxconn = 32768
net.ipv4.tcp_syncookies = 0
vm.swappiness=0
参数生效
sysctl -p
- 优化tidb用户进程的资源限制
# vi /etc/security/limits.conf
* hard nofile 65536
* soft nofile 65536
tidb soft nofile 1000000
tidb hard nofile 1000000
tidb soft stack 10240
- 禁用Linux系统透明大页(Transparent Hugepage)
- 修改 GRUB 配置文件,打开 GRUB 的默认配置文件:
vi /etc/default/grub
- 找到这行开头是
GRUB_CMDLINE_LINUX=
的内容,并在其引号内添加如下内容(如果已经有内容,直接在最后加上):
transparent_hugepage=never
- 修改后类似于下面这样:
GRUB_CMDLINE_LINUX="resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet transparent_hugepage=never"
- 保存并关闭文件
- 重新生成 GRUB 配置并重启系统,执行以下命令来应用刚才的修改:
grub2-mkconfig -o /boot/grub2/grub.cfg
systemctl reboot
- 验证配置是否生效,系统重启后,运行下面的命令检查透明大页是否已关闭,如果输出中显示
[never]
,说明已经成功关闭透明大页功能。
cat /sys/kernel/mm/transparent_hugepage/enabled
- 安装依赖
dnf install numactl -y
- 同步阿里云ntp时钟源时间
# 安装并启用 chrony
dnf install chrony -y
systemctl enable --now chronyd
# 配置阿里云 NTP 服务器:
sed -i 's/^pool.*/server ntp.aliyun.com iburst/' /etc/chrony.conf
# 重启服务并同步时间:
sudo systemctl restart chronyd
sudo chronyc -a makestep
# 验证同步状态:
chronyc sources -v
chronyc tracking
# 时间差异过大,强制立即同步:
chronyc -a 'makestep 1 -1'
- 调整操作系统时区为北京时间(Asia/Shanghai)
# 查看当前时区
timedatectl
# 设置时区为北京时间
sudo timedatectl set-timezone Asia/Shanghai
# 验证时区修改
timedatectl | grep "Time zone"
输出应显示:
Time zone: Asia/Shanghai (CST, +0800)
四、安装环境配置
1. 关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
- 关闭SELinux
修改SELinux配置文件,将SELINUX=enforcing改为SELINUX=disabled。
# vi /etc/selinux/config
SELINUX=disabled
- 配置IP与主机名映射,所有节点都需要配置
# vi /etc/hosts
## TiDB Cluster
192.168.131.200 TiDB01
192.168.131.201 TiDB02
192.168.131.202 TiDB03
192.168.131.203 Tikv01
192.168.131.204 Tikv02
192.168.131.205 Tikv03
- 配置SSH免密码互信
#创建tidb用户
# groupadd tidb
# useradd -g tidb tidb
# echo "你的密码" | sudo passwd --stdin tidb
echo "Welcome1" | sudo passwd --stdin tidb
vi /etc/sudoers
在以下内容中增加tidb账号配置
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
tidb ALL=(ALL) NOPASSWD: ALL
#进行相互之间免密操作,在所有节点均执行
su - tidb
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub tidb@192.168.131.200
ssh-copy-id -i ~/.ssh/id_rsa.pub tidb@192.168.131.201
ssh-copy-id -i ~/.ssh/id_rsa.pub tidb@192.168.131.202
ssh-copy-id -i ~/.ssh/id_rsa.pub tidb@192.168.131.203
ssh-copy-id -i ~/.ssh/id_rsa.pub tidb@192.168.131.204
ssh-copy-id -i ~/.ssh/id_rsa.pub tidb@192.168.131.205
ssh TiDB02
ssh TiDB01
ssh TiDB03
ssh Tikv01
ssh Tikv02
ssh Tikv03
- 创建安装目录,所有节点均执行
# mkdir -p /data/tidb-deploy /data/tidb-data
[root@TiDB01 network-scripts]# chown -R tidb:tidb /data
五、部署离线环境 TiUP 组件
- 将离线包发送到中控机服务器后进行部署,执行以下命令安装 TiUP 组件:
# 解压tar包
$ tar -zxvf tidb-community-server-v7.5.6-linux-amd64.tar.gz
$ cd tidb-community-server-v7.5.6-linux-amd64
# 安装TiUP 工具
$ sh local_install.sh
$ source /home/tidb/.bash_profile
$ tiup --version
tiup playground
- 合并离线包
如果是通过官方下载页面下载的离线软件包,需要将 TiDB-community-server 软件包和 TiDB-community-toolkit 软件包合并到离线镜像中。如果是通过 tiup mirror clone 命令手动打包的离线组件包,不需要执行此步骤。
执行以下命令合并离线组件到 server 目录下。
tar zxvf tidb-community-toolkit-v7.5.6-linux-amd64.tar.gz
ls -ld tidb-community-server-v7.5.6-linux-amd64 tidb-community-toolkit-v7.5.6-linux-amd64.tar.gz
cd tidb-community-server-v7.5.6-linux-amd64
cp -rp keys ~/.tiup/
tiup mirror merge ../tidb-community-toolkit-v7.5.6-linux-amd64
六、部署TiDB集群
- 初始化集群拓扑文件
- 针对两种常用的部署场景,可以通过以下命令生成建议的拓扑模板:混合部署场景:单台机器部署多个实例
tiup cluster template --full > topology.yaml
- 跨机房部署场景:跨机房部署 TiDB 集群
tiup cluster template --multi-dc > topology.yaml
- 编辑dev.yml拓扑文件(按照本文档中前面的最小拓扑架构表格中的信息)
vi dev.yaml
# # 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: "/data/tidb-deploy"
data_dir: "/data/tidb-data"
listen_host: 0.0.0.0
arch: "amd64"
# # Monitored variables are applied to all the machines.
monitored:
node_exporter_port: 9100
blackbox_exporter_port: 9115
server_configs:
# tidb:
tikv:
server.grpc-compression-type: gzip
pd:
replication.location-labels: ["zone","dc","rack","host"]
replication.max-replicas: 3
label-property:
reject-leader:
- key: "dc"
value: "sha"
pd_servers:
- host: 192.168.131.200
- host: 192.168.131.201
- host: 192.168.131.202
tidb_servers:
- host: 192.168.131.200
- host: 192.168.131.201
- host: 192.168.131.202
tikv_servers:
- host: 192.168.131.203
config:
server.labels:
dc: sz
host: Tikv01
- host: 192.168.131.204
config:
server.labels:
dc: sz
host: Tikv02
- host: 192.168.131.205
config:
server.labels:
dc: sz
host: Tikv03
monitoring_servers:
- host: 192.168.131.200
grafana_servers:
- host: 192.168.131.200
alertmanager_servers:
- host: 192.168.131.200
- 执行部署命令
执行部署命令前,先使用 check 及 check --apply 命令检查和自动修复集群存在的潜在风险:
- 检查集群存在的潜在风险:
tiup cluster check ./dev.yaml --user tidb
2.自动修复集群存在的潜在风险:
tiup cluster check ./dev.yaml --apply --user tidb
- 部署TiDB 集群
$ tiup cluster deploy tidb-dev v7.5.6 ./dev.yaml --user tidb -p
- 查看TiUP 管理的集群情况
$ tiup cluster list
- 检查部署的TiDB 集群情况
预期输出包括tidb-dev 集群中实例ID、角色、主机、监听端口和状态(由于还未启动,所以状态为Down/inactive)、目录信息。
$ tiup cluster display tidb-dev
[tidb@TiDB01 tidb-community-server-v7.5.6-linux-amd64]$ tiup cluster list
Name User Version Path PrivateKey
---- ---- ------- ---- ----------
tidb-dev tidb v7.5.6 /home/tidb/.tiup/storage/cluster/clusters/tidb-dev /home/tidb/.tiup/storage/cluster/clusters/tidb-dev/ssh/id_rsa
[tidb@TiDB01 tidb-community-server-v7.5.6-linux-amd64]$ tiup cluster display tidb-dev
Cluster type: tidb
Cluster name: tidb-dev
Cluster version: v7.5.6
Deploy user: tidb
SSH type: builtin
Grafana URL: http://192.168.131.200:3000
ID Role Host Ports OS/Arch Status Data Dir Deploy Dir
-- ---- ---- ----- ------- ------ -------- ----------
192.168.131.200:9093 alertmanager 192.168.131.200 9093/9094 linux/x86_64 Down /data/tidb-data/alertmanager-9093 /data/tidb-deploy/alertmanager-9093
192.168.131.200:3000 grafana 192.168.131.200 3000 linux/x86_64 Down - /data/tidb-deploy/grafana-3000
192.168.131.200:2379 pd 192.168.131.200 2379/2380 linux/x86_64 Down /data/tidb-data/pd-2379 /data/tidb-deploy/pd-2379
192.168.131.201:2379 pd 192.168.131.201 2379/2380 linux/x86_64 Down /data/tidb-data/pd-2379 /data/tidb-deploy/pd-2379
192.168.131.202:2379 pd 192.168.131.202 2379/2380 linux/x86_64 Down /data/tidb-data/pd-2379 /data/tidb-deploy/pd-2379
192.168.131.200:9090 prometheus 192.168.131.200 9090/12020 linux/x86_64 Down /data/tidb-data/prometheus-9090 /data/tidb-deploy/prometheus-9090
192.168.131.200:4000 tidb 192.168.131.200 4000/10080 linux/x86_64 Down - /data/tidb-deploy/tidb-4000
192.168.131.201:4000 tidb 192.168.131.201 4000/10080 linux/x86_64 Down - /data/tidb-deploy/tidb-4000
192.168.131.202:4000 tidb 192.168.131.202 4000/10080 linux/x86_64 Down - /data/tidb-deploy/tidb-4000
192.168.131.203:20160 tikv 192.168.131.203 20160/20180 linux/x86_64 N/A /data/tidb-data/tikv-20160 /data/tidb-deploy/tikv-20160
192.168.131.204:20160 tikv 192.168.131.204 20160/20180 linux/x86_64 N/A /data/tidb-data/tikv-20160 /data/tidb-deploy/tikv-20160
192.168.131.205:20160 tikv 192.168.131.205 20160/20180 linux/x86_64 N/A /data/tidb-data/tikv-20160 /data/tidb-deploy/tikv-20160
Total nodes: 12
- 启动集群
安全启动是 TiUP cluster 从 v1.9.0 起引入的一种新的启动方式,采用该方式启动数据库可以提高数据库安全性。推荐使用安全启动。安全启动后,TiUP 会自动生成 TiDB root 用户的密码,并在命令行界面返回密码。
注意 使用安全启动方式后,不能通过无密码的 root 用户登录数据库,你需要记录命令行返回的密码进行后续操作。 该自动生成的密码只会返回一次,如果没有记录或者忘记该密码,请参照忘记 root 密码修改密码。
方式一:安全启动
tiup cluster start tidb-dev --init
方式二:普通启动
$ tiup cluster start tidb-dev
预期结果输出 Started cluster tidb-dev
successfully,表示启动成功。使用普通启动方式后,可通过无密码的 root 用户登录数据库。
[tidb@TiDB01 tidb-community-server-v7.5.6-linux-amd64]$ tiup cluster start tidb-dev
Starting cluster tidb-dev...
+ [ Serial ] - SSHKeySet: privateKey=/home/tidb/.tiup/storage/cluster/clusters/tidb-dev/ssh/id_rsa, publicKey=/home/tidb/.tiup/storage/cluster/clusters/tidb-dev/ssh/id_rsa.pub
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.204
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.205
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.200
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.201
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.202
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.200
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.200
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.200
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.200
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.202
+ [Parallel] - UserSSH: user=tidb, host=192.168.131.203
+ [ Serial ] - StartCluster
Starting component pd
Starting instance 192.168.131.202:2379
Starting instance 192.168.131.200:2379
Starting instance 192.168.131.201:2379
Start instance 192.168.131.200:2379 success
Start instance 192.168.131.201:2379 success
Start instance 192.168.131.202:2379 success
Starting component tikv
Starting instance 192.168.131.205:20160
Starting instance 192.168.131.203:20160
Starting instance 192.168.131.204:20160
Start instance 192.168.131.205:20160 success
Start instance 192.168.131.203:20160 success
Start instance 192.168.131.204:20160 success
Starting component tidb
Starting instance 192.168.131.202:4000
Starting instance 192.168.131.200:4000
Starting instance 192.168.131.201:4000
Start instance 192.168.131.202:4000 success
Start instance 192.168.131.201:4000 success
Start instance 192.168.131.200:4000 success
Starting component prometheus
Starting instance 192.168.131.200:9090
Start instance 192.168.131.200:9090 success
Starting component grafana
Starting instance 192.168.131.200:3000
Start instance 192.168.131.200:3000 success
Starting component alertmanager
Starting instance 192.168.131.200:9093
Start instance 192.168.131.200:9093 success
Starting component node_exporter
Starting instance 192.168.131.205
Starting instance 192.168.131.202
Starting instance 192.168.131.203
Starting instance 192.168.131.200
Starting instance 192.168.131.201
Starting instance 192.168.131.204
Start 192.168.131.200 success
Start 192.168.131.202 success
Start 192.168.131.201 success
Start 192.168.131.204 success
Start 192.168.131.203 success
Start 192.168.131.205 success
Starting component blackbox_exporter
Starting instance 192.168.131.205
Starting instance 192.168.131.202
Starting instance 192.168.131.200
Starting instance 192.168.131.203
Starting instance 192.168.131.201
Starting instance 192.168.131.204
Start 192.168.131.202 success
Start 192.168.131.200 success
Start 192.168.131.205 success
Start 192.168.131.204 success
Start 192.168.131.203 success
Start 192.168.131.201 success
+ [ Serial ] - UpdateTopology: cluster=tidb-dev
Started cluster `tidb-dev` successfully
验证集群运行状态
tiup cluster display tidb-dev
[tidb@TiDB01 tidb-community-server-v7.5.6-linux-amd64]$ tiup cluster display tidb-dev
Cluster type: tidb
Cluster name: tidb-dev
Cluster version: v7.5.6
Deploy user: tidb
SSH type: builtin
Dashboard URL: http://192.168.131.200:2379/dashboard
Grafana URL: http://192.168.131.200:3000
ID Role Host Ports OS/Arch Status Data Dir Deploy Dir
-- ---- ---- ----- ------- ------ -------- ----------
192.168.131.200:9093 alertmanager 192.168.131.200 9093/9094 linux/x86_64 Up /data/tidb-data/alertmanager-9093 /data/tidb-deploy/alertmanager-9093
192.168.131.200:3000 grafana 192.168.131.200 3000 linux/x86_64 Up - /data/tidb-deploy/grafana-3000
192.168.131.200:2379 pd 192.168.131.200 2379/2380 linux/x86_64 Up|L|UI /data/tidb-data/pd-2379 /data/tidb-deploy/pd-2379
192.168.131.201:2379 pd 192.168.131.201 2379/2380 linux/x86_64 Up /data/tidb-data/pd-2379 /data/tidb-deploy/pd-2379
192.168.131.202:2379 pd 192.168.131.202 2379/2380 linux/x86_64 Up /data/tidb-data/pd-2379 /data/tidb-deploy/pd-2379
192.168.131.200:9090 prometheus 192.168.131.200 9090/12020 linux/x86_64 Up /data/tidb-data/prometheus-9090 /data/tidb-deploy/prometheus-9090
192.168.131.200:4000 tidb 192.168.131.200 4000/10080 linux/x86_64 Up - /data/tidb-deploy/tidb-4000
192.168.131.201:4000 tidb 192.168.131.201 4000/10080 linux/x86_64 Up - /data/tidb-deploy/tidb-4000
192.168.131.202:4000 tidb 192.168.131.202 4000/10080 linux/x86_64 Up - /data/tidb-deploy/tidb-4000
192.168.131.203:20160 tikv 192.168.131.203 20160/20180 linux/x86_64 Up /data/tidb-data/tikv-20160 /data/tidb-deploy/tikv-20160
192.168.131.204:20160 tikv 192.168.131.204 20160/20180 linux/x86_64 Up /data/tidb-data/tikv-20160 /data/tidb-deploy/tikv-20160
192.168.131.205:20160 tikv 192.168.131.205 20160/20180 linux/x86_64 Up /data/tidb-data/tikv-20160 /data/tidb-deploy/tikv-20160
Total nodes: 12
关闭群集
tiup cluster stop tidb-dev
验证集群运行状态
$ tiup cluster display tidb-dev
(此时状态已由Down/inactive变为Up)
七、监控面板
- Dashboard
访问地址:http://192.168.131.200:2379/dashboard
初始账户:root
初始密码:空
- Grafana
访问地址:http://192.168.131.200:3000/
管理员账号:admin
初始密码:admin
首次登陆会强制修改密码:******
- Prometheus
访问地址:http://192.168.131.200:9090/
八、结束语
本次 TiDB 在开发与测试环境中的部署过程整体较为顺利。通过部署与初步验证,得出以下结论:
- 容灾能力强:TiDB 可支持两地三中心或同城多中心的容灾部署架构,满足高可用性要求。
- 监控体系完善:内置丰富的监控与指标体系,便于实时运维与故障排查。
- HTAP 能力突出:同时具备 OLTP 和实时 OLAP 处理能力,适应多种业务负载。
- 弹性伸缩优越:支持水平扩容与缩容,扩展灵活,适合云环境部署。
- 原生分布式架构:具备云原生特性,结合 Kubernetes 部署可实现自动容错与自愈能力。
- 避免分库分表复杂性:架构设计规避了传统分库分表的难题,简化开发,降低 DBA 运维复杂度。
- 国产化开源生态成熟:社区活跃,文档丰富且中文支持良好,适合本地化落地。
- 广泛应用验证:目前已有超过 1000 家企业在生产环境中应用 TiDB,覆盖金融(如光大银行、微众银行、平安科技)、互联网(如知乎、美团、58集团)等多个行业,验证了其高成熟度和稳定性。
- 适配场景广泛:尤其适用于高可用、强一致性要求高、数据规模大、业务复杂度高的核心系统场景。
- MySQL 兼容性好:协议和生态兼容 MySQL,迁移成本低,维护简单,学习曲线平缓。
综上所述,TiDB 具备良好的功能完整性、扩展性与生态适应性,是一款在当前多种关键业务场景下值得考虑的国产分布式数据库解决方案。