TiDB集羣安裝主要操做

TiDB集羣安裝主要操做

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

html

1、TiDB數據簡介

  TiDB 是 PingCAP 公司設計的開源分佈式 HTAP (Hybrid Transactional and Analytical Processing) 數據庫,結合了傳統的 RDBMS 和 NoSQL 的最佳特性。node

TiDB 兼容 MySQL,支持無限的水平擴展,具有強一致性和高可用性。TiDB 的目標是爲 OLTP (Online Transactional Processing) 和 python

OLAP (Online Analytical Processing) 場景提供一站式的解決方案。mysql


  TiDB 具有以下特性:linux

一、高度兼容 MySQLgit

  大多數狀況下,無需修改代碼便可從 MySQL 輕鬆遷移至 TiDB,分庫分表後的 MySQL 集羣亦可經過 TiDB 工具進行實時遷移。github


二、水平彈性擴展web

  經過簡單地增長新節點便可實現 TiDB 的水平擴展,按需擴展吞吐或存儲,輕鬆應對高併發、海量數據場景。sql


三、分佈式事務shell

  TiDB 100% 支持標準的 ACID 事務。

四、真正金融級高可用

  相比於傳統主從 (M-S) 複製方案,基於 Raft 的多數派選舉協議能夠提供金融級的 100% 數據強一致性保證,且在不丟失大多數副本的前提下,

  能夠實現故障的自動恢復 (auto-failover),無需人工介入。


五、一站式 HTAP 解決方案
  TiDB 做爲典型的 OLTP 行存數據庫,同時兼具強大的 OLAP 性能,配合 TiSpark,可提供一站式 HTAP 解決方案,一份存儲同時處理 OLTP & OLAP,

  無需傳統繁瑣的 ETL 過程。


六、雲原生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,免除用戶數據同步的煩惱。

 2、生產環境部署推薦

  標準 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
###########################################################################

3、我的演示環境部署

分配機器資源
# 單機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-一、安裝中控機軟件

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

3-二、在中控機上建立 tidb 用戶,並生成 ssh key

# 建立tidb用戶 useradd -m -d /home/tidb tidb echo tidbpwd | passwd --stdin tidb

3-三、配置tidb用戶sudo權限

# 配置tidb用戶sudo權限 visudo tidb ALL=(ALL) NOPASSWD: ALL # 或者 cat >>/etc/sudoers<<"EOF" tidb ALL=(ALL) NOPASSWD: ALL EOF

3-四、設置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 ~]$

4、在中控機器上下載 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

5、在中控機上配置部署機器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

6、在目標機器上安裝ntp服務

# 中控機器上給目標主機安裝ntp服務 cd /home/tidb/tidb-ansible ansible-playbook -i hosts.ini deploy_ntp.yml -u tidb -b

7、目標機器上調整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

8、目標機器上添加數據盤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

9、分配機器資源

# 單機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

10、編輯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

11、檢測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]$

12、檢測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]$

十3、聯網下載 TiDB binary 到中控機

#執行 local_prepare.yml playbook,聯網下載 TiDB binary 到中控機 ansible-playbook local_prepare.yml # 初始化系統環境,修改內核參數 # 註釋掉磁盤檢查 # 參考資料:https://blog.csdn.net/mayancheng7/article/details/93896233

十4、修改檢測參數

  說明:TiDB對硬件配置的要求至關高,在測試過程當中,須要跳過預檢查。

14-一、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-二、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-三、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

十5、安裝TiD集羣

ansible-playbook deploy.yml

十6、啓動Tidb集羣

#啓動集羣服務 ansible-playbook start.yml

十7、測試集羣

# 使用 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)

十8、訪問監控web頁面

# 經過瀏覽器訪問監控平臺 地址:http://10.10.146.28:3000 默認賬號密碼是:admin/admin

十9、監控配置(可選)

本小節介紹如何配置 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

二10、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

二11、備份與恢復

#備份與恢復,下載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]$

二12、添加節點擴容 TiDB/TiKV 節點(這裏添加一個tidb節點)

cd /home/tidb/tidb-ansible
一、編輯 inventory.ini 文件,添加節點信息:
vim /home/tidb/tidb-ansible/inventory.ini

二、初始化新增節點:
ansible-playbook bootstrap.yml -l 10.10.1.139
三、部署新增節點:
ansible-playbook deploy.yml -l 10.10.1.139
四、啓動新節點
ansible-playbook start.yml -l 10.10.1.139
五、更新 Prometheus 配置並重啓:
ansible-playbook rolling_update_monitor.yml --tags=prometheus
六、打開瀏覽器訪問監控平臺:http://10.10.146.28:3000,監控整個集羣和新增節點的狀態。
可以使用一樣的步驟添加 TiKV 節點。但若是要添加 PD 節點,則需手動更新一些配置文件。

二十3、縮容 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

二十4、升級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]> 
# 升級完畢

二十5、tidb的限制

https://pingcap.com/docs-cn/stable/reference/mysql-compatibility/生產環境強烈推薦最低要求:3TiKV+3PD+2TiDBtidb使用坑記錄一、對硬盤要求很高,沒上SSD硬盤的不建議使用二、不支持分區,刪除數據是個大坑。解決方案:set @@session.tidb_batch_delete=1; 三、插入數據太大也會報錯解決方案:set @@session.tidb_batch_insert=1; 四、刪除表數據時不支持別名delete from 表名 表別名 where 表別名.col = '1'  會報錯五、內存使用有問題,GO語言致使不知道回收機制何時運做。內存使用過多會致使TIDB當機(這點徹底不像MYSQL)測試狀況是,32G內存,在10分鐘後纔回收一半。六、數據寫入的時候,tidb壓力很大, tikv的CPU也佔用很高七、不支持GBK八、不支持存儲過程#  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 優勢: 一、tidb徹底支持sql語法,對mysql的兼容作的比較好,能夠單獨看成直接寫入庫,    也能夠看成mysql庫當從庫(版本不一致時,不推薦這麼作) 二、對於大表查詢速度極快,上億條數據查詢 秒級別(用這個的主要緣由所在) 三、能夠在線添加架構節點(pd   tidb  tikv),水平拓展 (用這個的主要緣由所在)缺點:一、數據庫中存儲數據一樣數據保持三份,挺浪費存儲空間二、若是當從庫使用的話,對於上游修改字段長度 只能修改基於原來基礎增加而不能縮短 以及修改從無   符號修改有符號值時會致使不一樣步(即只能加長,不能縮短)三、數據寫入的時候,tidb壓力很大, tikv的CPU也佔用很高,寫入相對較慢四、對硬盤要求很高,沒上SSD硬盤的不建議使用 五、對於刪除數據(dml操做)須要執行相對應的命令,才能回收高水位不支持分區,刪除數據是個大坑。插入數據太大也會報錯不支持GBK刪除表數據時不支持別名備註:若是是獨立使用TIDB,問題會更少些(遵循規範)。

相關文章
相關標籤/搜索