当前位置: 首页> 娱乐> 影视 > 【TiDB】08-离线部署TiDB 8.1

【TiDB】08-离线部署TiDB 8.1

时间:2025/8/13 1:01:42来源:https://blog.csdn.net/xcg340123/article/details/141131561 浏览次数:0次

目录

1、环境检查

1.1、检测及关闭系统 swap

1.2、设置TiDB节点的临时空间

 1.3、安装NTP服务

1.3.1、更新apt源

1.3.2、安装NTP

1.3.3、将本机作为NTP服务器

1.3.4、客户端连接

1.4、配置SSH互信及sudo免密码

2、离线安装

2.1、下载离线安装包

2.2、解压安装

2.3、最小配置模板topology.yaml

2.4、风险检查

 2.5、风险自动修复

2.6、部署TiDB集群

3、集群管理

3.1、检查集群

3.2、启动集群

3.2、验证启动

3.4、访问平台


1、环境检查

1.1、检测及关闭系统 swap

TiDB 运行需要有足够的内存。如果想保持性能稳定,则建议永久关闭系统 swap,但可能在内存偏小时触发 OOM 问题;如果想避免此类 OOM 问题,则可只将 swap 优先级调低,但不做永久关闭。

  • 开启并使用 swap 可能会引入性能抖动问题,对于低延迟、稳定性要求高的数据库服务,建议永久关闭操作系统层 swap。要永久关闭 swap,可使用以下方法:

    • 在操作系统初始化阶段,不单独划分 swap 分区盘。

    • 如果在操作系统初始化阶段,已经单独划分了 swap 分区盘,并且启用了 swap,则使用以下命令进行关闭:

echo "vm.swappiness = 0">> /etc/sysctl.conf 
swapoff -a 
sysctl -p
  • 如果主机内存偏小,关闭系统 swap 可能会更容易触发 OOM 问题,可参考以如下方法将 swap 优先级调低,但不做永久关闭:

    echo "vm.swappiness = 0">> /etc/sysctl.conf 
    sysctl -p

1.2、设置TiDB节点的临时空间

TiDB 的部分操作需要向服务器写入临时文件,因此需要确保运行 TiDB 的操作系统用户具有足够的权限对目标目录进行读写。如果 TiDB 实例不是以 root 权限启动,则需要检查目录权限并进行正确设置。

  • TiDB 临时工作区

    哈希表构建、排序等内存消耗较大的操作可能会向磁盘写入临时数据,用来减少内存消耗,提升稳定性。写入的磁盘位置由配置项 tmp-storage-path 定义。在默认设置下,确保运行 TiDB 的用户对操作系统临时文件夹(通常为 /tmp)有读写权限。

  • Fast Online DDL 工作区

    当变量 tidb_ddl_enable_fast_reorg 被设置为 ON(v6.5.0 及以上版本中默认值为 ON)时,会激活 Fast Online DDL,这时部分 DDL 要对临时文件进行读写。临时文件位置由配置 temp-dir 定义,需要确保运行 TiDB 的用户对操作系统中该目录有读写权限。以默认目录 /tmp/tidb 为例:

    注意

    如果业务中可能存在针对大对象的 DDL 操作,推荐为 temp-dir 配置独立文件系统及更大的临时空间。

    sudo mkdir /tmp/tidb

    如果目录 /tmp/tidb 已经存在,需确保有写入权限。

    sudo chmod -R 777 /tmp/tidb

    注意

    如果目录不存在,TiDB 在启动时会自动创建该目录。如果目录创建失败,或者 TiDB 对该目录没有读写权限,Fast Online DDL 在运行时可能产生不可预知的问题。

 1.3、安装NTP服务

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

可以通过互联网中的 pool.ntp.org 授时服务来保证节点的时间同步,

也可以使用离线环境自己搭建的 NTP 服务来解决授时

基于ubuntu20.04.1

1.3.1、更新apt源

cd /etc/apt 替换 sources.list 内容

deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse # deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse # deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

arm UOS 源

deb http://mirrors.aliyun.com/ubuntu-ports/ xenial main deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial main deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates main deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates main deb http://mirrors.aliyun.com/ubuntu-ports/ xenial universe deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial universe deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates universe deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-security main deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-security main deb http://mirrors.aliyun.com/ubuntu-ports/ xenial-security universe deb-src http://mirrors.aliyun.com/ubuntu-ports/ xenial-security universe

更新软件列表

sudo apt-get update

1.3.2、安装NTP

# 检查是否存在 ntp服务
sudo systemctl status ntp.service# 不存在,则安装apt install ntp# 启动服务service ntp start

1.3.3、将本机作为NTP服务器

# 在服务端修改ntp配置开放客户端所在的网段
vim /etc/ntp.conf
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for helpdriftfile /var/lib/ntp/ntp.drift# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable# 主要配置   # 允许访问的网络端
restrict 192.168.20.113 mask 255.255.255.0 nomodify notraprestrict 192.168.0.0
restrict 127.0.0.1
restrict ::1
service ntp restart
service ntp status# 开放端口(一般情况下可省略)
# sudo ufw allow 123/udp

1.3.4、客户端连接

如果出现如下问题:no server suitable for synchronization found

等待五分钟

# 安装ntpdate
ntpdate -d 192.168.20.113

1.4、配置SSH互信及sudo免密码

对于有需求,通过手动配置中控机至目标节点互信的场景,可参考本段。

  1. 以 root 用户依次登录到部署目标机器创建 tidb 用户并设置登录密码。

    useradd tidb &&  passwd tidb

    # 将结尾处的 sh 修改为 bash
    vim  /etc/passwd

  2. 执行以下命令,将 tidb ALL=(ALL) NOPASSWD: ALL 添加到文件末尾,即配置好 sudo 免密码。

    visudo

    tidb ALL=(ALL) NOPASSWD: ALL

  3. 以 tidb 用户登录到中控机,执行以下命令。将 10.0.1.1 替换成你的部署目标机器 IP,按提示输入部署目标机器 tidb 用户密码,执行成功后即创建好 SSH 互信,其他机器同理。新建的 tidb 用户下没有 .ssh 目录,需要执行生成 rsa 密钥的命令来生成 .ssh 目录。如果要在中控机上部署 TiDB 组件,需要为中控机和中控机自身配置互信。

    # 登录tidb账号
    su tidb# 为了后续方便,直接按enter键
    ssh-keygen -t rsa #
    ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.1.1
  4. 以 tidb 用户登录中控机,通过 ssh 的方式登录目标机器 IP。如果不需要输入密码并登录成功,即表示 SSH 互信配置成功。

    ssh 10.0.1.1

    [tidb@10.0.1.1 ~]$

  5. 以 tidb 用户登录到部署目标机器后,执行以下命令,不需要输入密码并切换到 root 用户,表示 tidb 用户 sudo 免密码配置成功。

    sudo -su root

    [root@10.0.1.1 tidb]#

2、离线安装

2.1、下载离线安装包

下载地址从文末 参考文档上,两个包都需要下载

 

2.2、解压安装

注意后续操作全部以 tidb 账号登录

mkdir -p /home/tidb
chown -R tidb:tidb /home/tidb
su tidb
cd /home/tidbtar -zxf tidb-community-toolkit-v8.1.0-linux-amd64.tar.gz
tar -zxf tidb-community-server-v8.1.0-linux-amd64.tar.gzsh tidb-community-server-v8.1.0-linux-amd64/local_install.sh
source /home/tidb/.bashrc

2.3、最小配置模板topology.yaml

其中

1)pd_server:元数据管理、集群一致性管理

2)tidb_server:对外提供连接访问,对内与tikv_server通信

3)tikv_server:实际存储数据节点

# # Global variables are applied to all deployments and used as the default value of
# # the deployments if a specific deployment value is missing.
global:user: "tidb"ssh_port: 22deploy_dir: "/tidb-deploy"data_dir: "/tidb-data"# # Monitored variables are applied to all the machines.
monitored:node_exporter_port: 9100blackbox_exporter_port: 9115# deploy_dir: "/tidb-deploy/monitored-9100"# data_dir: "/tidb-data/monitored-9100"# log_dir: "/tidb-deploy/monitored-9100/log"# # Server configs are used to specify the runtime configuration of TiDB components.
# # All configuration items can be found in TiDB docs:
# # - TiDB: https://docs.pingcap.com/zh/tidb/stable/tidb-configuration-file
# # - TiKV: https://docs.pingcap.com/zh/tidb/stable/tikv-configuration-file
# # - PD: https://docs.pingcap.com/zh/tidb/stable/pd-configuration-file
# # All configuration items use points to represent the hierarchy, e.g:
# #   readpool.storage.use-unified-pool
# #
# # You can overwrite this configuration via the instance-level `config` field.server_configs:tidb:log.slow-threshold: 300binlog.enable: falsebinlog.ignore-error: falsetikv:# server.grpc-concurrency: 4# raftstore.apply-pool-size: 2# raftstore.store-pool-size: 2# rocksdb.max-sub-compactions: 1# storage.block-cache.capacity: "16GB"# readpool.unified.max-thread-count: 12readpool.storage.use-unified-pool: falsereadpool.coprocessor.use-unified-pool: truepd:replication.location-labels: ["zone","dc","host"]schedule.leader-schedule-limit: 4schedule.region-schedule-limit: 2048schedule.replica-schedule-limit: 64pd_servers:- host: 192.168.20.113# ssh_port: 22# name: "pd-1"# client_port: 2379# peer_port: 2380# deploy_dir: "/tidb-deploy/pd-2379"# data_dir: "/tidb-data/pd-2379"# log_dir: "/tidb-deploy/pd-2379/log"# numa_node: "0,1"# # The following configs are used to overwrite the `server_configs.pd` values.# config:#   schedule.max-merge-region-size: 20#   schedule.max-merge-region-keys: 200000 tidb_servers:- host: 192.168.20.113# ssh_port: 22# port: 4000# status_port: 10080# deploy_dir: "/tidb-deploy/tidb-4000"# log_dir: "/tidb-deploy/tidb-4000/log"# numa_node: "0,1"# # The following configs are used to overwrite the `server_configs.tidb` values.# config:#   log.slow-query-file: tidb-slow-overwrited.logtikv_servers:- host: 192.168.20.113# ssh_port: 22port: 20161status_port: 20181deploy_dir: "/tidb-deploy/tikv-20161"data_dir: "/tidb-data/tikv-20161"log_dir: "/tidb-deploy/tikv-20161/log"# numa_node: "0,1"# # The following configs are used to overwrite the `server_configs.tikv` values.config:#   server.grpc-concurrency: 4server.labels: { zone: "zone1", dc: "dc1", host: "host1" }- host: 192.168.20.113port: 20162status_port: 20182deploy_dir: "/tidb-deploy/tikv-20162"data_dir: "/tidb-data/tikv-20162"log_dir: "/tidb-deploy/tikv-20162/log"config:server.labels: { zone: "zone1", dc: "dc1", host: "host2" }- host: 192.168.20.113port: 20163status_port: 20183deploy_dir: "/tidb-deploy/tikv-20163"data_dir: "/tidb-data/tikv-20163"log_dir: "/tidb-deploy/tikv-20163/log"config:server.labels: { zone: "zone1", dc: "dc1", host: "host3" }monitoring_servers:- host: 192.168.20.113# ssh_port: 22# port: 9090# deploy_dir: "/tidb-deploy/prometheus-8249"# data_dir: "/tidb-data/prometheus-8249"# log_dir: "/tidb-deploy/prometheus-8249/log"grafana_servers:- host: 192.168.20.113# port: 3000# deploy_dir: /tidb-deploy/grafana-3000alertmanager_servers:- host: 192.168.20.113# ssh_port: 22# web_port: 9093# cluster_port: 9094# deploy_dir: "/tidb-deploy/alertmanager-9093"# data_dir: "/tidb-data/alertmanager-9093"# log_dir: "/tidb-deploy/alertmanager-9093/log"

也可以使用命令生成模板配置文件

#执行如下命令,生成集群初始化配置文件:
tiup cluster template > topology.yaml#混合部署场景:单台机器部署多个实例
tiup cluster template --full > topology.yaml#跨机房部署场景:跨机房部署 TiDB 集群
tiup cluster template --multi-dc > topology.yaml

2.4、风险检查

tiup cluster check ./topology.yaml --user tidb [-p] [-i /home/root/.ssh/gcp_rsa]

 2.5、风险自动修复

tiup cluster check ./topology.yaml --apply --user tidb [-p] [-i /home/root/.ssh/gcp_rsa]

 1)numactl not usable, bash: numectl: command not found

apt-get install numactl

2)CPU frequency governor is ondemand, should use performance

问题:CPU 频率调节器设置为 ondemand,应设置为 performance 以优化性能。

解决方法

sudo apt-get install cpufrequtils 
sudo cpufreq-set -r -g performance检查设置是否生效:cpufreq-info |grep ondemand

2.6、部署TiDB集群

tiup cluster deploy tidb-test v8.1.0 ./topology.yaml --user tidb [-p] [-i /home/root/.ssh/gcp_rsa]
  • tidb-test 为部署的集群名称。
  • v8.1.0 为部署的集群版本,可以通过执行 tiup list tidb 来查看 TiUP 支持的最新可用版本。
  • 初始化配置文件为 topology.yaml
  • --user root 表示通过 root 用户登录到目标主机完成集群部署,该用户需要有 ssh 到目标机器的权限,并且在目标机器有 sudo 权限。也可以用其他有 ssh 和 sudo 权限的用户完成部署。
  • [-i] 及 [-p] 为可选项,如果已经配置免密登录目标机,则不需填写。否则选择其一即可,[-i] 为可登录到目标机的 root 用户(或 --user 指定的其他用户)的私钥,也可使用 [-p] 交互式输入该用户的密码。

3、集群管理

3.1、检查集群

#查看 TiUP 管理的集群情况
tiup cluster list#TiUP 支持管理多个 TiDB 集群,该命令会输出当前通过 TiUP cluster 管理的所有集群信息,包括集群名称、部署用户、版本、密钥信息等。#执行如下命令检查 tidb-test 集群情况:
tiup cluster display tidb-test

3.2、启动集群

注意启动密码【请保管好密码】,如果不加 --init ,则无密码root访问集群

忘记密码修改

# 安全启动 
tiup cluster start tidb-test --init

只有第一次执行需要添加 --init 参数,后续则移除,否则再次启动报错

3.2、验证启动

tiup cluster display tidb-test

3.4、访问平台

输入root账号,及密码登录

http://192.168.20.113:2379/dashboard/

参考文档

TiDB 环境与系统配置检查 | PingCAP 文档中心

TiDB 社区版 离线安装包下载 | PingCAP

TiKV 配置文件描述 | PingCAP 文档中心

关键字:【TiDB】08-离线部署TiDB 8.1

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: