TiDB-单机学习环境部署(4.X版本)

TiUP 是 TiDB 4.0 版本引入的集群运维工具,TiUP cluster 是 TiUP 提供的使用 Golang 编写的集群管理组件,通过 TiUP cluster 组件就可以进行日常的运维工作,包括部署、启动、关闭、销毁、弹性扩缩容、升级 TiDB 集群;管理 TiDB 集群参数。
下面使用TiUP工具在RHEL7.6系统上部署TiDB单机环境。

软硬件环境需求

操作系统要求

Linux 操作系统平台 版本
Red Hat Enterprise Linux 7.3 及以上
CentOS 7.3 及以上
Oracle Enterprise Linux 7.3 及以上
Ubuntu LTS 16.04 及以上

注意:TiDB 只支持 Red Hat 兼容内核 (RHCK) 的 Oracle Enterprise Linux,不支持 Oracle Enterprise Linux 提供的 Unbreakable Enterprise Kernel。

软件环境要求配置

存储及文件系统配置

  • 建议使用 EXT4 类型文件系统的 NVME 类型的 SSD 磁盘存储 TiKV 数据文件
  • 挂载文件系统时添加 nodelallocnoatime 挂载参数。

I/O调度器

I/O 调度程序确定 I/O 操作何时在存储设备上运行以及持续多长时间。对于SSD设备,推荐设置为 noop 模式。

echo noop > /sys/block/${SSD_DEV_NAME}/queue/scheduler

检查及关闭swap

TiDB 运行需要有足够的内存,并且不建议使用 swap 作为内存不足的缓冲,这会降低性能。

echo "vm.swappiness = 0">> /etc/sysctl.conf
swapoff -a && swapon -a

关闭防火墙

# 1. 检查防火墙状态
sudo firewall-cmd --state
sudo systemctl status firewalld.service

# 2. 关闭防火墙
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

# 3. 检查确认
sudo systemctl status firewalld.service
sudo systemctl is-enable firewalld.service

检查及配置NTP服务

TiDB 是一套分布式数据库系统,需要节点间保证时间的同步,从而确保 ACID 模型的事务线性一致性。

# 1. 检查状态
sudo systemctl status ntpd.service
ntpstat

# 2. 安装ntp服务
sudo yum -y install ntp ntpdate && 
sudo systemctl start ntpd.service && 
sudo systemctl enable ntpd.service

检测和关闭透明大页

对于数据库应用,不推荐使用透明大页(即 Transparent Huge Pages,缩写为 THP),因为数据库的内存访问模式往往是稀疏的而非连续的。而且当高阶内存碎片化比较严重时,分配 THP 页面会出现较大的延迟。若开启针对 THP 的直接内存规整功能,也会出现系统 CPU 使用率激增的现象,因此建议关闭透明大页。

# 1. 查看透明大页的开启状态 -- 如果返回 [always] madvise never 则表示处于启用状态
cat /sys/kernel/mm/transparent_hugepage/enabled

# 2. grubby 命令查看默认内核版本
grubby --default-kernel

# 3. 执行 grubby --update-kernel 命令修改内核配置
grubby --args="transparent_hugepage=never" --update-kernel /boot/vmlinuz-3.10.0-957.el7.x86_64

# 4. 执行 grubby --info 命令查看修改后的默认内核配置
grubby --info /boot/vmlinuz-3.10.0-957.el7.x86_64

# 5. 重启
reboot


## 临时修改当前的内核配置(立即生效)
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

配置 SSH 互信及 sudo 免密码

推荐使用 TiUP 部署工具会自动配置 SSH 互信及免密登陆

创建 tidb 用户并设置登录密码

useradd -m -d /home/tidb tidb

echo "tidb"|passwd --stdin tidb

配置sudo

visudo

# 添加下面一行
tidb ALL=(ALL) NOPASSWD: ALL

部署TiDB

参照 TiUP 最小拓扑,准备一个 YAML 文件进行部署 TiDB 单机环境。

拓扑结构

实例 个数 IP 配置
TiKV 3 192.168.10.181 避免端口和目录冲突
TiDB 1 192.168.10.181 默认端口 全局目录配置
PD 1 192.168.10.181 默认端口 全局目录配置
Monitor 1 192.168.10.181 默认端口 全局目录配置

详细过程

下载并安装TiUP工具(联网)

curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh

安装集群组件

tiup cluster

确认当前 TiUP cluster 版本信息

tiup --binary cluster

创建最小架构 yaml 文件

global:
 user: "tidb"
 ssh_port: 22
 deploy_dir: "/tidb/app/tidb-deploy"
 data_dir: "/tidb/data"

# # Monitored variables are applied to all the machines.
monitored:
 node_exporter_port: 9100
 blackbox_exporter_port: 9115

server_configs:
 tidb:
   log.slow-threshold: 300
 tikv:
   readpool.storage.use-unified-pool: false
   readpool.coprocessor.use-unified-pool: true
 pd:
   replication.enable-placement-rules: true
   replication.location-labels: ["host"]
 tiflash:
   logger.level: "info"

pd_servers:
 - host: 192.168.10.181

tidb_servers:
 - host: 192.168.10.181

tikv_servers:
 - host: 192.168.10.181
   port: 20160
   status_port: 20180
   config:
     server.labels: { host: "logic-host-1" }

 - host: 192.168.10.181
   port: 20161
   status_port: 20181
   config:
     server.labels: { host: "logic-host-2" }

 - host: 192.168.10.181
   port: 20162
   status_port: 20182
   config:
     server.labels: { host: "logic-host-3" }

tiflash_servers:
 - host: 192.168.10.181

monitoring_servers:
 - host: 192.168.10.181

grafana_servers:
 - host: 192.168.10.181
 
alertmanager_servers:
  - host: 192.168.10.181

初始化集群

tiup cluster deploy tidb-cluster v4.0.9 ./topology.yaml --user root -p

image-20210105113330652

预期日志结尾输出会有 Deployed cluster tidb-cluster successfully 关键词,表示部署成功。

启动集群

# tiup cluster start <cluster-name>
tiup cluster start tidb-cluster

检查集群状态

# 输出当前通过 TiUP cluster 管理的所有集群信息
tiup cluster list

# 检查部署的 TiDB 集群情况, Status 状态信息为 Up 说明集群状态正常
tiup cluster display tidb-cluster

客户端访问TiDB集群

使用mysql客户端(若已安装 MySQL 客户端)

mysql -h 192.168.10.181 -P 4000 -u root

默认端口为4000,root默认密码为空.

web 界面访问

  • Dashboard
    PD默认安装Dashboard,可以通过 http://{pd-ip}:2379/dashboard 访问集群的监控界面。[默认用户名为 root,密码为空]

  • Grafana
    通过 http://{grafana-ip}:3000 访问监控页面。 [默认用户名和密码均为 admin]

原文地址:https://www.cnblogs.com/binliubiao/p/14275540.html