TiDB安装与部署 #
一、部署方式概览 #
1.1 部署方式对比 #
| 方式 | 适用场景 | 复杂度 | 推荐指数 |
|---|---|---|---|
| TiUP本地部署 | 开发测试 | ★☆☆☆☆ | ★★★★★ |
| TiUP生产部署 | 生产环境 | ★★★☆☆ | ★★★★★ |
| Docker Compose | 开发测试 | ★★☆☆☆ | ★★★★☆ |
| TiDB Operator | Kubernetes | ★★★★☆ | ★★★★☆ |
| TiDB Cloud | 云服务 | ★☆☆☆☆ | ★★★★★ |
1.2 环境要求 #
硬件要求
| 组件 | 最低配置 | 推荐配置 |
|---|---|---|
| TiDB | 2核4GB | 4核8GB |
| TiKV | 4核8GB | 8核32GB |
| PD | 2核4GB | 4核8GB |
| TiFlash | 8核32GB | 16核64GB |
操作系统要求
| 系统 | 版本要求 |
|---|---|
| CentOS | 7.3+ |
| Ubuntu | 18.04+ |
| macOS | 10.14+ |
| Debian | 9.0+ |
二、TiUP部署(推荐) #
2.1 TiUP简介 #
TiUP 是 TiDB 官方提供的包管理工具,可以方便地部署、管理 TiDB 集群。
text
TiUP 功能
├── 组件安装
├── 集群部署
├── 集群管理
├── 版本升级
└── 集群销毁
2.2 安装TiUP #
Linux/macOS 安装
bash
# 下载并安装 TiUP
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
# 设置环境变量
source ~/.bash_profile # 或 ~/.zshrc
# 验证安装
tiup --version
安装TiUP集群组件
bash
# 安装 cluster 组件
tiup cluster
# 查看已安装组件
tiup list
# 更新 TiUP
tiup update --self
# 更新 cluster 组件
tiup update cluster
2.3 本地单机部署 #
创建拓扑配置
yaml
# 创建配置文件 single.yaml
global:
user: "tidb"
ssh_port: 22
deploy_dir: "/tidb-deploy"
data_dir: "/tidb-data"
server_configs:
tidb:
log.slow-threshold: 300
tikv:
readpool.storage.use-unified-pool: false
readpool.coprocessor.use-unified-pool: true
pd_servers:
- host: 127.0.0.1
tidb_servers:
- host: 127.0.0.1
tikv_servers:
- host: 127.0.0.1
tiflash_servers:
- host: 127.0.0.1
monitoring_servers:
- host: 127.0.0.1
grafana_servers:
- host: 127.0.0.1
alertmanager_servers:
- host: 127.0.0.1
部署集群
bash
# 检查配置
tiup cluster check ./single.yaml --user tidb
# 部署集群
tiup cluster deploy tidb-single v7.5.0 ./single.yaml --user tidb
# 启动集群
tiup cluster start tidb-single
# 检查集群状态
tiup cluster display tidb-single
2.4 生产环境部署 #
拓扑规划
text
生产环境推荐架构 (最小高可用)
┌─────────────────────────────────────────────────────────────┐
│ 负载均衡层 │
│ HAProxy / LVS │
└─────────────────────────────────────────────────────────────┘
│
┌─────────────────────┼─────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ TiDB Server │ │ TiDB Server │ │ TiDB Server │
│ 192.168.1.1 │ │ 192.168.1.2 │ │ 192.168.1.3 │
└───────────────┘ └───────────────┘ └───────────────┘
│ │ │
└─────────────────────┼─────────────────────┘
│
┌─────────────────────┼─────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ PD │ │ PD │ │ PD │
│ 192.168.1.4 │ │ 192.168.1.5 │ │ 192.168.1.6 │
└───────────────┘ └───────────────┘ └───────────────┘
│ │ │
└─────────────────────┼─────────────────────┘
│
┌─────────┬─────────┬───────┼───────┬─────────┬─────────┐
▼ ▼ ▼ ▼ ▼ ▼ ▼
┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
│TiKV │ │TiKV │ │TiKV │ │TiKV │ │TiKV │ │TiKV │ │TiKV │
│ .7 │ │ .8 │ │ .9 │ │.10 │ │.11 │ │.12 │ │.13 │
└─────┘ └─────┘ └─────┘ └─────┘ └─────┘ └─────┘ └─────┘
生产配置文件
yaml
# production.yaml
global:
user: "tidb"
ssh_port: 22
deploy_dir: "/tidb-deploy"
data_dir: "/tidb-data"
arch: "amd64"
enable_tls: true
monitored:
node_exporter_port: 9100
blackbox_exporter_port: 9115
server_configs:
tidb:
log.slow-threshold: 300
binlog.enable: false
performance.max-procs: 0
prepared-plan-cache.enabled: true
tikv:
readpool.storage.use-unified-pool: false
readpool.coprocessor.use-unified-pool: true
storage.scheduler-worker-pool-size: 8
raftstore.sync-log: true
pd:
schedule.leader-schedule-limit: 4
schedule.region-schedule-limit: 2048
schedule.replica-schedule-limit: 64
pd_servers:
- host: 192.168.1.4
- host: 192.168.1.5
- host: 192.168.1.6
tidb_servers:
- host: 192.168.1.1
- host: 192.168.1.2
- host: 192.168.1.3
tikv_servers:
- host: 192.168.1.7
- host: 192.168.1.8
- host: 192.168.1.9
- host: 192.168.1.10
- host: 192.168.1.11
- host: 192.168.1.12
tiflash_servers:
- host: 192.168.1.13
- host: 192.168.1.14
monitoring_servers:
- host: 192.168.1.15
grafana_servers:
- host: 192.168.1.15
alertmanager_servers:
- host: 192.168.1.15
部署步骤
bash
# 1. 检查集群环境
tiup cluster check ./production.yaml --user tidb
# 2. 自动修复环境问题
tiup cluster check ./production.yaml --user tidb --apply
# 3. 部署集群
tiup cluster deploy tidb-prod v7.5.0 ./production.yaml --user tidb
# 4. 启动集群
tiup cluster start tidb-prod
# 5. 验证集群
tiup cluster display tidb-prod
2.5 集群管理命令 #
bash
# 查看集群列表
tiup cluster list
# 查看集群状态
tiup cluster display tidb-prod
# 停止集群
tiup cluster stop tidb-prod
# 启动集群
tiup cluster start tidb-prod
# 重启集群
tiup cluster restart tidb-prod
# 缩容节点
tiup cluster scale-in tidb-prod --node 192.168.1.1:4000
# 扩容节点
tiup cluster scale-out tidb-prod ./scale-out.yaml
# 升级集群
tiup cluster upgrade tidb-prod v7.5.1
# 销毁集群
tiup cluster destroy tidb-prod
# 清理数据
tiup cluster prune tidb-prod
三、Docker部署 #
3.1 Docker Compose部署 #
docker-compose.yml
yaml
version: '3'
services:
pd0:
image: pingcap/pd:v7.5.0
ports:
- "2379:2379"
- "2380:2380"
command:
- --name=pd0
- --client-urls=http://0.0.0.0:2379
- --peer-urls=http://0.0.0.0:2380
- --advertise-client-urls=http://pd0:2379
- --advertise-peer-urls=http://pd0:2380
- --initial-cluster=pd0=http://pd0:2380
tikv0:
image: pingcap/tikv:v7.5.0
command:
- --pd-endpoints=http://pd0:2379
- --addr=0.0.0.0:20160
- --advertise-addr=tikv0:20160
- --data-dir=/tikv/data
depends_on:
- pd0
tidb:
image: pingcap/tidb:v7.5.0
ports:
- "4000:4000"
- "10080:10080"
command:
- --store=tikv
- --path=pd0:2379
- --advertise-address=tidb
depends_on:
- tikv0
tiflash:
image: pingcap/tiflash:v7.5.0
command:
- --pd-endpoints=http://pd0:2379
- --addr=0.0.0.0:3930
- --advertise-addr=tiflash:3930
depends_on:
- tikv0
启动服务
bash
# 启动集群
docker-compose up -d
# 查看状态
docker-compose ps
# 连接TiDB
mysql -h 127.0.0.1 -P 4000 -u root
# 停止集群
docker-compose down
3.2 单容器快速体验 #
bash
# 快速启动单节点TiDB
docker run -d --name tidb-server \
-p 4000:4000 \
-p 10080:10080 \
pingcap/tidb:v7.5.0
# 连接
mysql -h 127.0.0.1 -P 4000 -u root
四、Kubernetes部署 #
4.1 TiDB Operator简介 #
TiDB Operator 是 TiDB 在 Kubernetes 上的自动化运维系统,提供完整的生命周期管理。
text
TiDB Operator 架构
┌─────────────────────────────────────────────────────────────┐
│ Kubernetes Cluster │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ TiDB Operator │ │
│ │ ┌─────────────┐ ┌─────────────────────────┐ │ │
│ │ │ tidb- │ │ tidb- │ │ │
│ │ │ controller │ │ scheduler │ │ │
│ │ └─────────────┘ └─────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ TiDB Cluster │ │
│ │ │ │
│ │ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │ │
│ │ │TiDB │ │ PD │ │TiKV │ │TiKV │ │TiKV │ │ │
│ │ └─────┘ └─────┘ └─────┘ └─────┘ └─────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
4.2 安装TiDB Operator #
bash
# 安装 Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# 添加 PingCAP 仓库
helm repo add pingcap https://charts.pingcap.org/
# 安装 TiDB Operator
kubectl create namespace tidb-admin
helm install tidb-operator pingcap/tidb-operator \
--namespace tidb-admin \
--version v1.5.0 \
--set operatorImage=pingcap/tidb-operator:v1.5.0 \
--set tidbDiscoveryImage=pingcap/tidb-operator:v1.5.0 \
--set scheduler.kubeSchedulerImageName=k8s.gcr.io/kube-scheduler
4.3 部署TiDB集群 #
创建TiDB集群配置
yaml
# tidb-cluster.yaml
apiVersion: pingcap.com/v1alpha1
kind: TidbCluster
metadata:
name: basic
namespace: tidb-cluster
spec:
version: v7.5.0
timezone: UTC
pvReclaimPolicy: Delete
enableDynamicConfiguration: true
configUpdateStrategy: RollingUpdate
pd:
baseImage: pingcap/pd
replicas: 3
requests:
storage: "10Gi"
config: |
[log]
level = "info"
tikv:
baseImage: pingcap/tikv
replicas: 3
requests:
storage: "100Gi"
config: |
[log]
level = "info"
[raftstore]
sync-log = true
tidb:
baseImage: pingcap/tidb
replicas: 2
service:
type: NodePort
config: |
[log]
level = "info"
tiflash:
baseImage: pingcap/tiflash
replicas: 1
requests:
storage: "100Gi"
部署集群
bash
# 创建命名空间
kubectl create namespace tidb-cluster
# 部署 TiDB 集群
kubectl apply -f tidb-cluster.yaml -n tidb-cluster
# 查看集群状态
kubectl get pods -n tidb-cluster
# 查看集群详情
kubectl describe tidbcluster basic -n tidb-cluster
4.4 连接TiDB集群 #
bash
# 端口转发
kubectl port-forward svc/basic-tidb 4000:4000 -n tidb-cluster
# 连接 TiDB
mysql -h 127.0.0.1 -P 4000 -u root
五、连接TiDB #
5.1 MySQL客户端连接 #
bash
# 基本连接
mysql -h <host> -P 4000 -u root -p
# 指定数据库
mysql -h <host> -P 4000 -u root -p -D test
# 使用SSL连接
mysql -h <host> -P 4000 -u root -p --ssl-ca=ca.pem --ssl-cert=client.pem --ssl-key=client-key.pem
5.2 应用程序连接 #
Java (JDBC)
java
// JDBC连接字符串
String url = "jdbc:mysql://localhost:4000/test?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);
Python
python
import pymysql
connection = pymysql.connect(
host='localhost',
port=4000,
user='root',
password='',
database='test'
)
Go
go
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main() {
dsn := "root:@tcp(localhost:4000)/test?charset=utf8mb4&parseTime=True&loc=Local"
db, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
defer db.Close()
}
Node.js
javascript
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
port: 4000,
user: 'root',
password: '',
database: 'test'
});
connection.connect();
5.3 ORM框架配置 #
Spring Boot
yaml
spring:
datasource:
url: jdbc:mysql://localhost:4000/test?useSSL=false&serverTimezone=UTC
username: root
password: ""
driver-class-name: com.mysql.cj.jdbc.Driver
Django
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': 'localhost',
'PORT': 4000,
'USER': 'root',
'PASSWORD': '',
'NAME': 'test',
}
}
GORM
go
import "gorm.io/driver/mysql"
dsn := "root:@tcp(localhost:4000)/test?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
六、验证安装 #
6.1 检查集群状态 #
sql
-- 连接 TiDB
mysql -h 127.0.0.1 -P 4000 -u root
-- 查看版本
SELECT tidb_version();
-- 查看存储信息
SELECT * FROM INFORMATION_SCHEMA.TIKV_STORE_STATUS;
-- 查看集群信息
SELECT * FROM INFORMATION_SCHEMA.CLUSTER_INFO;
6.2 创建测试表 #
sql
-- 创建数据库
CREATE DATABASE IF NOT EXISTS test_db;
USE test_db;
-- 创建测试表
CREATE TABLE users (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 插入测试数据
INSERT INTO users (name, email) VALUES
('Alice', 'alice@example.com'),
('Bob', 'bob@example.com');
-- 查询数据
SELECT * FROM users;
-- 查看表分布情况
SHOW TABLE REGIONS;
6.3 性能基准测试 #
bash
# 使用 sysbench 进行基准测试
sysbench oltp_read_write \
--mysql-host=127.0.0.1 \
--mysql-port=4000 \
--mysql-user=root \
--mysql-db=test \
--tables=10 \
--table-size=10000 \
--threads=10 \
prepare
sysbench oltp_read_write \
--mysql-host=127.0.0.1 \
--mysql-port=4000 \
--mysql-user=root \
--mysql-db=test \
--tables=10 \
--table-size=10000 \
--threads=10 \
run
七、常见问题 #
7.1 端口冲突 #
bash
# 检查端口占用
netstat -tlnp | grep 4000
# 修改 TiDB 端口
# 在配置文件中修改
tidb_servers:
- host: 127.0.0.1
port: 14000
status_port: 110080
7.2 内存不足 #
bash
# 检查内存使用
free -h
# 调整 TiKV 内存配置
tikv_servers:
- host: 127.0.0.1
config: |
[rocksdb]
max-open-files = 4096
[raftdb]
max-open-files = 4096
7.3 启动失败 #
bash
# 查看日志
tiup cluster display tidb-prod
# 查看具体组件日志
# 日志位置: {deploy_dir}/logs/
tail -f /tidb-deploy/tidb-4000/log/tidb.log
八、总结 #
部署方式选择建议:
| 场景 | 推荐方式 |
|---|---|
| 本地开发 | TiUP 单机部署 |
| 测试环境 | TiUP 多机部署 |
| 生产环境 | TiUP 生产部署 |
| 云原生环境 | TiDB Operator |
| 快速体验 | TiDB Cloud |
下一步,让我们深入了解 TiDB 的架构设计!
最后更新:2026-03-27