一.Tidb的简单介绍

1.Tidb是什么

TiDB 是 PingCAP 公司设计的开源分布式 HTAP (Hybrid Transactional and Analytical Processing) 数据库,结合了传统的 RDBMS 和 NoSQL 的最佳特性。TiDB 兼容 MySQL,支持无限的水平扩展,具备强一致性和高可用性。TiDB 的目标是为 OLTP (Online Transactional Processing) 和 OLAP (Online Analytical Processing) 场景提供一站式的解决方案

2.整体架构

TiDB 集群主要包括三个核心组件:TiDB Server,PD Server 和 TiKV Server。此外,还有用于解决用户复杂 OLAP 需求的 TiSpark 组件

tidb 主从架构_tidb 主从架构


(1)Tidb Server

TiDB Server 负责接收 SQL 请求,处理 SQL 相关的逻辑,并通过 PD 找到存储计算所需数据的 TiKV 地址,与 TiKV 交互获取数据,最终返回结果。TiDB Server 是无状态的,其本身并不存储数据,只负责计算,可以无限水平扩展,可以通过负载均衡组件(如LVS、HAProxy 或 F5)对外提供统一的接入地址。

(2)PD Server

Placement Driver (简称 PD) 是整个集群的管理模块,其主要工作有三个:一是存储集群的元信息(某个 Key 存储在哪个 TiKV 节点);二是对 TiKV 集群进行调度和负载均衡(如数据的迁移、Raft group leader 的迁移等);三是分配全局唯一且递增的事务 ID。

PD 通过 Raft 协议保证数据的安全性。Raft 的 leader server 负责处理所有操作,其余的 PD server 仅用于保证高可用。
建议部署奇数个 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 为单位进行调度
(4)TiSpark
TiSpark 作为 TiDB 中解决用户复杂 OLAP 需求的主要组件,将 Spark SQL 直接运行在 TiDB 存储层上,同时融合 TiKV 分布式集群的优势,并融入大数据社区生态。至此,TiDB 可以通过一套系统,同时支持 OLTP 与 OLAP,免除用户数据同步的烦恼
二.Tidb+Zabbix的简单部署
基于之前zabbix部署来实验
实验环境部署
三台主机
172.25.4.111 server1 zabbix-server mariadb PD1,TiDB
172.25.4.113 server3 Tikv1
172.25.4.114 server4 Tikv2
172.25.4.115 server5 Tikv3
1.校验安装包是否完整

sha256sum -c tidb-latest-linux-amd64.sha256
tidb-latest-linux-amd64.tar.gz: OK

2.解压并开启PD

[root@server1 ~]# ls
4.0  tidb-latest-linux-amd64.sha256  tidb-latest-linux-amd64.tar.gz
[root@server1 ~]# tar zxf tidb-latest-linux-amd64.tar.gz 
[root@server1 ~]# cd tidb-latest-linux-amd64
[root@server1 tidb-latest-linux-amd64]# ls
bin
[root@server1 tidb-latest-linux-amd64]# ./bin/pd-server --name=pd1 --data-dir=pd1 --client-urls="http://172.25.4.111:2379" --peer-urls="http://172.25.4.111:2380" --initial-cluster="pd1=http://172.25.4.111:2380" --log-file=pd.log &  ##开启PD并后台运行
[1] 2404
[root@server1 tidb-latest-linux-amd64]# netstat -antlp  ##查看2379与2380端口是否开放
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 172.25.4.111:2379       0.0.0.0:*               LISTEN      2404/./bin/pd-serve 
tcp        0      0 172.25.4.111:2380       0.0.0.0:*               LISTEN      2404/./bin/pd-serve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      679/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1105/master         
tcp        0      0 172.25.4.111:36808      172.25.4.111:2379       ESTABLISHED 2404/./bin/pd-serve 
tcp        0      0 172.25.4.111:34130      172.25.4.111:2380       TIME_WAIT   -                   
tcp        0      0 172.25.4.111:22         172.25.4.250:39388      ESTABLISHED 2334/sshd: root@pts 
tcp        0      0 172.25.4.111:2379       172.25.4.111:36810      ESTABLISHED 2404/./bin/pd-serve 
tcp        0      0 172.25.4.111:36810      172.25.4.111:2379       ESTABLISHED 2404/./bin/pd-serve 
tcp        0      0 172.25.4.111:2379       172.25.4.111:36808      ESTABLISHED 2404/./bin/pd-serve 
tcp6       0      0 :::80                   :::*                    LISTEN      609/httpd           
tcp6       0      0 :::22                   :::*                    LISTEN      679/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1105/master

3.解压并开启tikv

[root@server3 ~]# tar zxf tidb-latest-linux-amd64.tar.gz 
[root@server3 ~]# cd tidb-latest-linux-amd64
[root@server3 tidb-latest-linux-amd64]# ./bin/tikv-server --pd="172.25.4.111:2379" --addr="172.25.4.113:20160" --data-dir=tikv1 --log-file=tikv.log &
[1] 11884
[root@server3 tidb-latest-linux-amd64]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      733/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      886/master          
tcp        0      0 172.25.4.113:22         172.25.4.250:48002      ESTABLISHED 2498/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      733/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      886/master          
[1]+  Illegal instruction     ./bin/tikv-server --pd="172.25.4.111:2379" --addr="172.25.4.113:20160" --data-dir=tikv1 --log-file=tikv.log
[root@server4 ~]# tar zxf tidb-latest-linux-amd64.tar.gz 
[root@server4 ~]# cd tidb-latest-linux-amd64
[root@server4 tidb-latest-linux-amd64]# ./bin/tikv-server --pd="172.25.4.111:2379" --addr="172.25.4.114:20160" --data-dir=tikv1 --log-file=tikv.log &
[1] 2096
[root@server4 tidb-latest-linux-amd64]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      661/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      820/master          
tcp        0      0 172.25.4.114:22         172.25.4.250:49404      ESTABLISHED 2047/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      661/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      820/master          
[1]+  Illegal instruction     ./bin/tikv-server --pd="172.25.4.111:2379" --addr="172.25.4.114:20160" --data-dir=tikv1 --log-file=tikv.log
[root@server5 tidb-latest-linux-amd64]# ./bin/tikv-server --pd="172.25.4.111:2379" --addr="172.25.4.115:20160" --data-dir=tikv1 --log-file=tikv.log &
[1] 11432
[root@server5 tidb-latest-linux-amd64]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      658/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      868/master          
tcp        0      0 172.25.4.115:22         172.25.4.250:56858      ESTABLISHED 2040/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      658/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      868/master          
[1]+  Illegal instruction     ./bin/tikv-server --pd="172.25.4.111:2379" --addr="172.25.4.115:20160" --data-dir=tikv1 --log-file=tikv.log

4.开启zabbix及tidb

[root@server1 tidb-latest-linux-amd64]# ./bin/tidb-server  ##开启时间较为缓慢
[root@server1 tidb-latest-linux-amd64]# ./bin/tidb-server &  ##可以打入后台运行
[2] 2834
[root@server1 tidb-latest-linux-amd64]# ps ax|grep tidb
 2834 pts/0    Sl     0:00 ./bin/tidb-server
 2846 pts/0    S+     0:00 grep --color=auto tidb

查看4000端口是否开启

[root@server1 tidb-latest-linux-amd64]# netstat -antlp | grep 4000
tcp6       0      0 :::4000                 :::*                    LISTEN      2834/./bin/tidb-ser

启动顺序为PD-tikv-tidb(顺序很重要)启动时选择后台启动,避免前台失效后程序自动退出
5.数据库连接tidb

[root@server1 tidb-latest-linux-amd64]# mysql -h 172.25.4.111 -P 4000 -uroot  ##数据库通过4000端口连接tidb,连接成功
[2019/07/08 23:56:03.183 +08:00] [INFO] [server.go:414] ["new connection"] [conn=2] [remoteAddr=172.25.4.111:33920]
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-TiDB-v3.0.0-rc.1-290-g21d2590ac MySQL Community Server (Apache License 2.0)

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

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

MySQL [(none)]> create database zabbix character set utf8 collate utf8_bin;  ##创建数据库
MySQL [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'westos';
ERROR 1105 (HY000): You are not allowed to create a user with GRANT  ##直接给用户授权出现错误,则可以先创建用户再给用户授权
MySQL [(none)]> CREATE USER 'zabbix'@'%' IDENTIFIED BY 'westos';  ##创建用户
Query OK, 1 row affected (0.29 sec)

MySQL [(none)]> grant all privileges on *.* to 'zabbix'@'%';  ##用户授权
Query OK, 0 rows affected (0.00 sec)

6.给数据库中导入数据

[root@server1 ~]# cd /usr/share/doc/zabbix-server-mysql-4.0.5/
[root@server1 zabbix-server-mysql-4.0.5]# ls
AUTHORS  ChangeLog  COPYING  create.sql.gz  NEWS  README
[root@server1 zabbix-server-mysql-4.0.5]# zcat create.sql.gz |mysql -h 172.25.4.111 -P 4000 -uroot zabbix  ##出现错误,原因是数据不全

7.zabbix配置文件的修改

[root@server1 ~]# vim /etc/zabbix/zabbix_server.conf
DBPort=4000  ##修改端口为4000
[root@server1 ~]# cd /etc/zabbix/
[root@server1 zabbix]# ls
web  zabbix_agentd.conf  zabbix_agentd.d  zabbix_server.conf
[root@server1 zabbix]# cd web/
[root@server1 web]# ls
maintenance.inc.php  zabbix.conf.php
[root@server1 web]# vim zabbix.conf.php 
$DB['TYPE']     = 'MYSQL';
$DB['SERVER']   = '172.25.4.111';  ##修改远程主机
$DB['PORT']     = '4000';  ##修改端口
$DB['DATABASE'] = 'zabbix';
$DB['USER']     = 'zabbix';
$DB['PASSWORD'] = 'westos';

重启服务

[root@server1 web]# systemctl restart zabbix-server
[root@server1 web]# systemctl restart zabbix-agent

浏览器端进行访问:172.25.4.111/zabbix

tidb 主从架构_tidb 主从架构_02


出现原因:之前导入的数据库数据不够完整

方法2:将之前的数据库备份出来并同步到tidb数据库系统的zabbix中

[root@server1 zabbix-server-mysql-4.0.5]# mysqldump -uroot -pwestos zabbix >/mnt/zabbix.sql  ##将之前导入的zabbix库的数据倒出来
[root@server1 mnt]#  mysql -h 172.25.4.111 -P 4000 -uroot
MySQL [(none)]> use zabbix;
Database changed
MySQL [zabbix]> set tidb_batch_insert=1;
MySQL [zabbix]> source /mnt/zabbix.sql;  ##将备份的数据库导入tidb的数据库中

浏览器端再次进行访问:172.25.4.111/zabbix

tidb 主从架构_Server_03