TiDB集群安装主要操作

TiDB集群安装主要操作

参考资料:https://www.cnblogs.com/plyx/archive/2018/12/21/10158615.html

一、TiDB数据简介

  TiDB 是 PingCAP 公司设计的开源分布式 HTAP (Hybrid Transactional and Analytical Processing) 数据库,结合了传统的 RDBMS 和 NoSQL 的最佳特性。

TiDB 兼容 MySQL,支持无限的水平扩展,具备强一致性和高可用性。TiDB 的目标是为 OLTP (Online Transactional Processing) 和

OLAP (Online Analytical Processing) 场景提供一站式的解决方案。


  TiDB 具备如下特性:

1、高度兼容 MySQL

  大多数情况下,无需修改代码即可从 MySQL 轻松迁移至 TiDB,分库分表后的 MySQL 集群亦可通过 TiDB 工具进行实时迁移。


2、水平弹性扩展

  通过简单地增加新节点即可实现 TiDB 的水平扩展,按需扩展吞吐或存储,轻松应对高并发、海量数据场景。


3、分布式事务

  TiDB 100% 支持标准的 ACID 事务。

4、真正金融级高可用

  相比于传统主从 (M-S) 复制方案,基于 Raft 的多数派选举协议可以提供金融级的 100% 数据强一致性保证,且在不丢失大多数副本的前提下,

  可以实现故障的自动恢复 (auto-failover),无需人工介入。


5、一站式 HTAP 解决方案
  TiDB 作为典型的 OLTP 行存数据库,同时兼具强大的 OLAP 性能,配合 TiSpark,可提供一站式 HTAP 解决方案,一份存储同时处理 OLTP & OLAP,

  无需传统繁琐的 ETL 过程。


6、云原生SQL数据库
  TiDB 是为云而设计的数据库,支持公有云、私有云和混合云,使部署、配置和维护变得十分简单。

  TiDB Server:

    TiDB Server 负责接收 SQL 请求,处理 SQL 相关的逻辑,并通过 PD 找到存储计算所需数据的 TiKV 地址,与 TiKV 交互获取数据,最终返回结果。

  TiDB Server 是无状态的,其本身并不存储数据,只负责计算,可以无限水平扩展,可以通过负载均衡组件(如LVS、HAProxy 或 F5)对外提供统一的接入地址。
  

  PD Server:

    Placement Driver (简称 PD) 是整个集群的管理模块,其主要工作有三个:一是存储集群的元信息(某个 Key 存储在哪个 TiKV 节点);

  二是对 TiKV 集群进行调度和负载均衡(如数据的迁移、Raft group leader 的迁移等);三是分配全局唯一且递增的事务 ID。PD 是一个集群,

  需要部署奇数个节点,一般线上推荐至少部署 3 个节点。

  TiKV Server:

    TiKV Server 负责存储数据,从外部看 TiKV 是一个分布式的提供事务的 Key-Value 存储引擎。存储数据的基本单位是 Region,

  每个 Region 负责存储一个 Key Range(从 StartKey 到 EndKey 的左闭右开区间)的数据,每个 TiKV 节点会负责多个 Region。TiKV 使用 Raft 协议做复制,

  保持数据的一致性和容灾。副本以 Region 为单位进行管理,不同节点上的多个 Region 构成一个 Raft Group,互为副本。数据在多个 TiKV 之间的负载均衡

  由 PD 调度,这里也是以 Region 为单位进行调度
  

  TiSpark:

    TiSpark 作为 TiDB 中解决用户复杂 OLAP 需求的主要组件,将 Spark SQL 直接运行在 TiDB 存储层上,同时融合 TiKV 分布式集群的优势,

  并融入大数据社区生态。至此,TiDB 可以通过一套系统,同时支持 OLTP 与 OLAP,免除用户数据同步的烦恼。

 二、生产环境部署推荐

  标准 TiDB 集群需要 6 台机器:
###########################################################################
2 个 TiDB 节点
3 个 PD 节点
3 个 TiKV 节点,第一台 TiDB 机器同时用作监控机
默认情况下,单台机器上只需部署一个 TiKV 实例。如果你的 TiKV 部署机器 CPU 及内存配置是部署
建议的两倍或以上,并且拥有两块 SSD 硬盘或单块容量超 2T 的 SSD 硬盘,可以考虑部署两实例,
但不建议部署两个以上实例。

单机单 TiKV 实例集群拓扑
Name    Host IP    Services
node1    172.16.10.1    PD1, TiDB1
node2    172.16.10.2    PD2, TiDB2
node3    172.16.10.3    PD3
node4    172.16.10.4    TiKV1
node5    172.16.10.5    TiKV2
node6    172.16.10.6    TiKV3
###########################################################################

三、个人演示环境部署

分配机器资源
# 单机Tikv实例
Name HostIP Services
bj-db-m1 10.10.146.28 PD1, TiDB1, TiKV1
bj-db-m2 10.10.1.139  PD2, TiDB2, TiKV2
bj-db-m3 10.10.173.84 PD3, TiKV3

10.10.69.73     bj-db-manage

3-1、安装中控机软件

yum -y install epel-release git curl sshpass atop vim htop net-tools 
yum -y install python-pip

3-2、在中控机上创建 tidb 用户,并生成 ssh key

# 创建tidb用户
useradd -m -d /home/tidb tidb
echo tidbpwd | passwd --stdin tidb

3-3、配置tidb用户sudo权限

# 配置tidb用户sudo权限
visudo
tidb ALL=(ALL) NOPASSWD: ALL


# 或者 
cat >>/etc/sudoers<<"EOF"
tidb    ALL=(ALL)      NOPASSWD: ALL
EOF

3-4、设置ssh免秘钥登录

  

# 使用tidb账户生成 ssh key
su - tidb 
ssh-keygen -t rsa 

# 一路回车
[root@bj-db-manage ~]# su - tidb
[tidb@bj-db-manage ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tidb/.ssh/id_rsa):
Created directory '/home/tidb/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tidb/.ssh/id_rsa.
Your public key has been saved in /home/tidb/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:XcP3rQqjGN1TDdMsjpy9vsTN+vl+jicsLknJHjx9UIk tidb@bj-db-manage
The key's randomart image is:
+---[RSA 2048]----+
|             . . |
|           .Eoo  |
|            B.+  |
|         o *.B ..|
|        So=o+.. o|
|       . .Bo.+.. |
|      . .o=++o+  |
|       o .+*.oooo|
|      . .  o*++*=|
+----[SHA256]-----+
[tidb@bj-db-manage ~]$

四、在中控机器上下载 TiDB-Ansible

1、# 下载Tidb-Ansible 版本
  cd /home/tidb && git clone -b release-2.0 https://github.com/pingcap/tidb-ansible.git
2、# 安装ansible及依赖
  cd /home/tidb/tidb-ansible/ && pip install -r ./requirements.txt

五、在中控机上配置部署机器ssh互信及sudo 规则

# 配置hosts.ini
su - tidb && cd /home/tidb/tidb-ansible
[tidb@bj-db-manage tidb-ansible]$ cat /home/tidb/tidb-ansible/hosts.ini 
[servers]
10.10.146.28
10.10.1.139
10.10.173.84

[all:vars]
username = tidb
ntp_server = pool.ntp.org
[tidb@bj-db-manage tidb-ansible]$ # 配置ssh 互信 ansible
-playbook -i hosts.ini create_users.yml -u root -k
[tidb@bj-db-manage tidb-ansible]$ ansible-playbook -i hosts.ini create_users.yml -u root -k
SSH password:

PLAY [all] **************************************************************************************************************************

TASK [create user] ******************************************************************************************************************
changed: [10.10.1.139]
changed: [10.10.146.28]
changed: [10.10.173.84]

TASK [set authorized key] ***********************************************************************************************************
changed: [10.10.146.28]
changed: [10.10.1.139]
changed: [10.10.173.84]

TASK [update sudoers file] **********************************************************************************************************
ok: [10.10.1.139]
ok: [10.10.146.28]
ok: [10.10.173.84]

PLAY RECAP **************************************************************************************************************************
10.10.1.139                : ok=3    changed=2    unreachable=0    failed=0   
10.10.146.28               : ok=3    changed=2    unreachable=0    failed=0   
10.10.173.84               : ok=3    changed=2    unreachable=0    failed=0   

Congrats! All goes well. :-)
[tidb@bj-db-manage tidb-ansible]$ # 需要输入root的密码 0zWYbnc55Wh20eDwbRHx

六、在目标机器上安装ntp服务

# 中控机器上给目标主机安装ntp服务
 cd /home/tidb/tidb-ansible
 ansible-playbook -i hosts.ini deploy_ntp.yml -u tidb -b

七、目标机器上调整cpufreq

  备注:传统物理机器需要,我的环境是云主机,不适合

1、 # 查看cpupower 调节模式,目前虚拟机不支持,调节10服务器cpupower
2、 cpupower frequency-info --governors
3、 analyzing CPU 0:
4、 available cpufreq governors: Not Available
5、 # 配置cpufreq调节模式
6、 cpupower frequency-set --governor performance
View Code

八、目标机器上添加数据盘ext4 文件系统挂载

备注:传统物理机器需要,我的环境是云主机,不适合

# 创建分区表
parted -s -a optimal /dev/nvme0n1 mklabel gpt -- mkpart primary ext4 1 -1
# 手动创建分区
parted dev/sdb
mklabel gpt
mkpart primary 0KB 210GB 
# 格式化分区
mkfs.ext4 /dev/sdb
# 查看数据盘分区 UUID
[root@tidb-tikv1 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda 
├─sda1 xfs f41c3b1b-125f-407c-81fa-5197367feb39 /boot
├─sda2 xfs 8119193b-c774-467f-a057-98329c66b3b3 /
├─sda3 
└─sda5 xfs 42356bb3-911a-4dc4-b56e-815bafd08db2 /home
sdb ext4 532697e9-970e-49d4-bdba-df386cac34d2 
# 分别在三台机器上,编辑 /etc/fstab 文件,添加 nodelalloc 挂载参数
vim /etc/fstab
UUID=8119193b-c774-467f-a057-98329c66b3b3 / xfs defaults 0 0
UUID=f41c3b1b-125f-407c-81fa-5197367feb39 /boot xfs defaults 0 0
UUID=42356bb3-911a-4dc4-b56e-815bafd08db2 /home xfs defaults 0 0
UUID=532697e9-970e-49d4-bdba-df386cac34d2 /data ext4 defaults,nodelalloc,noatime 0 2
# 挂载数据盘
mkdir /data
mount -a
mount -t ext4
/dev/sdb on /data type ext4 (rw,noatime,seclabel,nodelalloc,data=ordered)
View Code

九、分配机器资源

# 单机Tikv实例
Name HostIP Services
tidb-tikv1 10.10.146.28 PD1, TiDB1, TiKV1
tidb-tikv2 10.10.1.139  PD2,  TiKV2
tidb-tikv3 10.10.173.84 TiKV3

十、编辑inventory.ini 文件

# 编辑inventory.ini 文件
cd /home/tidb/tidb-ansible
cd /home/tidb/tidb-ansible
vim /home/tidb/tidb-ansible/inventory.ini
## TiDB Cluster Part
[tidb_servers]
10.10.146.28

[tikv_servers]
10.10.146.28
10.10.1.139
10.10.173.84

[pd_servers]
10.10.146.28
10.10.1.139

[spark_master]

[spark_slaves]

## Monitoring Part
# prometheus and pushgateway servers
[monitoring_servers]
10.10.146.28
[grafana_servers]
10.10.146.28
# node_exporter and blackbox_exporter servers
[monitored_servers]
10.10.146.28
10.10.1.139
10.10.173.84
[alertmanager_servers]

[kafka_exporter_servers]

## Binlog Part
[pump_servers:children]
tidb_servers

[drainer_servers]

## Group variables
[pd_servers:vars]
# location_labels = ["zone","rack","host"]

## Global variables
[all:vars]
deploy_dir = /data/tidb/deploy

## Connection
# ssh via normal user
ansible_user = tidb

cluster_name = test-cluster

tidb_version = v2.0.11

# process supervision, [systemd, supervise]
process_supervision = systemd

timezone = Asia/Shanghai

enable_firewalld = False
# check NTP service
enable_ntpd = True
set_hostname = False

## binlog trigger
enable_binlog = False
# zookeeper address of kafka cluster for binlog, example:
# zookeeper_addrs = "192.168.0.11:2181,192.168.0.12:2181,192.168.0.13:2181"
zookeeper_addrs = ""
# kafka cluster address for monitoring, example:
# kafka_addrs = "192.168.0.11:9092,192.168.0.12:9092,192.168.0.13:9092"
kafka_addrs = ""

# store slow query log into seperate file
enable_slow_query_log = False

# enable TLS authentication in the TiDB cluster
enable_tls = False

# KV mode
deploy_without_tidb = False

# Optional: Set if you already have a alertmanager server.
# Format: alertmanager_host:alertmanager_port
alertmanager_target = ""

grafana_admin_user = "admin"
grafana_admin_password = "admin"


### Collect diagnosis
collect_log_recent_hours = 2

enable_bandwidth_limit = True
# default: 10Mb/s, unit: Kbit/s
collect_bandwidth_limit = 10000
View Code

十一、检测ssh互信

[tidb@bj-db-manage tidb-ansible]$ ansible -i inventory.ini all -m shell -a 'whoami'
10.10.146.28 | SUCCESS | rc=0 >>
tidb

10.10.1.139 | SUCCESS | rc=0 >>
tidb

10.10.173.84 | SUCCESS | rc=0 >>
tidb

[tidb@bj-db-manage tidb-ansible]$ 

十二、检测tidb 用户 sudo 免密码配置

[tidb@bj-db-manage tidb-ansible]$  ansible -i inventory.ini all -m shell -a 'whoami' -b
10.10.146.28 | SUCCESS | rc=0 >>
root

10.10.173.84 | SUCCESS | rc=0 >>
root

10.10.1.139 | SUCCESS | rc=0 >>
root

[tidb@bj-db-manage tidb-ansible]$ 

十三、联网下载 TiDB binary 到中控机

#执行 local_prepare.yml playbook,联网下载 TiDB binary 到中控机
ansible-playbook local_prepare.yml
# 初始化系统环境,修改内核参数
# 注释掉磁盘检查
# 参考资料:https://blog.csdn.net/mayancheng7/article/details/93896233

十四、修改检测参数

  说明:TiDB对硬件配置的要求相当高,在测试过程中,需要跳过预检查。

14-1、fio_randread.yml

[tidb@bj-db-manage tidb-ansible]$ cat  /home/tidb/tidb-ansible/roles/machine_benchmark/tasks/fio_randread.yml
---

#- name: fio randread benchmark on tikv_data_dir disk
#  shell: "cd {{ fio_deploy_dir }} && ./fio -ioengine=psync -bs=32k -fdatasync=1 -thread -rw=randread -size={{ benchmark_size }} -filename=fio_randread_test.txt -name='fio randread test' -iodepth=4 -runtime=60 -numjobs=4 -group_reporting --output-format=json --output=fio_randread_result.json"
#  register: fio_randread

#- name: clean fio randread benchmark temporary file
#  file:
#    path: "{{ fio_deploy_dir }}/fio_randread_test.txt"
#    state: absent

#- name: get fio randread iops
#  shell: "python parse_fio_output.py --target='fio_randread_result.json' --read-iops"
#  register: disk_randread_iops
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: get fio randread summary
#  shell: "python parse_fio_output.py --target='fio_randread_result.json' --summary"
#  register: disk_randread_smmary
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: fio randread benchmark command
#  debug:
#    msg: "fio randread benchmark command: {{ fio_randread.cmd }}."
#  run_once: true

#- name: fio randread benchmark summary
#  debug:
#    msg: "fio randread benchmark summary: {{ disk_randread_smmary.stdout }}."

#- name: Preflight check - Does fio randread iops of tikv_data_dir disk meet requirement
#  fail:
#    msg: 'fio: randread iops of tikv_data_dir disk is too low: {{ disk_randread_iops.stdout }} < {{ min_ssd_randread_iops }}, it is strongly recommended to use SSD disks for TiKV and PD, or there might be performance issues.'
#  when: disk_randread_iops.stdout|int < min_ssd_randread_iops|int
View Code

14-2、fio_randread_write_latency.yml

[tidb@bj-db-manage tidb-ansible]$ cat /home/tidb/tidb-ansible/roles/machine_benchmark/tasks/fio_randread_write_latency.yml          
---

#- name: fio mixed randread and sequential write benchmark for latency on tikv_data_dir disk
#  shell: "cd {{ fio_deploy_dir }} && ./fio -ioengine=psync -bs=32k -fdatasync=1 -thread -rw=randrw -percentage_random=100,0 -size={{ benchmark_size }} -filename=fio_randread_write_latency_test.txt -name='fio mixed randread and sequential write test' -iodepth=1 -runtime=60 -numjobs=1 -group_reporting --output-format=json --output=fio_randread_write_latency_test.json"
#  register: fio_randread_write_latency

#- name: clean fio mixed randread and sequential write benchmark for latency temporary file
#  file:
#    path: "{{ fio_deploy_dir }}/fio_randread_write_latency_test.txt"
#    state: absent

#- name: get fio mixed test randread latency
#  shell: "python parse_fio_output.py --target='fio_randread_write_latency_test.json' --read-lat"
#  register: disk_mix_randread_lat
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: get fio mixed test write latency
#  shell: "python parse_fio_output.py --target='fio_randread_write_latency_test.json' --write-lat"
#  register: disk_mix_write_lat
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: get fio mixed randread and sequential write for latency summary
#  shell: "python parse_fio_output.py --target='fio_randread_write_latency_test.json' --summary"
#  register: disk_mix_randread_write_latency_smmary
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: fio mixed randread and sequential write benchmark for latency command
#  debug:
#    msg: "fio mixed randread and sequential write benchmark for latency command: {{ fio_randread_write_latency.cmd }}."
#  run_once: true

#- name: fio mixed randread and sequential write benchmark for latency summary
#  debug:
#    msg: "fio mixed randread and sequential write benchmark summary: {{ disk_mix_randread_write_latency_smmary.stdout }}."

#- name: Preflight check - Does fio mixed randread and sequential write latency of tikv_data_dir disk meet requirement - randread
#  fail:
#    msg: 'fio mixed randread and sequential write test: randread latency of  tikv_data_dir disk is too low: {{ disk_mix_randread_lat.stdout }} ns > {{ max_ssd_mix_randread_lat }} ns, it is strongly recommended to use SSD disks for TiKV and PD, or there might be performance issues.'
#  when: disk_mix_randread_lat.stdout|int > max_ssd_mix_randread_lat|int

#- name: Preflight check - Does fio mixed randread and sequential write latency of tikv_data_dir disk meet requirement - sequential write
#  fail:
#    msg: 'fio mixed randread and sequential write test: sequential write latency of tikv_data_dir disk is too low: {{ disk_mix_write_lat.stdout }} ns > {{ max_ssd_mix_write_lat }} ns, it is strongly recommended to use SSD disks for TiKV and PD, or there might be performance issues.'
#  when: disk_mix_write_lat.stdout|int > max_ssd_mix_write_lat|int
View Code

14-3、fio_randread_write.yml

[tidb@bj-db-manage tidb-ansible]$ cat  /home/tidb/tidb-ansible/roles/machine_benchmark/tasks/fio_randread_write.yml      
---

#- name: fio mixed randread and sequential write benchmark on tikv_data_dir disk
#  shell: "cd {{ fio_deploy_dir }} && ./fio -ioengine=psync -bs=32k -fdatasync=1 -thread -rw=randrw -percentage_random=100,0 -size={{ benchmark_size }} -filename=fio_randread_write_test.txt -name='fio mixed randread and sequential write test' -iodepth=4 -runtime=60 -numjobs=4 -group_reporting --output-format=json --output=fio_randread_write_test.json"
#  register: fio_randread_write

#- name: clean fio mixed randread and sequential write benchmark temporary file
#  file:
#    path: "{{ fio_deploy_dir }}/fio_randread_write_test.txt"
#    state: absent

#- name: get fio mixed test randread iops
#  shell: "python parse_fio_output.py --target='fio_randread_write_test.json' --read-iops"
#  register: disk_mix_randread_iops
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: get fio mixed test write iops
#  shell: "python parse_fio_output.py --target='fio_randread_write_test.json' --write-iops"
#  register: disk_mix_write_iops
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: get fio mixed randread and sequential write summary
#  shell: "python parse_fio_output.py --target='fio_randread_write_test.json' --summary"
#  register: disk_mix_randread_write_smmary
#  args:
#    chdir: "{{ fio_deploy_dir }}/"

#- name: fio mixed randread and sequential write benchmark command
#  debug:
#    msg: "fio mixed randread and sequential write benchmark command: {{ fio_randread_write.cmd }}."
#  run_once: true

#- name: fio mixed randread and sequential write benchmark summary
#  debug:
#    msg: "fio mixed randread and sequential write benchmark summary: {{ disk_mix_randread_write_smmary.stdout }}."

#- name: Preflight check - Does fio mixed randread and sequential write iops of tikv_data_dir disk meet requirement - randread
#  fail:
#    msg: 'fio mixed randread and sequential write test: randread iops of  tikv_data_dir disk is too low: {{ disk_mix_randread_iops.stdout }} < {{ min_ssd_mix_randread_iops }}, it is strongly recommended to use SSD disks for TiKV and PD, or there might be performance issues.'
#  when: disk_mix_randread_iops.stdout|int < min_ssd_mix_randread_iops|int

#- name: Preflight check - Does fio mixed randread and sequential write iops of tikv_data_dir disk meet requirement - sequential write
#  fail:
#    msg: 'fio mixed randread and sequential write test: sequential write iops of tikv_data_dir disk is too low: {{ disk_mix_write_iops.stdout }} < {{ min_ssd_mix_write_iops }}, it is strongly recommended to use SSD disks for TiKV and PD, or there might be performance issues.'
#  when: disk_mix_write_iops.stdout|int < min_ssd_mix_write_iops|int
View Code
# 执行如下命令,进行检查:

ansible-playbook bootstrap.yml

十五、安装TiD集群

ansible-playbook deploy.yml

十六、启动Tidb集群

#启动集群服务 
ansible-playbook start.yml

十七、测试集群

# 使用 MySQL 客户端连接测试,TCP 4000 端口是 TiDB 服务默认端口
mysql -u root -h 10.10.146.28 -P 4000
[tidb@bj-db-manage tidb-ansible]$ mysql -u root -h 10.10.146.28 -P 4000
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MySQL connection id is 41
Server version: 5.7.10-TiDB-v2.0.11 MySQL Community Server (Apache License 2.0)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| INFORMATION_SCHEMA |
| PERFORMANCE_SCHEMA |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

十八、访问监控web页面

# 通过浏览器访问监控平台
地址:http://10.10.146.28:3000 默认帐号密码是:admin/admin

十九、监控配置(可选)

本小节介绍如何配置 Grafana。

第 1 步:添加 Prometheus 数据源
登录 Grafana 界面。

默认地址:http://10.10.146.28:3000
默认账户:admin
默认密码:admin
点击 Grafana 图标打开侧边栏。

在侧边栏菜单中,点击 Data Source。

点击 Add data source。

指定数据源的相关信息:

在 Name 处,为数据源指定一个名称。
在 Type 处,选择 Prometheus。
在 URL 处,指定 Prometheus 的 IP 地址。
根据需求指定其它字段。
点击 Add 保存新的数据源。

第 2 步:导入 Grafana 面板
执行以下步骤,为 PD Server、TiKV Server 和 TiDB Server 分别导入 Grafana 面板:

点击侧边栏的 Grafana 图标。

在侧边栏菜单中,依次点击 Dashboards > Import 打开 Import Dashboard 窗口。

点击 Upload .json File 上传对应的 JSON 文件(下载 TiDB Grafana 配置文件)。
注意:

TiKV、PD 和 TiDB 面板对应的 JSON 文件分别为 tikv_summary.json,tikv_details.json,
tikv_trouble_shooting.json,pd.json,tidb.json,tidb_summary.json。

wget https://github.com/pingcap/tidb-ansible/blob/master/scripts/tikv_summary.json
wget https://github.com/pingcap/tidb-ansible/blob/master/scripts/tikv_details.json
wget https://github.com/pingcap/tidb-ansible/blob/master/scripts/tikv_trouble_shooting.json
wget https://github.com/pingcap/tidb-ansible/blob/master/scripts/pd.json
wget https://github.com/pingcap/tidb-ansible/blob/master/scripts/tidb.json
wget https://github.com/pingcap/tidb-ansible/blob/master/scripts/tidb_summary.json
View Code

二十、TiDB运维常见操作

1、停止集群操作
ansible-playbook  stop.yml
2、启动集群操作
ansible-playbook  start.yml
3、清除集群数据 此操作会关闭 TiDB、Pump、TiKV、PD 服务,并清空 Pump、TiKV、PD 数据目录
[tidb@bj-db-manage ~]$ cd tidb-ansible
[tidb@bj-db-manage tidb-ansible]$ ansible-playbook unsafe_cleanup_data.yml
销毁集群
此操作会关闭集群,并清空部署目录,若部署目录为挂载点,会报错,可忽略。
ansible-playbook unsafe_cleanup.yml

二十一、备份与恢复

#备份与恢复,下载TIDB工具集
wget http://download.pingcap.org/tidb-enterprise-tools-latest-linux-amd64.tar.gz && 
wget http://download.pingcap.org/tidb-enterprise-tools-latest-linux-amd64.sha256
#检查文件完整性
sha256sum -c tidb-enterprise-tools-latest-linux-amd64.sha256
# 解压
tar -xzf tidb-enterprise-tools-latest-linux-amd64.tar.gz && 
cd tidb-enterprise-tools-latest-linux-amd64
#使用 mydumper/loader 全量备份恢复数据
#mydumper 是一个强大的数据备份工具,具体可以参考:https://github.com/maxbube/mydumper
# 可使用 mydumper 从 TiDB 导出数据进行备份,然后用 loader 将其导入到 TiDB 里面进行恢复。
/*
注意:

必须使用企业版工具集包的 mydumper,不要使用你的操作系统的包管理工具提供的 mydumper。mydumper 的上游版本并不能对 
TiDB 进行正确处理 (#155)。由于使用 mysqldump 进行数据备份和恢复都要耗费许多时间,这里也并不推荐。
*/


mydumper/loader 全量备份恢复最佳实践,为了快速地备份恢复数据 (特别是数据量巨大的库),可以参考以下建议:
  使用 Mydumper 导出来的数据文件尽可能的小,最好不要超过 64M,可以将参数 -F 设置为 64。
Loader的 -t 参数可以根据 TiKV 的实例个数以及负载进行评估调整,推荐设置为 32。当 TiKV 负载过高,Loader 以及 TiDB 日志中出现大量

backoffer.maxSleep 15000ms is exceeded 时,可以适当调小该值;当 TiKV 负载不是太高的时候,可以适当调大该值。

从 TiDB 备份数据
我们使用 mydumper 从 TiDB 备份数据,如下:
./bin/mydumper -h 10.10.146.28 -P 4000 -u root -t 32 -F 64 -B test -T t1,t2 --skip-tz-utc -o /data/test.t1.t1.dumper.sql
[tidb@bj-db-manage tidb-enterprise-tools-latest-linux-amd64]$ ./bin/mydumper -h 10.10.146.28 -P 4000 -u root -t 32 -F 64 -B test -T t1,t2 --skip-tz-utc -o /data/test.t1.t1.dumper.sql
[tidb@bj-db-manage tidb-enterprise-tools-latest-linux-amd64]$ ll /data/test.t1.t1.dumper.sql/
total 16
-rw-rw-r-- 1 tidb tidb 129 Nov 12 16:34 metadata
-rw-rw-r-- 1 tidb tidb  64 Nov 12 16:34 test-schema-create.sql
-rw-rw-r-- 1 tidb tidb 134 Nov 12 16:34 test.t1-schema.sql
-rw-rw-r-- 1 tidb tidb  34 Nov 12 16:34 test.t1.sql
[tidb@bj-db-manage tidb-enterprise-tools-latest-linux-amd64]$

上面,我们使用 -B test 表明是对 test 这个 database 操作,然后用 -T t1,t2 表明只导出 t1,t2 两张表。

-t 32 表明使用 32 个线程去导出数据。-F 64 是将实际的 table 切分成多大的 chunk,这里就是 64MB 一个 chunk。

--skip-tz-utc 添加这个参数忽略掉 TiDB 与导数据的机器之间时区设置不一致的情况,禁止自动转换。

向 TiDB 恢复数据

MySQL [(none)]> drop database test;
Query OK, 0 rows affected (0.24 sec)

[tidb@bj-db-manage tidb-enterprise-tools-latest-linux-amd64]$  ./bin/loader  -h 10.10.146.28 -P 4000  -u root  -t 32 -d /data/test.t1.t1.dumper.sql/
2019/11/12 16:36:44 printer.go:52: [info] Welcome to loader
2019/11/12 16:36:44 printer.go:53: [info] Release Version: v1.0.0-76-gad009d9
2019/11/12 16:36:44 printer.go:54: [info] Git Commit Hash: ad009d917b2cdc2a9cc26bc4e7046884c1ff43e7
2019/11/12 16:36:44 printer.go:55: [info] Git Branch: master
2019/11/12 16:36:44 printer.go:56: [info] UTC Build Time: 2019-10-21 06:22:03
2019/11/12 16:36:44 printer.go:57: [info] Go Version: go version go1.12 linux/amd64
2019/11/12 16:36:44 main.go:51: [info] config: {"log-level":"info","log-file":"","status-addr":":8272","pool-size":32,"dir":"/data/test.t1.t1.dumper.sql/","db":{"host":"10.10.146.28","user":"root","port":4000,"sql-mode":"@DownstreamDefault","max-allowed-packet":67108864},"checkpoint-schema":"tidb_loader","config-file":"","route-rules":null,"do-table":null,"do-db":null,"ignore-table":null,"ignore-db":null,"rm-checkpoint":false}
 2019/11/12 16:36:44 loader.go:532: [info] [loader] prepare takes 0.000103 seconds
2019/11/12 16:36:44 checkpoint.go:207: [info] calc checkpoint finished. finished tables (map[])
2019/11/12 16:36:44 loader.go:715: [info] [loader][run db schema]/data/test.t1.t1.dumper.sql//test-schema-create.sql[start]
2019/11/12 16:36:45 loader.go:720: [info] [loader][run db schema]/data/test.t1.t1.dumper.sql//test-schema-create.sql[finished]
2019/11/12 16:36:45 loader.go:736: [info] [loader][run table schema]/data/test.t1.t1.dumper.sql//test.t1-schema.sql[start]
2019/11/12 16:36:45 loader.go:741: [info] [loader][run table schema]/data/test.t1.t1.dumper.sql//test.t1-schema.sql[finished]
2019/11/12 16:36:45 loader.go:773: [info] [loader] create tables takes 0.290772 seconds
2019/11/12 16:36:45 loader.go:788: [info] [loader] all data files have been dispatched, waiting for them finished 
2019/11/12 16:36:45 loader.go:158: [info] [loader][restore table data sql]/data/test.t1.t1.dumper.sql//test.t1.sql[start]
2019/11/12 16:36:46 loader.go:216: [info] data file /data/test.t1.t1.dumper.sql/test.t1.sql scanned finished.
2019/11/12 16:36:46 loader.go:165: [info] [loader][restore table data sql]/data/test.t1.t1.dumper.sql//test.t1.sql[finished]
2019/11/12 16:36:46 loader.go:791: [info] [loader] all data files has been finished, takes 2.039493 seconds
2019/11/12 16:36:46 status.go:32: [info] [loader] finished_bytes = 34, total_bytes = GetAllRestoringFiles34, progress = 100.00 %
2019/11/12 16:36:46 main.go:88: [info] loader stopped and exits 
[tidb@bj-db-manage tidb-enterprise-tools-latest-linux-amd64]$  

二十二、添加节点扩容 TiDB/TiKV 节点(这里添加一个tidb节点)

cd  /home/tidb/tidb-ansible
1、编辑 inventory.ini 文件,添加节点信息:
vim /home/tidb/tidb-ansible/inventory.ini

2、初始化新增节点:
ansible-playbook bootstrap.yml -l 10.10.1.139
3、部署新增节点:
ansible-playbook deploy.yml -l 10.10.1.139
4、启动新节点
ansible-playbook start.yml -l 10.10.1.139
5、更新 Prometheus 配置并重启:
ansible-playbook rolling_update_monitor.yml --tags=prometheus
6、打开浏览器访问监控平台:http://10.10.146.28:3000,监控整个集群和新增节点的状态。
可使用同样的步骤添加 TiKV 节点。但如果要添加 PD 节点,则需手动更新一些配置文件。

二十三、缩容 TiDB 节点

# 缩容 TiDB 节点
1、停止该节点上的服务:
ansible-playbook stop.yml -l 10.10.1.139
2、
vim /home/tidb/tidb-ansible/inventory.ini 
3、
ansible-playbook rolling_update_monitor.yml --tags=prometheus

二十四、升级TiDB版本

安装ansible及其依赖
https://pingcap.com/docs-cn/stable/how-to/deploy/orchestrated/ansible#%E5%9C%A8%E4%B8%AD%E6%8E%A7%E6%9C%BA%E5%99%A8%E4%B8%8A%E5%AE%89%E8%A3%85-ansible-%E5%8F%8A%E5%85%B6%E4%BE%9D%E8%B5%96

https://pingcap.com/docs-cn/stable/how-to/upgrade/from-previous-version/
在中控机器上下载 TiDB Ansible
以 tidb 用户登录中控机并进入 /home/tidb 目录,备份 TiDB 2.0 版本或 TiDB 2.1 版本的 tidb-ansible 文件夹:
su - tidb
# 以 tidb 用户登录中控机并进入 /home/tidb 目录,备份 TiDB 2.0 版本或 TiDB 2.1 版本的 tidb-ansible 文件夹:
mv tidb-ansible tidb-ansible-bak
#下载 TiDB 3.0 版本对应 tag 的 tidb-ansible 下载 TiDB Ansible,默认的文件夹名称为 tidb-ansible。

git clone -b v3.0.5 https://github.com/pingcap/tidb-ansible.git
[tidb@bj-db-manage tidb-ansible]$ cat /home/tidb/tidb-ansible/hosts.ini
[servers]
10.10.146.28
10.10.1.139
10.10.173.84

[all:vars]
username = tidb
ntp_server = pool.ntp.org
[tidb@bj-db-manage tidb-ansible]$
#编辑 inventory.ini 文件和配置文件 #以tidb 用户登录中控机并进入 /home/tidb/tidb-ansible 目录
[tidb@bj-db-manage tidb-ansible]$ cat /home/tidb/tidb-ansible/inventory.ini
## TiDB Cluster Part
[tidb_servers]
10.10.146.28
10.10.1.139

[tikv_servers]
10.10.146.28
10.10.1.139
10.10.173.84

[pd_servers]
10.10.146.28
10.10.1.139

[spark_master]

[spark_slaves]

[lightning_server]

[importer_server]

## Monitoring Part
# prometheus and pushgateway servers
[monitoring_servers]
10.10.146.28

[grafana_servers]
10.10.146.28

# node_exporter and blackbox_exporter servers
[monitored_servers]
10.10.146.28
10.10.1.139
10.10.173.84

[alertmanager_servers]
10.10.146.28

[kafka_exporter_servers]

## Binlog Part
[pump_servers]

[drainer_servers]

## Group variables
[pd_servers:vars]
# location_labels = ["zone","rack","host"]

## Global variables
[all:vars]
deploy_dir = /data/tidb/deploy

## Connection
# ssh via normal user
ansible_user = tidb

cluster_name = Pro-cluster

tidb_version = v3.0.5

# process supervision, [systemd, supervise]
process_supervision = systemd

timezone = Asia/Shanghai

enable_firewalld = False
# check NTP service
enable_ntpd = True
set_hostname = False

## binlog trigger
enable_binlog = False

# kafka cluster address for monitoring, example:
# kafka_addrs = "192.168.0.11:9092,192.168.0.12:9092,192.168.0.13:9092"
kafka_addrs = ""

# store slow query log into seperate file
enable_slow_query_log = True

# zookeeper address of kafka cluster for monitoring, example:
# zookeeper_addrs = "192.168.0.11:2181,192.168.0.12:2181,192.168.0.13:2181"
zookeeper_addrs = ""

# enable TLS authentication in the TiDB cluster
enable_tls = False

# KV mode
deploy_without_tidb = False

# wait for region replication complete before start tidb-server.
wait_replication = True

# Optional: Set if you already have a alertmanager server.
# Format: alertmanager_host:alertmanager_port
alertmanager_target = ""

grafana_admin_user = "admin"
grafana_admin_password = "admin"


### Collect diagnosis
collect_log_recent_hours = 2

enable_bandwidth_limit = True
# default: 10Mb/s, unit: Kbit/s
collect_bandwidth_limit = 20000
[tidb@bj-db-manage tidb-ansible]$


vi inventory.ini 参照之前的参数文件修改ip及路径
vi /home/tidb/tidb-ansible/conf/tikv.yml 参照之前的参数文件修改内存等参数
vi /home/tidb/tidb-ansible/conf/alertmanager.yml 参照之前的参数文件修改邮件信息
vi /home/tidb/tidb-ansible/conf/tidb.yml (修改split-table: true)
vi /home/tidb/tidb-ansible/conf/pump.yml ( 修改gc: 10)
vi /home/tidb/tidb-ansible/group_vars/all.yml (修改tikv_metric_method: "pull")
vi /home/tidb/tidb-ansible/roles/prometheus/defaults/main.yml(修改prometheus_storage_retention: "90d")

下载 TiDB 3.0 binary 到中控机

[tidb@bj-db-manage tidb-ansible]$ pwd
/home/tidb/tidb-ansible
[tidb@bj-db-manage tidb-ansible]$ ansible-playbook local_prepare.yml
# 滚动升级 TiDB 集群组件
# 如果当前 process_supervision 变量使用默认的 systemd 参数,则通过 excessive_rolling_update.yml 滚动升级 TiDB 集群。
ansible-playbook excessive_rolling_update.yml

#如果当前 process_supervision 变量使用 supervise 参数,则通过 rolling_update.yml 滚动升级 TiDB 集群。
ansible-playbook rolling_update.yml


# 滚动升级 TiDB 监控组件
ansible-playbook rolling_update_monitor.yml

升级完毕后的测试

[tidb@bj-db-manage tidb-ansible]$ mysql -h 10.10.146.28 -P 4000  -u root
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.25-TiDB-v3.0.5 MySQL Community Server (Apache License 2.0)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| INFORMATION_SCHEMA |
| PERFORMANCE_SCHEMA |
| mysql              |
| test               |
| tidb_loader        |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [test]> select * from t1;
+----+------+
| id | name |
+----+------+
|  1 | aa   |
+----+------+
1 row in set (0.00 sec)

MySQL [test]> show tables from tidb_loader;
+-----------------------+
| Tables_in_tidb_loader |
+-----------------------+
| checkpoint            |
+-----------------------+
1 row in set (0.00 sec)

MySQL [test]> 
# 升级完毕

二十五、tidb的限制

https://pingcap.com/docs-cn/stable/reference/mysql-compatibility/


生产环境强烈推荐最低要求:
3TiKV+3PD+2TiDB

tidb使用坑记录
1、对硬盘要求很高,没上SSD硬盘的不建议使用

2、不支持分区,删除数据是个大坑。

解决方案:set @@session.tidb_batch_delete=1;

3、插入数据太大也会报错

解决方案:set @@session.tidb_batch_insert=1;

4、删除表数据时不支持别名

delete from 表名 表别名 where 表别名.col = '1'  会报错

5、内存使用有问题,GO语言导致不知道回收机制什么时候运作。内存使用过多会导致TIDB当机(这点完全不像MYSQL)

测试情况是,32G内存,在10分钟后才回收一半。

6、数据写入的时候,tidb压力很大, tikv的CPU也占用很高

7、不支持GBK

8、不支持存储过程

#  sysbench测试
 curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
 sudo yum -y install sysbench
 
 mysql -h 10.10.69.73 -P 4000 -u root
 create database sbtest
 
 sysbench /usr/share/sysbench/oltp_common.lua --mysql-host=10.10.146.28 --mysql-port=4000 --mysql-user=root --tables=20 --table_size=20000000 --threads=100 --max-requests=0 prepare
 
 sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=10.10.146.28 --mysql-port=4000 --mysql-user=root --tables=20 --table_size=2000000 --threads=10 --max-requests=0 run
 
 
 ############ tidb 优缺点 ############
 tidb 就几个疑问来学习,我们为什么使用TIDB(优点)?
 
生产环境强烈推荐最低要求:
3TiKV+3PD+2TiDB

 优点:
 1、tidb完全支持sql语法,对mysql的兼容做的比较好,可以单独当作直接写入库,
    也可以当作mysql库当从库(版本不一致时,不推荐这么做)

 2、对于大表查询速度极快,上亿条数据查询 秒级别(用这个的主要原因所在)

 3、可以在线添加架构节点(pd   tidb  tikv),水平拓展 (用这个的主要原因所在)

缺点:
1、数据库中存储数据同样数据保持三份,挺浪费存储空间
2、如果当从库使用的话,对于上游修改字段长度 只能修改基于原来基础增长而不能缩短 以及修改从无
   符号修改有符号值时会导致不同步(即只能加长,不能缩短)
3、数据写入的时候,tidb压力很大, tikv的CPU也占用很高,写入相对较慢
4、对硬盘要求很高,没上SSD硬盘的不建议使用
5、对于删除数据(dml操作)需要执行相对应的命令,才能回收高水位


不支持分区,删除数据是个大坑。
插入数据太大也会报错
不支持GBK
删除表数据时不支持别名

备注:如果是独立使用TIDB,问题会更少些(遵循规范)。


原文地址:https://www.cnblogs.com/bjx2020/p/11840380.html