外观
TiDB 手动部署
TiDB 手动部署是指不使用 TiUP 等自动化工具,手动安装和配置 TiDB 集群的各个组件。手动部署适用于对 TiDB 集群组件有深入了解、需要精确控制每个组件配置、在特殊环境中部署、学习测试部署过程或自动化部署工具无法满足需求的场景。
手动部署准备
1. 硬件和软件要求
硬件要求
| 组件 | CPU | 内存 | 存储 | 网络 | 数量 |
|---|---|---|---|---|---|
| TiDB | 8 核+ | 16 GB+ | SSD/HDD | 千兆网卡 | 2+ |
| PD | 4 核+ | 8 GB+ | SSD | 千兆网卡 | 3+ |
| TiKV | 8 核+ | 16 GB+ | SSD | 千兆网卡 | 3+ |
| TiFlash | 16 核+ | 32 GB+ | SSD | 千兆网卡 | 1+ |
| TiCDC | 8 核+ | 16 GB+ | SSD | 千兆网卡 | 1+ |
软件要求
| 组件 | 版本 |
|---|---|
| Linux | CentOS 7.3+ / Ubuntu 16.04+ |
| Go | 1.16+(仅用于编译) |
| Rust | 1.54+(仅用于编译) |
| MySQL 客户端 | 5.7+ |
| NTP 服务 | 已安装并同步 |
2. 环境准备
关闭防火墙
bash
# CentOS 7
systemctl stop firewalld
systemctl disable firewalld
# Ubuntu
systemctl stop ufw
systemctl disable ufw关闭 SELinux
bash
# 临时关闭
setenforce 0
# 永久关闭
vi /etc/selinux/config
# 将 SELINUX=enforcing 改为 SELINUX=disabled配置 NTP 服务
bash
# 安装 NTP 服务
# CentOS 7
yum install -y ntp
# Ubuntu
apt-get install -y ntp
# 启动 NTP 服务
systemctl start ntpd
systemctl enable ntpd
# 验证时间同步
timedatectl status
ntpq -p配置文件系统
bash
# 格式化磁盘(以 /dev/sdb 为例)
mkfs.xfs /dev/sdb
# 创建挂载点
mkdir -p /tidb-data
# 挂载磁盘
echo "/dev/sdb /tidb-data xfs defaults 0 0" >> /etc/fstab
mount -a3. 下载 TiDB 组件
从 TiDB 官方下载页面下载所需版本的 TiDB 组件:
bash
# 创建下载目录
mkdir -p /tidb-download
cd /tidb-download
# 下载 TiDB 组件
wget https://download.pingcap.org/tidb-<version>-linux-amd64.tar.gz
wget https://download.pingcap.org/tikv-<version>-linux-amd64.tar.gz
wget https://download.pingcap.org/pd-<version>-linux-amd64.tar.gz
wget https://download.pingcap.org/tiflash-<version>-linux-amd64.tar.gz
wget https://download.pingcap.org/ticdc-<version>-linux-amd64.tar.gz
# 解压组件
tar -xzf tidb-<version>-linux-amd64.tar.gz
tar -xzf tikv-<version>-linux-amd64.tar.gz
tar -xzf pd-<version>-linux-amd64.tar.gz
tar -xzf tiflash-<version>-linux-amd64.tar.gz
tar -xzf ticdc-<version>-linux-amd64.tar.gz手动部署步骤
1. 部署 PD 集群
1.1 配置 PD 组件
在每个 PD 节点上创建 PD 配置文件:
toml
# pd.toml
# 基本配置
name = "pd-1" # PD 节点名称,每个节点必须唯一
data-dir = "/tidb-data/pd-2379" # PD 数据目录
client-urls = "http://<pd-ip>:2379" # PD 客户端访问地址
peer-urls = "http://<pd-ip>:2380" # PD 节点间通信地址
advertise-client-urls = "http://<pd-ip>:2379" # 对外暴露的客户端访问地址
advertise-peer-urls = "http://<pd-ip>:2380" # 对外暴露的节点间通信地址
# 集群配置
initial-cluster = "pd-1=http://<pd1-ip>:2380,pd-2=http://<pd2-ip>:2380,pd-3=http://<pd3-ip>:2380" # 初始集群节点列表
initial-cluster-state = "new" # 初始集群状态,new 表示新建集群
# 日志配置
log-file = "/tidb-data/pd-2379/pd.log" # 日志文件路径
log-level = "info" # 日志级别1.2 启动 PD 集群
在每个 PD 节点上启动 PD 服务:
bash
# 启动 PD 服务
nohup /tidb-download/pd-<version>-linux-amd64/bin/pd-server --config=/path/to/pd.toml > /dev/null 2>&1 &
# 验证 PD 服务
curl http://<pd-ip>:2379/pd/api/v1/members2. 部署 TiKV 集群
2.1 配置 TiKV 组件
在每个 TiKV 节点上创建 TiKV 配置文件:
toml
# tikv.toml
# 基本配置
addr = "0.0.0.0:20160" # TiKV 节点监听地址
advertise-addr = "<tikv-ip>:20160" # 对外暴露的 TiKV 节点地址
data-dir = "/tidb-data/tikv-20160" # TiKV 数据目录
# PD 配置
pd-endpoints = ["http://<pd1-ip>:2379", "http://<pd2-ip>:2379", "http://<pd3-ip>:2379"] # PD 节点列表
# 日志配置
log-file = "/tidb-data/tikv-20160/tikv.log" # 日志文件路径
log-level = "info" # 日志级别
# 存储配置
[storage]
enable-encryption = false # 是否启用数据加密
engine = "rocksdb" # 存储引擎
# Raft 配置
[raftstore]
raft-base-tick-interval = "1s" # Raft 基础 tick 间隔
raft-election-timeout-ticks = 10 # Raft 选举超时时间
raft-heartbeat-ticks = 2 # Raft 心跳间隔2.2 启动 TiKV 集群
在每个 TiKV 节点上启动 TiKV 服务:
bash
# 启动 TiKV 服务
nohup /tidb-download/tikv-<version>-linux-amd64/bin/tikv-server --config=/path/to/tikv.toml > /dev/null 2>&1 &
# 验证 TiKV 服务
curl http://<pd-ip>:2379/pd/api/v1/stores3. 部署 TiDB 集群
3.1 配置 TiDB 组件
在每个 TiDB 节点上创建 TiDB 配置文件:
toml
# tidb.toml
# 基本配置
host = "0.0.0.0" # TiDB 节点监听地址
port = 4000 # TiDB 服务器的 SQL 端口
status-port = 10080 # TiDB 服务器的状态端口
store = "tikv" # 存储引擎类型
path = "127.0.0.1:2379" # PD 节点地址
# 日志配置
log-file = "/tidb-data/tidb-4000/tidb.log" # 日志文件路径
log-level = "info" # 日志级别
# 性能配置
[performance]
max-procs = 0 # TiDB 服务器使用的最大 CPU 核心数
mem-quota-query = 1073741824 # 单个查询允许使用的最大内存
# 事务配置
[txn]
mode = "pessimistic" # 事务模式3.2 启动 TiDB 集群
在每个 TiDB 节点上启动 TiDB 服务:
bash
# 启动 TiDB 服务
nohup /tidb-download/tidb-<version>-linux-amd64/bin/tidb-server --config=/path/to/tidb.toml > /dev/null 2>&1 &
# 验证 TiDB 服务
mysql -h <tidb-ip> -P 4000 -u root -e "SELECT version();"4. 部署 TiFlash(可选)
4.1 配置 TiFlash 组件
在每个 TiFlash 节点上创建 TiFlash 配置文件:
toml
# tiflash.toml
# 基本配置
[server]
http_port = 8123 # HTTP 服务端口
tcp_port = 9000 # TCP 服务端口
flash_service_port = 3930 # Flash 服务端口
grpc_port = 20170 # gRPC 服务端口
# PD 配置
[pd_client]
pd_addr = ["http://<pd1-ip>:2379", "http://<pd2-ip>:2379", "http://<pd3-ip>:2379"] # PD 节点列表
# 存储配置
[storage]
data_path = "/tidb-data/tiflash" # 数据存储目录
# 日志配置
[logger]
log = "/tidb-data/tiflash/logs/tiflash.log" # 日志文件路径
level = "info" # 日志级别4.2 启动 TiFlash 集群
在每个 TiFlash 节点上启动 TiFlash 服务:
bash
# 启动 TiFlash 服务
nohup /tidb-download/tiflash-<version>-linux-amd64/bin/tiflash server --config-file=/path/to/tiflash.toml > /dev/null 2>&1 &
# 验证 TiFlash 服务
mysql -h <tidb-ip> -P 4000 -u root -e "SELECT * FROM information_schema.tiflash_replica;"5. 部署 TiCDC(可选)
5.1 配置 TiCDC 组件
在每个 TiCDC 节点上创建 TiCDC 配置文件:
toml
# ticdc.toml
# 基本配置
addr = "0.0.0.0:8300" # TiCDC 服务器监听地址
advertise-addr = "<ticdc-ip>:8300" # 对外暴露的 TiCDC 服务器地址
pd-endpoints = ["http://<pd1-ip>:2379", "http://<pd2-ip>:2379", "http://<pd3-ip>:2379"] # PD 节点列表
# 日志配置
log-file = "/tidb-data/ticdc-8300/ticdc.log" # 日志文件路径
log-level = "info" # 日志级别
# GC 配置
gc-ttl = 86400 # GC TTL,单位:秒5.2 启动 TiCDC 集群
在每个 TiCDC 节点上启动 TiCDC 服务:
bash
# 启动 TiCDC 服务
nohup /tidb-download/ticdc-<version>-linux-amd64/bin/cdc server --config=/path/to/ticdc.toml > /dev/null 2>&1 &
# 验证 TiCDC 服务
curl http://<ticdc-ip>:8300/health集群验证
1. 验证 PD 集群
bash
# 查看 PD 集群成员
curl http://<pd-ip>:2379/pd/api/v1/members
# 查看 PD 集群状态
curl http://<pd-ip>:2379/pd/api/v1/status2. 验证 TiKV 集群
bash
# 查看 TiKV 节点状态
curl http://<pd-ip>:2379/pd/api/v1/stores
# 查看 Region 分布
curl http://<pd-ip>:2379/pd/api/v1/regions/meta3. 验证 TiDB 集群
bash
# 连接 TiDB 数据库
mysql -h <tidb-ip> -P 4000 -u root
# 查看 TiDB 版本
SELECT version();
# 创建测试表
CREATE DATABASE test;
USE test;
CREATE TABLE t1 (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO t1 VALUES (1, 'test');
SELECT * FROM t1;4. 验证 TiFlash(如果已部署)
bash
# 连接 TiDB 数据库
mysql -h <tidb-ip> -P 4000 -u root
# 创建 TiFlash 副本
ALTER TABLE test.t1 SET TIFLASH REPLICA 1;
# 查看 TiFlash 副本状态
SELECT * FROM information_schema.tiflash_replica;
# 使用 TiFlash 执行查询
SELECT * FROM test.t1 FORCE INDEX();手动部署最佳实践
1. 使用 systemd 管理服务
为每个组件创建 systemd 服务文件,便于管理和监控:
bash
# PD systemd 服务文件
cat > /etc/systemd/system/pd.service << EOF
[Unit]
Description=PD Service
After=network.target
[Service]
User=tidb
Group=tidb
ExecStart=/tidb-download/pd-<version>-linux-amd64/bin/pd-server --config=/path/to/pd.toml
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# 启动 PD 服务
systemctl daemon-reload
systemctl start pd
systemctl enable pd
# 其他组件类似,创建对应的 systemd 服务文件2. 配置监控和告警
部署 Prometheus 和 Grafana,监控 TiDB 集群的状态和性能:
bash
# 部署 Prometheus
# 下载 Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v<version>/prometheus-<version>.linux-amd64.tar.gz
tar -xzf prometheus-<version>.linux-amd64.tar.gz
# 配置 Prometheus
cat > prometheus.yml << EOF
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'tidb'
static_configs:
- targets: ['<tidb1-ip>:10080', '<tidb2-ip>:10080']
- job_name: 'pd'
static_configs:
- targets: ['<pd1-ip>:2379', '<pd2-ip>:2379', '<pd3-ip>:2379']
- job_name: 'tikv'
static_configs:
- targets: ['<tikv1-ip>:20180', '<tikv2-ip>:20180', '<tikv3-ip>:20180']
EOF
# 启动 Prometheus
nohup ./prometheus --config.file=prometheus.yml > /dev/null 2>&1 &
# 部署 Grafana
# 下载 Grafana
wget https://dl.grafana.com/oss/release/grafana-<version>.linux-amd64.tar.gz
tar -xzf grafana-<version>.linux-amd64.tar.gz
# 启动 Grafana
nohup ./grafana-<version>/bin/grafana-server > /dev/null 2>&1 &3. 配置日志管理
配置日志滚动和管理,避免日志文件过大:
toml
# PD 日志配置
[log]
file = "/tidb-data/pd-2379/pd.log"
level = "info"
max-size = 100 # 单个日志文件最大大小,单位:MB
max-days = 7 # 日志文件保留天数
max-backups = 10 # 保留的日志文件数量
# 其他组件类似,配置对应的日志滚动参数4. 定期备份数据
配置定期备份策略,确保数据安全:
bash
# 使用 BR 工具进行全量备份
/path/to/br backup full --pd <pd-ip>:2379 --storage "local:///path/to/backup"
# 使用 CRON 定期执行备份
0 0 * * * /path/to/br backup full --pd <pd-ip>:2379 --storage "local:///path/to/backup-$(date +\%Y\%m\%d)"常见问题(FAQ)
Q1: 手动部署 TiDB 集群时,PD 节点无法启动怎么办?
A1: 可以从以下几个方面排查:
- 检查 PD 配置文件是否正确,特别是
initial-cluster参数 - 检查 PD 数据目录是否有写权限
- 检查 PD 节点间的网络连接是否正常
- 查看 PD 日志,分析具体错误信息
Q2: TiKV 节点无法连接到 PD 集群怎么办?
A2: 可以从以下几个方面排查:
- 检查 TiKV 配置文件中的
pd-endpoints参数是否正确 - 检查 TiKV 节点与 PD 节点间的网络连接是否正常
- 检查 PD 集群是否正常运行
- 查看 TiKV 日志,分析具体错误信息
Q3: 如何添加新的 TiKV 节点到现有集群?
A3: 可以按照以下步骤添加新的 TiKV 节点:
- 配置新的 TiKV 节点
- 启动新的 TiKV 节点
- 新的 TiKV 节点会自动注册到 PD 集群
- PD 会自动调度 Region 到新的 TiKV 节点
Q4: 如何升级手动部署的 TiDB 集群?
A4: 可以按照以下步骤升级手动部署的 TiDB 集群:
- 备份数据
- 逐个升级 PD 节点
- 逐个升级 TiKV 节点
- 逐个升级 TiDB 节点
- 升级其他组件(如 TiFlash、TiCDC 等)
- 验证集群状态
Q5: 手动部署和 TiUP 部署有什么区别?
A5: 主要区别如下:
- 手动部署需要手动配置和管理每个组件,灵活性高,但工作量大
- TiUP 部署是自动化部署,工作量小,但灵活性相对较低
- 手动部署适合对 TiDB 组件有深入了解的用户
- TiUP 部署适合大多数用户,特别是生产环境
Q6: 如何监控手动部署的 TiDB 集群?
A6: 可以使用以下方式监控手动部署的 TiDB 集群:
- 部署 Prometheus 和 Grafana,配置对应组件的监控
- 使用 TiDB Dashboard 监控集群状态
- 定期查看组件日志
- 设置告警规则,及时发现和处理问题
