Zabbix-tidb
什么是tidb
TiDB 是 PingCAP 公司基于 Google Spanner / F1 论文实现的开源分布式 NewSQL 数据库。
1.TiDB 集群主要分为三个组件
1.1 TiDB Server
TiDB Server 负责接收 SQL 请求,处理 SQL 相关的逻辑,并通过 PD 找到存储计算所需数据的 TiKV 地址,与 TiKV 交互获取数据,最终返回结果。 TiDB Server是无状态的,其本身并不存储数据,只负责计算,可以无限水平扩展,可以通过负载均衡组件(如LVS、HAProxy 或 F5)对外提供统一的接入地址。
1.2 PD Server
Placement Driver (简称 PD) 是整个集群的管理模块,其主要工作有三个: 一是存储集群的元信息(某个 Key 存储在哪个 TiKV 节点);二是对 TiKV 集群进行调度和负载均衡(如数据的迁移、Raft group leader 的迁移等);三是分配全局唯一且递增的事务 ID。
PD 是一个集群,需要部署奇数个节点,一般线上推荐至少部署 3 个节点。
1.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 为单位进行调度。
2. 下载并解压压缩包
[root@server1 ~]# ls
172.25.16.250 ca_zabbix_release-3.0.1.tar.gz schema.sql.gz tidb-latest-linux-amd64.tar.gz zbx_percona_mysql_template.xml
4.0 percona-zabbix-templates-1.1.8-1.noarch.rpm simkai.ttf zabbix.api
[root@server1 ~]# tar zxf tidb-latest-linux-amd64.tar.gz
[root@server1 ~]# ls
172.25.16.250 ca_zabbix_release-3.0.1.tar.gz schema.sql.gz tidb-latest-linux-amd64 zabbix.api
4.0 percona-zabbix-templates-1.1.8-1.noarch.rpm simkai.ttf tidb-latest-linux-amd64.tar.gz zbx_percona_mysql_template.xml
3. 配置环境
三台主机
172.25.16.1 server1 zabbix-server mariadb PD1,TiDB
172.25.16.2 server2 Tikv1
172.25.16.3 server3 Tikv2
172.25.16.4 server4 Tikv3
3.1 开启PD
[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.16.1:2379" --peer-urls="http://172.25.16.1:2380" --initial-cluster="pd1=http://172.25.16.1:2380" --log-file=pd.log &
[1] 7341
[root@server1 tidb-latest-linux-amd64]# netstat -antlp
Active Internet connections (servers and established)
tcp 0 0 172.25.16.1:2379 0.0.0.0:* LISTEN 7341/./bin/pd-serve
tcp 0 0 172.25.16.1:2380 0.0.0.0:* LISTEN 7341/./bin/pd-serve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3133/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3585/master
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 3156/zabbix_agentd
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 3213/zabbix_server
3.2 开启tikv
server2,3,4操作一致
[root@server2 ~]# tar zxf tidb-latest-linux-amd64.tar.gz
[root@server2 ~]# cd tidb-latest-linux-amd64/
[root@server2 tidb-latest-linux-amd64]# ./bin/tikv-server --pd="172.25.16.1:2379" --addr="172.25.16.2:20160" --data-dir=tikv1 --log-file=tikv.log &
[1] 7198
[root@server2 tidb-latest-linux-amd64]# netstat -antlp
Active Internet connections (servers and established)
tcp6 0 0 :::22 :::* LISTEN 3151/sshd
tcp6 0 0 ::1:25 :::* LISTEN 3400/master
tcp6 0 0 172.25.16.2:20160 :::* LISTEN 7198/./bin/tikv-ser
tcp6 0 0 :::10050 :::* LISTEN 3163/zabbix_agentd
tcp6 0 0 172.25.16.2:35092 172.25.16.1:2379 TIME_WAIT -
tcp6 0 0 172.25.16.2:35098 172.25.16.1:2379 ESTABLISHED 7198/./bin/tikv-ser
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
[root@server1 tidb-latest-linux-amd64]# netstat -antlp | grep 4000
tcp6 0 0 :::4000 :::* LISTEN 2834/./bin/tidb-ser
5. 数据库连接tidb
[root@server1 tidb-latest-linux-amd64]# mysql -h 172.25.16.1 -P 4000 -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.1-TiDB-0.9.0 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)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 0 rows affected (0.01 sec)
MySQL [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'westos';
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]>
6. 给数据库中导入数据
[root@server1 tidb-latest-linux-amd64]# 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.16.1 -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_java_gateway.conf zabbix_java_gateway_logback.xml 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'] = 'localhost';
$DB['PORT'] = '4000';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'westos';
8. 重启服务
[root@server1 web]# systemctl restart zabbix-server
[root@server1 web]# systemctl restart zabbix-agent
浏览器端进行访问:172.25.4.111/zabbix
出现原因:之前导入的数据库数据不够完整
解决方法:将之前的数据库备份出来并同步到tidb数据库系统的zabbix中
[root@server1 zabbix-server-mysql-4.0.5]# mysqldump -uroot -pwestos zabbix >/mnt/zabbix.sql
[root@server1 zabbix-server-mysql-4.0.5]# mysql -h 172.25.16.1 -P 4000 -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.7.1-TiDB-0.9.0 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)]> use zabbix;
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 [zabbix]> set tidb_batch_insert=1;
Query OK, 0 rows affected (0.00 sec)
MySQL [zabbix]> source /mnt/zabbix.sql;
Query OK, 0 rows affected (0.00 sec)
9. 开启服务
在浏览器中进行查看