Skip to content

Deploying a TiDB cluster (PD + TiKV + TiDB) on AlmaLinux 9 with tiup

Stand up a self-hosted TiDB cluster on AlmaLinux 9 — PD for placement, TiKV for storage, TiDB for SQL — fully orchestrated by tiup with topology, certs, and snapshot plan.

You are migrating off a sharded MySQL fleet because the application’s data model finally outgrew the manual sharding you wrote three years ago. You evaluated TiDB, ran the tiup playground on a laptop, and the SQL surface matched. Then you went to deploy it on real infrastructure and found that “a TiDB cluster” is actually three distinct services on three different machine classes — PD for placement, TiKV for the Raft-replicated storage layer, TiDB for the stateless SQL gateway — and the question of how to size them, where to put the disks, and how to keep tiup-central in version control turned a two-hour task into a two-week project. This guide is what we wish we had on day one.

What’s happening

A production TiDB cluster on AlmaLinux 9 is not one binary. It is three distinct roles, each on its own VM, each with its own lifecycle. PD (Placement Driver) is the brain — a Raft group of typically three nodes that owns cluster metadata, region scheduling, and timestamp oracle. PD is small, CPU-light, and absolutely critical: lose quorum and the cluster stops accepting writes. TiKV is the storage layer — every row is stored as a key-value pair in a Raft group across (typically) three TiKV nodes; ranges of keys are sharded into regions, and PD moves regions around for load balance. TiKV is the heavy node: NVMe, lots of RAM, and the bulk of your spend. TiDB is the SQL gateway — stateless, horizontally scalable, sits between your application and TiKV; it parses SQL and turns it into key-value operations. It is small and you can have as many as you want.

The orchestrator is tiup, a Go binary that runs from a control host (laptop or a dedicated CI runner) and SSHes into the cluster nodes to drive every operation: deploy, start, stop, scale-in, scale-out, upgrade. Your source of truth is a single YAML topology file under version control. Drift away from “everything goes through tiup” and the cluster gets unmanageable in three weeks.

This installer prepares three “host classes” of AlmaLinux 9 VMs — PD, TiKV, TiDB — then runs tiup from a control host to deploy the cluster. The minimum viable production topology is 3 PD + 3 TiKV + 2 TiDB on six or seven VMs; this guide writes the topology for that and the host-prep script that runs on each VM before tiup ever touches it.

How to verify

Cluster status from the control host:

tiup cluster display tidb-prod
# Cluster name:       tidb-prod
# Cluster version:    v7.5.4
# Deploy user:        tidb
# SSH type:           builtin
# ID                Role          Host         Ports        OS/Arch        Status   Data Dir            Deploy Dir
# 10.30.0.11:2379   pd            10.30.0.11   2379/2380   linux/x86_64    Up|L|UI  /tidb-data/pd-2379  /tidb-deploy/pd-2379
# 10.30.0.12:2379   pd            10.30.0.12   2379/2380   linux/x86_64    Up       ...
# 10.30.0.13:2379   pd            10.30.0.13   2379/2380   linux/x86_64    Up       ...
# 10.30.0.21:20160  tikv          10.30.0.21   20160/20180 linux/x86_64    Up       ...
# 10.30.0.22:20160  tikv          10.30.0.22   20160/20180 linux/x86_64    Up       ...
# 10.30.0.23:20160  tikv          10.30.0.23   20160/20180 linux/x86_64    Up       ...
# 10.30.0.31:4000   tidb          10.30.0.31   4000/10080  linux/x86_64    Up       -                   ...
# 10.30.0.32:4000   tidb          10.30.0.32   4000/10080  linux/x86_64    Up       -                   ...

SQL surface from any TiDB node:

mysql -h 10.30.0.31 -P 4000 -u root -e "SELECT TIDB_VERSION();"

PD region health:

tiup ctl:v7.5.4 pd -u http://10.30.0.11:2379 region check miss-peer
tiup ctl:v7.5.4 pd -u http://10.30.0.11:2379 region check pending-peer

Either should return empty. If you see regions stuck with miss-peer for more than a few minutes, a TiKV node didn’t finish bootstrapping; check that node before traffic.

NTP drift check (TiDB is unforgiving here — PD uses a TSO and clock skew breaks transactions):

chronyc tracking | grep "System time"
# System time     : 0.000034210 seconds slow of NTP time

Anything over a few hundred milliseconds is a fault.

The fix

You run the host-prep script on every node (PD, TiKV, TiDB) first. Then from the control host, install tiup, write the topology file, and run tiup cluster deploy.

Save the host-prep script as /root/setup-tidb-host.sh on each cluster node:

#!/usr/bin/env bash
set -euo pipefail

### AlmaLinux 9 TiDB host preparation
### Run on every node (PD, TiKV, TiDB) before tiup deploy
### Does NOT install TiDB binaries — tiup handles that from the control host

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

# === EDIT PER NODE ===
NODE_ROLE="tikv"                     # pd / tikv / tidb
NODE_IP="10.30.0.21"                 # this node's private IP
TIDB_DEPLOY_USER="tidb"
TIDB_DEPLOY_USER_AUTHORIZED_KEY="ssh-ed25519 AAAA... ops-control"
DATA_DEVICE="/dev/nvme1n1"           # NVMe for TiKV; can be /dev/sdb on PD/TiDB
DATA_MOUNT="/tidb-data"
DEPLOY_MOUNT="/tidb-deploy"
# =====================

echo
echo "========================================"
echo "TiDB host prep: ${NODE_ROLE} (${NODE_IP})"
echo "========================================"
echo

echo "==> Updating base OS"
dnf -y update
dnf -y install epel-release
dnf -y install vim curl wget chrony fail2ban dnf-automatic \
  policycoreutils-python-utils lsof tcpdump bind-utils tar gzip \
  iproute net-tools jq socat numactl xfsprogs lvm2

systemctl enable --now chronyd

# TiDB is sensitive to clock drift -> tighten chrony to local NTP sources
cat > /etc/chrony.conf <<'CHRONY'
pool 2.pool.ntp.org iburst maxsources 4
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
CHRONY
systemctl restart chronyd

echo "==> SELinux enforcing"
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
setenforce 1 || true

echo "==> Disabling transparent hugepages (TiKV recommendation)"
cat > /etc/systemd/system/disable-thp.service <<'THP'
[Unit]
Description=Disable Transparent Huge Pages
After=sysinit.target local-fs.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled; echo never > /sys/kernel/mm/transparent_hugepage/defrag"
RemainAfterExit=yes

[Install]
WantedBy=basic.target
THP
systemctl daemon-reload
systemctl enable --now disable-thp.service

echo "==> Kernel/sysctl tuning for TiDB"
cat > /etc/sysctl.d/99-tidb.conf <<'SYSCTL'
vm.swappiness = 0
vm.max_map_count = 262144
fs.file-max = 2097152
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 10
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.ip_local_port_range = 10240 65535
net.core.netdev_max_backlog = 65535
kernel.numa_balancing = 0
kernel.dmesg_restrict = 1
SYSCTL
sysctl --system

cat > /etc/security/limits.d/99-tidb.conf <<EOF
${TIDB_DEPLOY_USER} soft nofile 1000000
${TIDB_DEPLOY_USER} hard nofile 1000000
${TIDB_DEPLOY_USER} soft stack 32768
${TIDB_DEPLOY_USER} hard stack 32768
${TIDB_DEPLOY_USER} soft nproc 65535
${TIDB_DEPLOY_USER} hard nproc 65535
EOF

echo "==> Disabling swap permanently"
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab

echo "==> Hardening SSH"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart sshd

echo "==> fail2ban + automatic security updates"
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban

cat > /etc/dnf/automatic.conf <<'DNFAUTO'
[commands]
upgrade_type = security
random_sleep = 900
download_updates = yes
apply_updates = yes
[emitters]
emit_via = stdio
DNFAUTO
systemctl enable --now dnf-automatic.timer

echo "==> Creating tidb deploy user"
id "${TIDB_DEPLOY_USER}" >/dev/null 2>&1 || useradd -m -s /bin/bash "${TIDB_DEPLOY_USER}"
mkdir -p "/home/${TIDB_DEPLOY_USER}/.ssh"
echo "${TIDB_DEPLOY_USER_AUTHORIZED_KEY}" > "/home/${TIDB_DEPLOY_USER}/.ssh/authorized_keys"
chmod 700 "/home/${TIDB_DEPLOY_USER}/.ssh"
chmod 600 "/home/${TIDB_DEPLOY_USER}/.ssh/authorized_keys"
chown -R "${TIDB_DEPLOY_USER}:${TIDB_DEPLOY_USER}" "/home/${TIDB_DEPLOY_USER}/.ssh"

cat > /etc/sudoers.d/tidb <<EOF
${TIDB_DEPLOY_USER} ALL=(ALL) NOPASSWD: ALL
Defaults:${TIDB_DEPLOY_USER} !requiretty
EOF
chmod 440 /etc/sudoers.d/tidb

echo "==> Preparing data + deploy mounts"
mkdir -p "${DATA_MOUNT}" "${DEPLOY_MOUNT}"
if [[ -n "${DATA_DEVICE}" && -b "${DATA_DEVICE}" ]]; then
  if ! blkid "${DATA_DEVICE}" >/dev/null 2>&1; then
    echo "Formatting ${DATA_DEVICE} as ext4"
    mkfs.ext4 -L tidb-data "${DATA_DEVICE}"
  fi
  if ! grep -q "${DATA_MOUNT}" /etc/fstab; then
    echo "LABEL=tidb-data ${DATA_MOUNT} ext4 defaults,noatime,nodelalloc 0 0" >> /etc/fstab
  fi
  mount -a
fi
chown -R "${TIDB_DEPLOY_USER}:${TIDB_DEPLOY_USER}" "${DATA_MOUNT}" "${DEPLOY_MOUNT}"

echo "==> Opening firewall ports (PD: 2379/2380, TiKV: 20160/20180, TiDB: 4000/10080)"
if systemctl is-active --quiet firewalld; then
  case "${NODE_ROLE}" in
    pd)
      firewall-cmd --permanent --add-port=2379/tcp
      firewall-cmd --permanent --add-port=2380/tcp
      ;;
    tikv)
      firewall-cmd --permanent --add-port=20160/tcp
      firewall-cmd --permanent --add-port=20180/tcp
      ;;
    tidb)
      firewall-cmd --permanent --add-port=4000/tcp
      firewall-cmd --permanent --add-port=10080/tcp
      ;;
  esac
  firewall-cmd --reload
fi

echo
echo "==> Host prep complete on ${NODE_ROLE} ${NODE_IP}"
echo "    tidb deploy user: ${TIDB_DEPLOY_USER}"
echo "    data mount:       ${DATA_MOUNT}"
echo "    deploy mount:     ${DEPLOY_MOUNT}"
echo
ss -lnt | grep -E ':(2379|2380|20160|20180|4000|10080)\b' || true

Run it on each of the seven (or six) cluster nodes — set NODE_ROLE and NODE_IP per host. The script does not install TiDB binaries; tiup will push them in the next step.

On a separate control host (your laptop or a dedicated CI runner), install tiup:

curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
source ~/.bash_profile
tiup cluster --version

Write the topology file as ~/tidb-prod-topology.yaml:

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

monitored:
  node_exporter_port: 9100
  blackbox_exporter_port: 9115

server_configs:
  tidb:
    log.slow-threshold: 300
    new_collations_enabled_on_first_bootstrap: true
  tikv:
    readpool.unified.max-thread-count: 8
    storage.scheduler-worker-pool-size: 4
    raftstore.apply-pool-size: 4
    raftstore.store-pool-size: 4
  pd:
    schedule.leader-schedule-limit: 4
    schedule.region-schedule-limit: 2048
    schedule.replica-schedule-limit: 64

pd_servers:
  - host: 10.30.0.11
  - host: 10.30.0.12
  - host: 10.30.0.13

tikv_servers:
  - host: 10.30.0.21
  - host: 10.30.0.22
  - host: 10.30.0.23

tidb_servers:
  - host: 10.30.0.31
  - host: 10.30.0.32

monitoring_servers:
  - host: 10.30.0.41

grafana_servers:
  - host: 10.30.0.41

alertmanager_servers:
  - host: 10.30.0.41

Deploy and start the cluster from the control host:

tiup cluster check ~/tidb-prod-topology.yaml --user tidb -i ~/.ssh/id_ed25519_ops --apply
tiup cluster deploy tidb-prod v7.5.4 ~/tidb-prod-topology.yaml \
  --user tidb -i ~/.ssh/id_ed25519_ops
tiup cluster start tidb-prod --init
# Initial root password is printed once — save it to the secret store immediately.
tiup cluster display tidb-prod

Connect from any client that can reach a TiDB node on port 4000:

mysql -h 10.30.0.31 -P 4000 -u root -p

Create your app database and a user (TiDB speaks the MySQL protocol):

CREATE DATABASE app;
CREATE USER 'app'@'%' IDENTIFIED BY 'CHANGE_ME';
GRANT ALL PRIVILEGES ON app.* TO 'app'@'%';
FLUSH PRIVILEGES;

For backups, install BR (Backup & Restore) on the control host and schedule a full backup to object storage weekly with incremental log backups continuously enabled — that combination gives you point-in-time restore. Pair that with volume snapshots of the /tidb-data mount on each TiKV node every six hours; the volume snapshot is your fast single-node recovery and the BR archive is your cross-fleet rebuild path. Test the restore quarterly; an untested restore is a hypothesis.

Run TiDB on Stack Harbor’s managed dedicated cloud with NVMe-backed TiKV nodes, NTP discipline, and monitoring already in place under environment management — or start a scoping call if you’d like us to plan the topology with you.