TiDB 是 PingCAP 公司设计的开源分布式 HTAP (Hybrid Transactional and Analytical Processing) 数据库,结合了传统的 RDBMS 和 NoSQL 的最佳特性。TiDB 兼容 MySQL,支持无限的水平扩展,具备强一致性和高可用性。TiDB 的目标是为 OLTP (Online Transactional Processing) 和 OLAP (Online Analytical Processing) 场景提供一站式的解决方案。
官方链接:https://pingcap.com/docs-cn/v3.0/overview/
不建议单节点,开发环境使用.
[root@iZ31lo2i2a80qcZ /]# cd tmp/
[root@iZ31lo2i2a80qcZ tmp]# wget http://download.pingcap.org/tidb-latest-linux-amd64.tar.gz
[root@iZ31lo2i2a80qcZ tmp]# wget http://download.pingcap.org/tidb-latest-linux-amd64.sha256
///验证文件完整性/
[root@iZ31lo2i2a80qcZ tmp]# sha256sum -c tidb-latest-linux-amd64.sha256
tidb-latest-linux-amd64.tar.gz: OK #返回ok说明文件没问题
[root@iZ31lo2i2a80qcZ tmp]# tar -zxvf tidb-latest-linux-amd64.tar.gz
[root@iZ31lo2i2a80qcZ tmp]# mv tidb-latest-linux-amd64 /tidb-server
[root@iZ31lo2i2a80qcZ /]# cd tidb-server/
[root@iZ31la80qcZ tidb-server]# ./bin/pd-server --data-dir=/data/pd --log-file=/data/logs/pd.log &
[root@iZ31la80qcZ tidb-server]# ps -ef|grep pd-server
root 11900 11812 0 14:12 pts/0 00:00:00 ./bin/pd-server --data-dir=/data/pd --log-file=/data/logs/pd.log
root 11916 11812 0 14:13 pts/0 00:00:00 grep --color=auto pd-server
[root@iZ31lo2i2a80qcZ tidb-server]# ./bin/tikv-server --pd="127.0.0.1:2379" --data-dir=/data/tikv --log-file=/data/logs/tikv.log &
[root@iZ31lo2i2a80qcZ tidb-server]# ps -ef|grep tikv-server
root 11920 11812 10 14:14 pts/0 00:00:02 ./bin/tikv-server --pd=127.0.0.1:2379 --data-dir=/data/tikv --log-file=/data/logs/tikv.log
root 12021 11812 0 14:14 pts/0 00:00:00 grep --color=auto tikv-server
[root@iZ31lo2i2a80qcZ tidb-server]# ./bin/tidb-server --store=tikv --path="127.0.0.1:2379" --log-file=/data/logs/tidb.log &
[root@iZ31lo2i2a80qcZ tidb-server]# ps -ef|grep tidb-server
root 12025 11812 1 14:15 pts/0 00:00:00 ./bin/tidb-server --store=tikv --path=127.0.0.1:2379 --log-file=/data/logs/tidb.log
root 12063 11812 0 14:16 pts/0 00:00:00 grep --color=auto tidb-server
[root@iZ31lo2i2a80qcZ tidb-server]# mysql -h 127.0.0.1 -P 4000 -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.25-TiDB-v4.0.0-alpha-1258-g8e88825f5 TiDB Server (Apache License 2.0), MySQL 5.7 compatible
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
踩过的坑:单节点在数据库无连接时会自动关闭
Host | Hostname | Tidb | Pd | Tikv | Grafana | CPU | RAM |
---|---|---|---|---|---|---|---|
172.18.12.209 | tidb-pd | √ | √ | √ | 4 | 4 | |
172.18.12.210 | tikv-1 | √ | 2 | 2 | |||
172.18.12.211 | tikv-2 | √ | 2 | 2 | |||
172.18.12.212 | Tikv-3 | √ | 2 | 2 |
[root@localhost ~]# hostnamectl set-hostname tidb-pd
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost tidb-pd localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# reboot
[root@localhost ~]# hostnamectl set-hostname tidb-pd
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost tikv-1 localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# reboot
[root@localhost ~]# hostnamectl set-hostname tidb-pd
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost tikv-2 localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.
[root@localhost ~]# reboot
[root@localhost ~]# hostnamectl set-hostname tidb-pd
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost tikv-3 localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.
[root@localhost ~]# reboot
[root@localhost ~]# fdisk -l
[root@localhost ~]# parted -s -a optimal /dev/sdb mklabel gpt -- mkpart primary ext4 1 -1
[root@localhost ~]# mkfs.ext4 /dev/sdb
[root@localhost ~]# lsblk -f
[root@localhost ~]# vi /etc/fstab
UUID=25fbd496-b5bd-480f-8c2a-de4044f91074 /data1 ext4 defaults,nodelalloc,noatime 0 2
[root@localhost ~]# mkdir /data1 && mount -a
[root@localhost ~]# mount -t ext4
/dev/sdb on /data1 type ext4 (rw,noatime,seclabel,nodelalloc,data=ordered)
[root@tidb-pd ~]# yum -y install epel-release git curl sshpass
[root@tidb-pd ~]# yum -y install python2-pip
[root@tidb-pd tidb-ansible]# pip install --upgrade pip
[root@tidb-pd ~]# useradd -m -d /home/tidb tidb
[root@tidb-pd ~]# passwd tidb
[root@tidb-pd ~]# visudo
tidb ALL=(ALL) NOPASSWD: ALL
[root@tidb-pd ~]# su - tidb
[tidb@tidb-pd ~]$ ssh-keygen -t rsa
[tidb@tidb-pd ~]$ git clone -b v3.0.2 https://github.com/pingcap/tidb-ansible.git
[tidb@tidb-pd ~]$cd /home/tidb/tidb-ansible
[tidb@tidb-pd tidb-ansible]$ sudo pip install -r ./requirements.txt
[tidb@tidb-pd tidb-ansible]$ cd /home/tidb/tidb-ansible
[tidb@tidb-pd tidb-ansible]$ vi hosts.ini
[servers]
172.18.12.209
172.18.12.210
172.18.12.211
172.18.12.212
[all:vars]
username = tidb
ntp_server = pool.ntp.org
[tidb@tidb-pd tidb-ansible]$ ansible-playbook -i hosts.ini create_users.yml -u root -k
[tidb@tidb-pd tidb-ansible]$ cd /home/tidb/tidb-ansible
[tidb@tidb-pd tidb-ansible]$ ansible-playbook -i hosts.ini deploy_ntp.yml -u tidb -b
以 tidb 用户登录中控机,编辑 /home/tidb/tidb-ansible/inventory.ini 文件为 TiDB 集群分配机器资源。一个标准的 TiDB 集群需要 6 台机器:2 个 TiDB 实例,3 个 PD 实例,3 个 TiKV 实例。
[tidb@tidb-pd tidb-ansible]$ vim inventory.ini
## TiDB Cluster Part
[tidb_servers]
172.18.12.209
[tikv_servers]
172.18.12.210
172.18.12.211
172.18.12.212
[pd_servers]
172.18.12.209
[spark_master]
[spark_slaves]
[lightning_server]
[importer_server]
## Monitoring Part
# prometheus and pushgateway servers
[monitoring_servers]
172.18.12.209
[grafana_servers]
172.18.12.209
# node_exporter and blackbox_exporter servers
[monitored_servers]
172.18.12.209
172.18.12.210
172.18.12.211
172.18.12.212
部署目录通过 deploy_dir 变量控制,默认全局变量已设置为 /home/tidb/deploy,对所有服务生效。如数据盘挂载目录为 /data1,可设置为 /data1/deploy,样例如下:
## Global variables
[all:vars]
deploy_dir = /data1/deploy
执行以下命令,如果所有 server 均返回 tidb,表示 SSH 互信配置成功:
[tidb@tidb-pd tidb-ansible]$ ansible -i inventory.ini all -m shell -a 'whoami'
172.18.12.212 | CHANGED | rc=0 >>
tidb
172.18.12.211 | CHANGED | rc=0 >>
tidb
172.18.12.210 | CHANGED | rc=0 >>
tidb
172.18.12.209 | CHANGED | rc=0 >>
Tidb
执行以下命令,如果所有 server 均返回 root,表示 tidb 用户 sudo 免密码配置成功。
[tidb@tidb-pd tidb-ansible]$ ansible -i inventory.ini all -m shell -a 'whoami' -b
172.18.12.209 | CHANGED | rc=0 >>
root
172.18.12.211 | CHANGED | rc=0 >>
root
172.18.12.210 | CHANGED | rc=0 >>
root
172.18.12.212 | CHANGED | rc=0 >>
Root
执行 local_prepare.yml playbook,联网下载 TiDB binary 到中控机。
[tidb@tidb-pd tidb-ansible]$ ansible-playbook local_prepare.yml
初始化系统环境,修改内核参数。
这里由于我的配置不够:cpu核心数(我的为单核心,官方需要8核心)。磁盘空间太小,内存太小等等会检测不通过,所以需要修改bootstrap.yml文件把检测任务注释掉,真正的生产环境中最痛通过所有的检测,否则会有性能问题。
- name: check system
hosts: all
any_errors_fatal: true
roles:
- check_system_static
# - { role: check_system_optional, when: not dev_mode|default(false) }
- name: tikv_servers machine benchmark
hosts: tikv_servers
gather_facts: false
roles:
# - { role: machine_benchmark, when: not dev_mode|default(false) }
[tidb@tidb-pd tidb-ansible]$ ansible-playbook bootstrap.yml
部署Tidb集群软件
[tidb@tidb-pd tidb-ansible]$ ansible-playbook deploy.yml
启动Tidb集群
[tidb@tidb-pd tidb-ansible]$ ansible-playbook start.yml
安装源
[root@tidb-pd tidb-ansible]#
rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
可以通过yum搜索
[root@tidb-pd tidb-ansible]# yum search mysql-community
安装客户端
[root@tidb-pd tidb-ansible]# yum -y install mysql-community-client.x86_64
[root@tidb-pd tidb-ansible]# mysql -u root -h 127.0.0.1 -P 4000
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
mysql> flush privileges;
Host | Hostname | Tidb | Pd | Tikv | Grafana | CPU | RAM |
---|---|---|---|---|---|---|---|
172.18.12.209 | tidb-pd | √ | √ | √ | 4 | 4 | |
172.18.12.210 | tikv-1 | √ | 2 | 2 | |||
172.18.12.211 | tikv-2 | √ | 2 | 2 | |||
172.18.12.212 | Tikv-3 | √ | 2 | 2 |
组件 | 端口 | 协议 | 描述 |
---|---|---|---|
TiDB | 4000 | TCP | 应用程序的通信端口和DBA工具 |
TiDB | 10080 | TCP | 通信端口TiDB报告状态 |
TiKV | 20160 | TCP | TiKV通信端口 |
Pd | 2379 | TCP | 之间的通信端口TiDB和PD |
Pd | 2380 | TCP | 集群中的节点间通信端口PD |
[root@localhost /]# fdisk -l
磁盘 /dev/sdb:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000a8b94
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@localhost /]# swapoff /dev/mapper/centos-swap
[root@localhost /]# vim /etc/fstab
#注释掉这一句
#/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost /]# free -m
total used free shared buff/cache available
Mem: 10381 211 9683 11 486 9851
Swap: 0 0 0
[root@localhost /]# vim /etc/sysctl.conf
新增一句:vm.swappiness=0
[root@localhost /]# sysctl -p
[root@localhost /]# ulimit -SHn 1000000
[root@localhost /]# vim /etc/security/limits.conf
新增:
# BEGIN ANSIBLE MANAGED BLOCK
* soft nofile 1000000
* hard nofile 1000000
* soft stack 10240
[root@localhost /]# vim /etc/sysctl.conf
新增:
net.core.somaxconn=32768
net.ipv4.tcp_syncookies=0
fs.file-max=1000000
[root@localhost /]# sysctl -p
[root@localhost /]# parted -s -a optimal /dev/sdb mklabel gpt -- mkpart primary ext4 1 -1
[root@localhost /]# mkfs.ext4 /dev/sdb
[root@localhost /]# lsblk -f
[root@localhost /]# vim /etc/fstab
增加:
UUID=b118c778-114c-4de6-9304-081f436739e6 /data1 ext4 defaults,nodelalloc,noatime 0 2
[root@localhost /]# mkdir /data1 && mount -a
[root@localhost /]# mount -t ext4
/dev/sdb on /data1 type ext4 (rw,noatime,seclabel,nodelalloc,data=ordered)#挂载参数中没有nodelalloc就有问题
[root@localhost /]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 1.2G 16G 7% /
devtmpfs 5.1G 0 5.1G 0% /dev
tmpfs 5.1G 0 5.1G 0% /dev/shm
tmpfs 5.1G 12M 5.1G 1% /run
tmpfs 5.1G 0 5.1G 0% /sys/fs/cgroup
/dev/sda1 1014M 146M 869M 15% /boot
tmpfs 1.1G 0 1.1G 0% /run/user/0
/dev/sdb 50G 53M 47G 1% /data1
[root@localhost]# wget http://download.pingcap.org/tidb-latest-linux-amd64.tar.gz
[root@localhost]# wget http://download.pingcap.org/tidb-latest-linux-amd64.sha256
///验证文件完整性/
[root@localhost]# sha256sum -c tidb-latest-linux-amd64.sha256
tidb-latest-linux-amd64.tar.gz: OK #返回ok说明文件没问题
[root@localhost]# tar -zxvf tidb-latest-linux-amd64.tar.gz
[root@localhost tidb]# mkdir config data log
[root@localhost tidb]# cd config/
[root@localhost config]# vim config.toml
# TiDB Configuration.
# TiDB server port.
port = 3306
[root@localhost config]# cd ..
[root@localhost tidb]# vim pd_start.sh
./bin/pd-server --name=pd1 \
--data-dir=/data1/tidb/data/pd \
--client-urls="http://172.18.12.233:2379" \
--peer-urls="http://172.18.12.233:2380" \
--initial-cluster="pd1=http://172.18.12.233:2380" \
-L "info" \
--log-file=/data1/tidb/log/pd.log &
[root@localhost tidb]# vim tidb_start.sh
./bin/tidb-server --store=tikv \
--path="172.18.12.233:2379" \
--config="config/config.toml" \
--log-file=/data1/tidb/log/tidb.log &
[root@localhost tidb]# systemctl disable firewalld
[root@localhost tidb]# systemctl stop firewalld
#正式环境请只开放需要用到的端口
[root@localhost tidb]# chmod +x pd_start.sh tidb_start.sh
###################Tikv三台主机分别配置#########################
[root@localhost tikv1]# vim tikv_start.sh
./bin/tikv-server --pd="172.18.12.233:2379" \
--addr="172.18.12.234:20160" \
--data-dir=/data1/tikv1/data/tikv \
--log-file=/data1/tikv1/log/tikv.log &
[root@localhost tidb]# chmod +x tikv_start.sh
[root@localhost tikv2]# vim tikv_start.sh
./bin/tikv-server --pd="172.18.12.233:2379" \
--addr="172.18.12.235:20160" \
--data-dir=/data1/tikv1/data/tikv \
--log-file=/data1/tikv1/log/tikv.log &
[root@localhost tidb]# chmod +x tikv_start.sh
[root@localhost tikv3]# vim tikv_start.sh
./bin/tikv-server --pd="172.18.12.233:2379" \
--addr="172.18.12.236:20160" \
--data-dir=/data1/tikv1/data/tikv \
--log-file=/data1/tikv1/log/tikv.log &
[root@localhost tidb]# chmod +x tikv_start.sh
安装源
[root@localhost tidb]#
rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
可以通过yum搜索
[root@localhost tidb]# yum search mysql-community
安装客户端
[root@localhost tidb]# yum -y install mysql-community-client.x86_64
[root@localhost tidb]# mysql -u root -h 127.0.0.1 -P 4000
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
mysql> flush privileges;
.Config配置文件参数详解:
https://github.com/pingcap/tidb/blob/release-3.0/config/config.toml.example