TiDB部署

简介

TiDB开源分布式关系型数据库,适合高可用、强一致要求较高、数据规模较大等各种应用场景,兼容 MySQL 协议和 MySQL 生态
官网:https://cn.pingcap.com/
文档:https://docs.pingcap.com/zh/tidb/stable

在单机上模拟部署生产环境集群

希望用单台 Linux 服务器,体验 TiDB 最小的完整拓扑的集群,并模拟生产环境下的部署步骤。

环境

系统:CentOS 7
TiDB:v7.5.3

集群拓扑

实例 个数 IP 配置
TiKV 3 10.88.88.70
10.88.88.70
10.88.88.70
避免端口和目录冲突
TiDB 1 10.88.88.70 默认端口
全局目录配置
PD 1 10.88.88.70 默认端口
全局目录配置
TiFlash 1 10.88.88.70 默认端口
全局目录配置
Monitor 1 10.88.88.70 默认端口
全局目录配置

安装

下载并安装 TiUP
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
执行打印日志如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload Upload Total Spent Left Speed
100 5152k 100 5152k 0 0 4201k 0 0:00:01 0:00:01 --:--:-- 4206k
WARN: adding root certificate via internet: https://tiup-mirrors.pingcap.com/root.json
You can revoke this by remove /root/.tiup/bin/7b8e153f2e2d0928.root.json
Successfully set mirror to https://tiup-mirrors.pingcap.com
Detected shell: bash
Shell profile: /root/.bash_profile
/root/.bash_profile has been modified to add tiup to PATH
open a new terminal or source /root/.bash_profile to use it
Installed path: /root/.tiup/bin/tiup
===============================================
Have a try: tiup playground
===============================================

声明全局环境变量
source /root/.bash_profile

安装 TiUP 的 cluster 组件
tiup cluster
如果机器已经安装 TiUP cluster,需要更新软件版本:
tiup update --self && tiup update cluster

由于模拟多机部署,需要通过 root 用户调大 sshd 服务的连接数限制:
修改 /etc/ssh/sshd_configMaxSessions 调至 20。
重启 sshd 服务:service sshd restart

创建topo.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# # 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: "/home/tidb/tidb-deploy"
data_dir: "/home/tidb/tidb-data"

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

server_configs:
tidb:
instance.tidb_slow_log_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: 10.88.88.70

tidb_servers:
- host: 10.88.88.70

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

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

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

tiflash_servers:
- host: 10.88.88.70

monitoring_servers:
- host: 10.88.88.70

grafana_servers:
- host: 10.88.88.70

执行集群部署命令:
tiup cluster deploy tidb-cluster v7.5.3 ./topo.yaml --user root -p
按照引导,输入”y”及 root 密码,来完成部署

查看集群列表:
tiup cluster list

1
2
3
4
[root@hf2 home]# tiup cluster list
Name User Version Path PrivateKey
---- ---- ------- ---- ----------
tidb-cluster tidb v7.5.3 /root/.tiup/storage/cluster/clusters/tidb-cluster /root/.tiup/storage/cluster/clusters/tidb-cluster/ssh/id_rsa

启动集群:
tiup cluster start tidb-cluster

1
2
3
......
+ [ Serial ] - UpdateTopology: cluster=tidb-cluster
Started cluster `tidb-cluster` successfully

查看集群的拓扑结构和状态:
tiup cluster display tidb-cluster

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@hf2 tidb-topo]# tiup cluster display tidb-cluster
Cluster type: tidb
Cluster name: tidb-cluster
Cluster version: v7.5.3
Deploy user: tidb
SSH type: builtin
Dashboard URL: http://10.88.88.70:2379/dashboard
Grafana URL: http://10.88.88.70:3000
ID Role Host Ports OS/Arch Status Data Dir Deploy Dir
-- ---- ---- ----- ------- ------ -------- ----------
10.88.88.70:3000 grafana 10.88.88.70 3000 linux/x86_64 Up - /home/tidb/tidb-deploy/grafana-3000
10.88.88.70:2379 pd 10.88.88.70 2379/2380 linux/x86_64 Up|L|UI /home/tidb/tidb-data/pd-2379 /home/tidb/tidb-deploy/pd-2379
10.88.88.70:9090 prometheus 10.88.88.70 9090/12020 linux/x86_64 Up /home/tidb/tidb-data/prometheus-9090 /home/tidb/tidb-deploy/prometheus-9090
10.88.88.70:4000 tidb 10.88.88.70 4000/10080 linux/x86_64 Up - /home/tidb/tidb-deploy/tidb-4000
10.88.88.70:9000 tiflash 10.88.88.70 9000/8123/3930/20170/20292/8234 linux/x86_64 Up /home/tidb/tidb-data/tiflash-9000 /home/tidb/tidb-deploy/tiflash-9000
10.88.88.70:20160 tikv 10.88.88.70 20160/20180 linux/x86_64 Up /home/tidb/tidb-data/tikv-20160 /home/tidb/tidb-deploy/tikv-20160
10.88.88.70:20161 tikv 10.88.88.70 20161/20181 linux/x86_64 Up /home/tidb/tidb-data/tikv-20161 /home/tidb/tidb-deploy/tikv-20161
10.88.88.70:20162 tikv 10.88.88.70 20162/20182 linux/x86_64 Up /home/tidb/tidb-data/tikv-20162 /home/tidb/tidb-deploy/tikv-20162
Total nodes: 8

修改root密码

安装mysql client
yum install mysql
登录客户端
mysql -h 127.0.0.1 -P 4000 -u root
修改root密码
SET PASSWORD FOR 'root'@'%' = 'xxx';

监控服务

Grafana 监控: http://10.88.88.70:3000/
默认用户名和密码均为 admin,第一次进入可以修改密码

TiDB 的 Dashboard: http://10.88.88.70:2379/dashboard/
使用root账号和密码登录

停止集群

tiup cluster stop tidb-cluster

删除集群

清除数据:tiup cluster clean tidb-cluster --all
彻底删除:tiup cluster destroy tidb-cluster

重启集群

tiup cluster restart tidb-cluster

问题

  1. tiup cluster deploy tidb-cluster v7.5.3 ./topo.yaml --user root -p 部署报错
    1
    2
    3
    4
    Error: Failed to initialize TiDB environment on remote host '10.88.88.70' (task.env_init.failed)
    caused by: Failed to create '~/.ssh' directory for user 'tidb'
    caused by: Failed to execute command over SSH for 'root@10.88.88.70:22'
    caused by: Process exited with status 1
    先创建了个/home/tidb文件夹,删除手动创建的文件夹,重新执行正常