事情发生的原因是 tidb 的测试环境基础设施希望将 pd 数据盘调整为机械盘来节省资源,调整后 pd 因为写 wal 太慢无法正常启动,我想临时将 pd 数据目录通过符号链接挂到系统所在的固态盘看能否启动,结果操作过程中不慎删除 pd 所有数据,当时没发现,启动集群时 kv 节点报 cluster id 不一致才发现这个问题。由于是测试环境,就丢给 ai 看能不能自己帮我恢复,结果还真成了,特于此记录过程,感谢 tidb 功能文档完善,感谢 ai 大大,以下均为 ai 排查和操作记录。
误删 PD 数据目录导致 cluster ID 变更、TiKV 拒绝启动
时间:根因动作 2026-10-12 约 17:5x;故障暴露 2026-10-14 09:46:55;恢复完成 2026-10-14 10:24
集群:datacenter-test,TiDB / PD / TiKV / TiFlash v6.5.12 Community(tiup cluster 部署)
实例:PD10.120.3.76:2379;TiKV10.120.3.71/.74/.77:20160;TiFlash10.120.3.75:9000
现象:TiKV 启动即 FATAL ——
failed to bootstrap node id: "[src/server/node.rs:307]: cluster ID mismatch, local 7218861505342273096 != remote 7695718441998114221, you are trying to connect to another cluster, please reconnect to the correct PD"
结论:操作者把 PD 数据目录/tidb-data/pd-2379移到/opt/pd-2379,并在原路径留软链接;随后用带尾斜杠的rm pd-2379//rm -r pd-2379//rm -rf pd-2379/清理——GNUrm把尾斜杠解释为“跟随软链接”,递归删掉了真实目录/opt/pd-2379内的全部内容。原始 PD 数据(etcdmember/、region-meta/)被清空,PD 以空目录重新 bootstrap 出新集群(cluster id7695718441998114221),而三个 TiKV 的 StoreIdent 仍是原集群7218861505342273096,两边不一致。
当前状态:已用官方pd-recover(全量重建 PD)恢复。cluster id 回到7218861505342273096,3 个 TiKV + 1 TiFlash 全部 Up,24,184 个 Region 由 TiKV 心跳自动重建,SQL 可查(6,424 张表)。TiKV 数据未丢失;PD 层动态配置(TiFlash 副本规则等)不可恢复,见 §7。
1. 结论
- 报错语义是“连错集群”,不是“集群坏数据”。 TiKV 读本地
StoreIdent.cluster_id,与 PD 下发的 cluster_id 比对,不一致即拒绝启动。本集群两侧分别是7218861505342273096(TiKV,原集群)与7695718441998114221(PD,新集群)。 - 直接原因是 PD 数据目录被清空,PD 重新 bootstrap 成新集群。 PD(内嵌 etcd)的
DataDir同时承载集群身份与元数据:member/是 etcd 的 WAL+snapshot,region-meta/是 Region 元数据。这些被删空后,PD 找不到/pd/cluster_id,于是生成一个新的 cluster id 写回 etcd。 - 删除动作由
rm -rf <软链接>/的尾斜杠造成。 操作者把/tidb-data/pd-2379移走后用软链接指回/opt/pd-2379;随后执行的rm -r pd-2379//rm -rf pd-2379/因为带尾斜杠,被rm当作目录跟随,穿透软链接删除了目标目录的内容(软链接本身还在)。现场已复现该行为,见 §3.2。 - 两轮操作里只有第一轮是破坏性的。
~/.bash_history中第一轮(2026-10-12 傍晚)含带尾斜杠的rm -r/-rf pd-2379/;第二轮(2026-10-13 09:29)是rm pd-2379(无尾斜杠)+mv回迁,只删软链、不伤数据。 - TiKV 侧数据完好。 三个 TiKV 数据目录分别为 225G / 229G / 226G,StoreIdent 与 Region 数据都在。恢复后 Region 全部由 TiKV 心跳自动上报重建。
- 本次没有可用的 PD 备份兜底,只能走
pd-recover全量重建路径。这是本次事件从“可恢复”变成“必须重建元数据”的关键放大因素。
2. 时间线
| 时间(+08:00) | 事件 | 证据 |
|---|---|---|
| 2026-10-12 16:37 / 17:27 | TiKV /data1/tikv-20160/raft-engine、import/ 目录 mtime,lightning 导入刚结束 |
ls -la /data1/tikv-20160/(.74) |
| 2026-10-12 约 17:5x | 第一轮:tiup cluster stop -R pd → mv pd-2379/ /opt/ → ln -s /opt/pd-2379/ ./ → 启动 PD → 通读 /opt/pd-2379/{hot-region,region-meta} → rm -r pd-2379/、rm -rf pd-2379/(均带尾斜杠) → mv /opt/pd-2379/ ./ → 启动 PD |
~/.bash_history |
| 2026-10-12 17:58:01 | 新 PD 数据目录 region-meta LevelDB 首次 open,version@stat F·[] S·0B[](空库);同日 18:24:42 再次 open |
/tidb-data/pd-2379.newcluster-7695718441998114221/region-meta/LOG |
| 2026-10-13 08:30 前后 | 主机重启(sudo reboot now),排查 tiup 日志 |
~/.bash_history、~/.tiup/logs/tiup-cluster-debug-2026-10-13-08-30-53.log |
| 2026-10-13 09:16 | cat /dev/null > /tidb-deploy/pd-2379/log/pd.log 截断日志;当日 09:16:09 起 PD 反复启停、多次 ErrCancelStartEtcd |
pd.log 行 1 起 |
| 2026-10-13 09:29 | 第二轮:mv pd-2379/ /opt/ → ln -s /opt/pd-2379/ ./ → rm pd-2379(无尾斜杠) → sudo mv /opt/pd-2379/ ./。此轮无破坏性 |
~/.bash_history;/opt、/tidb-data mtime = 10-13 09:29 |
| 2026-10-13 09:42:29.484 | init cluster id 7695718441998114221 —— 新集群 ID 首次落库 |
pd.log(server.go:404) |
| 2026-10-13 10:31:39.810 | 再次 init cluster id 7695718441998114221 |
pd.log |
| 2026-10-14 09:42:57.894 | 再次 init cluster id 7695718441998114221 |
pd.log |
| 2026-10-14 09:46:55.981 | TiKV 报错暴露:cluster ID mismatch, local 7218861505342273096 != remote 7695718441998114221 |
TiKV FATAL(server.rs:995) |
| 2026-10-14 09:58 | 备份故障 PD 目录 cp -a /tidb-data/pd-2379 → /tidb-data/pd-2379.bak-20261014095823 |
本次处置 |
| 2026-10-14 10:13:55 | 挪走故障目录,用空目录启动 PD(生成随机 id 7696341041504297871,未 bootstrap) |
本次处置 |
| 2026-10-14 10:14:54 | pd-recover 执行成功,写入 cluster-id=7218861505342273096、alloc-id=100000000000;raft_bootstrap_time 记为 10:14:54.379953765 |
PD /pd/api/v1/cluster/status |
| 2026-10-14 10:15–10:18 | 重启 PD;PD 上报 cluster id 已回到 7218861505342273096 |
PD /pd/api/v1/cluster |
| 2026-10-14 10:18–10:24 | tiup cluster start 拉起 TiKV / TiDB / TiFlash / 监控 |
tiup 输出 |
| 2026-10-14 10:24:33 | TiDB coprocessor 正常命中 TiKV region(region_id:137385838、78291890) |
/tidb-deploy/tidb-4000/log/tidb.log |
| 2026-10-14 10:35 | 复查:4 store Up,miss-peer/down-peer/pending-peer 均为 0,SQL 可查 6,424 张表 |
PD API + dumpling |
3. 根因
3.1 触发动作(~/.bash_history,第一轮)
tiup cluster stop datacenter-test -R pd # cwd = /tidb-data
mv pd-2379/ /opt/ # 原始 PD 数据整目录搬到 /opt
ln -s /opt/pd-2379/ ./ # /tidb-data/pd-2379 -> /opt/pd-2379
tiup cluster start datacenter-test -R pd # PD 仍读到原始数据,服务正常
... # 通读 /opt/pd-2379/{hot-region,region-meta}
tiup cluster stop datacenter-test -R pd
rm pd-2379/ # 带尾斜杠
rm -rf pd-2379/ # 带尾斜杠 ← 破坏发生在此
rm pd-2379 # 此时才删掉软链接本身
mv /opt/pd-2379/ ./ # 无 sudo,/opt 为 root 所有,失败
sudo mv /opt/pd-2379/ ./ # 移回一个已被清空的目录
tiup cluster start datacenter-test -R pd # PD 以空目录启动 → 新集群
3.2 rm -rf <软链接>/ 会删掉目标目录的内容(现场复现)
$ rm -rf /tmp/rmtest; mkdir -p /tmp/rmtest/real && touch /tmp/rmtest/real/x
$ ln -s /tmp/rmtest/real /tmp/rmtest/link
$ ls -la /tmp/rmtest/ # link -> /tmp/rmtest/real, real/ 中有 x
$ rm -rf /tmp/rmtest/link/ ; echo exit=$?
exit=0
$ ls -la /tmp/rmtest/real/ # 内容 x 已被删除,real/ 目录本身保留
$ ls -la /tmp/rmtest/ # 软链接 link 仍然存在
结论:尾斜杠使 rm 把参数当“目录”处理并跟随软链接进入目标;-r/-rf 于是递归删除目标内容,且退出码为 0(静默成功)。软链接自身不会被删——这正好解释了后续 rm pd-2379(无斜杠)仍然执行成功。
这条行为同时解释了第一轮里
rm pd-2379/(无-r)未报错:对软链接目录,rm跟随后再判断为目录,同样作用于内容。
3.3 为什么 PD 变成“另一个集群”
PD 的 DataDir 被清空后:
- etcd 在空
member/上以单节点重新 bootstrap(pd.log中反复出现added member/set initial cluster version)。 - PD 启动时读取
/pd/cluster_id,不存在则用member.MustNewMember(cfg).ID()生成新值并写回 —— 即日志[server.go:404] ["init cluster id"] [cluster-id=7695718441998114221]。 - 同时
region-meta/(Region 元数据)从空开始,dashboard.sqlite.db也被重建,说明整个 PD 元数据面是全新的。
佐证:/tidb-data/pd-2379/region-meta/LOG 的第一条记录是 2026-10-12 17:58:01,且 version@stat F·[] S·0B[];而 CURRENT.bak 指向已不存在的 MANIFEST-000007,也说明该目录是新生的空库。
3.4 为什么 TiKV 直接 FATAL(保护性设计)
TiKV 启动流程 Node::try_bootstrap_store → Node::check_store:
// tikv/src/server/node.rs
fn check_store(&self, engines: &Engines<EK, ER>) -> Result<u64> {
let res = engines.kv.get_msg::<StoreIdent>(keys::STORE_IDENT_KEY)?;
if res.is_none() { return Ok(INVALID_ID); }
let ident = res.unwrap();
if ident.get_cluster_id() != self.cluster_id {
return Err(box_err!(
"cluster ID mismatch, local {} != remote {}, \
you are trying to connect to another cluster, please reconnect to the correct PD",
ident.get_cluster_id(), self.cluster_id));
}
...
}
- 本地源码:
tikv/src/server/node.rs:299(check_store),错误串在:308;调用方try_bootstrap_store在:193,Node::start中put_store在:246。 - 报障机器上的构建对应行号不同(错误串落在
node.rs:307,FATAL 打印在server.rs:995),语义完全相同。 - 设计意图:阻止 TiKV 把 A 集群的数据挂到 B 集群的 PD 上,避免跨集群误调度造成数据损坏。这是正确行为,不是 bug。
4. 关键证据
| 证据 | 命令 / 位置 | 观察值 |
|---|---|---|
| 原集群 ID(历史) | grep "init cluster id" pd-2026-*.log |
2026-06-10 / 07-08 / 07-16 均为 7218861505342273096 |
| 新集群 ID | grep "init cluster id" pd.log |
2026-10-13 09:42:29.484、10-13 10:31:39.810、10-14 09:42:57.894 均为 7695718441998114221 |
| TiKV 侧 cluster ID | TiKV 启动 FATAL 文本 | local 7218861505342273096(= StoreIdent) |
| PD 数据目录“新生且为空” | /tidb-data/pd-2379/region-meta/LOG |
首条 2026-10-12 17:58:01;version@stat F·[] S·0B[] |
| 搬迁动作 | ~/.bash_history |
两轮 mv pd-2379/ /opt/ + ln -s /opt/pd-2379/ ./;第一轮含带尾斜杠 rm -r/-rf pd-2379/ |
/opt 曾被改动 |
ls -ld /opt |
mtime 2026-10-13 09:29(第二轮),现为空目录 |
alloc-id 下界 |
grep "idAllocator allocates a new id" pd-2026-08-26T14-24-21.830.log |
最后一条 2026-08-14 16:03:21 new-end=216361000 |
| 恢复前 PD 日志异常 | pd.log |
每次启动都 restarted local member → switched to configuration voters=() → added member,即反复重建单节点 etcd |
5. 恢复过程(已执行,2026-10-14)
采用 PD 官方 Method 2:全量重建 PD 集群(“all PD data is lost, but the data of other components still exists”)。
# 0. 确认全集群已停(PD/TiKV/TiDB/TiFlash 均无进程)
ps -ef | grep -E "pd-server|tikv-server|tidb-server|tiflash" | grep -v grep
# 1. 先备份现场,再挪走故障目录(不 rm,保留回溯能力)
cp -a /tidb-data/pd-2379 /tidb-data/pd-2379.bak-$(date +%Y%m%d%H%M%S)
mv /tidb-data/pd-2379 /tidb-data/pd-2379.newcluster-7695718441998114221
# 2. 从本地离线镜像安装 pd-recover(集群镜像指向本地目录,无需外网)
~/.tiup/bin/tiup install pd-recover:v6.5.12
# 3. 用空数据目录启动 PD,使其可提供服务(此时 cluster id 是新的随机值,未 bootstrap)
~/.tiup/bin/tiup cluster start datacenter-test -R pd
curl -sS http://10.120.3.76:2379/pd/api/v1/cluster # id=7696341041504297871, not bootstrapped
# 4. 写入原 cluster id 与 alloc id(pd-recover 直连 etcd,非 PD API)
~/.tiup/bin/tiup pd-recover \
-endpoints http://10.120.3.76:2379 \
-cluster-id 7218861505342273096 \
-alloc-id 100000000000
# → recover success! please restart the PD cluster
# 5. 重启 PD(cluster id 生效),再拉起整个集群
~/.tiup/bin/tiup cluster restart datacenter-test -R pd # 注意:会交互确认,需输入 y
~/.tiup/bin/tiup cluster start datacenter-test
alloc-id 取值依据:原始 PD 日志中分配器 2026-08-14 已到 new-end=216,361,000;恢复后实际观测到的 Region id 量级为 7.8e7 ~ 1.4e8。取 1e11 比历史最大值高约 3 个数量级,远大于任何可能已分配的 ID,且远小于 int64 上限。该值必须严格大于原集群曾分配过的最大 ID,否则会与既有 Region/Peer/Store ID 冲突。
6. 恢复验证
| 检查项 | 命令 | 结果 |
|---|---|---|
| PD cluster ID | curl /pd/api/v1/cluster |
7218861505342273096 |
| Store 列表 | curl /pd/api/v1/stores |
4 store 全 Up(TiKV 1 / 203377 / 203378,TiFlash 77161001) |
| Region 重建 | 同上 region_count |
每个 TiKV store 24,184 |
| Region 健康 | /pd/api/v1/regions/check/{miss-peer,down-peer,pending-peer} |
三者 count 均为 0 |
| TiDB 可用性 | dumpling --sql 查询 |
5.7.25-TiDB-v6.5.12,information_schema.tables = 6,424 |
| TiDB↔TiKV 读路径 | tail /tidb-deploy/tidb-4000/log/tidb.log |
coprocessor 正常命中 TiKV region |
| 组件状态 | tiup cluster display datacenter-test |
9 个节点全部 Up |
| 开机自启 | systemctl is-enabled ... |
先前被 disable 的 pd-2379.service 已重新 enable |
7. 影响面与不可恢复项
TiKV 数据本体无丢失:Region / Peer / Store 元数据由 TiKV 心跳自动上报重建(官方原文:“Other information like region, store, peer will be reported from TiKV automatically after PD cluster is recovered”)。三份数据目录未被任何写操作触碰。
不可恢复(PD 侧元数据本身已丢):
- TiFlash 副本规则(placement rule)全部丢失。PD 现在只有默认规则
count=3,TiFlash store 的region_count=0,information_schema.tiflash_replica将为空。若此前有表建过 TiFlash 副本,需重新执行ALTER TABLE <db>.<tbl> SET TIFLASH REPLICA <n>;并等待回填。 - 自定义 scheduler / placement rule /
location-labels回到默认;balance-hot-region-scheduler等如有手工暂停/参数,需重新确认。 - TiDB 相关 PD 元数据(DDL owner 键、
/pd/{cluster_id}/timestamp等)重新初始化;TSO 从当前墙钟重新起算(向前跳,不影响正确性)。 - GC safe point 重新初始化。恢复后观测
service_gc_safe_points中已有gc_worker条目、dumpling_*临时条目,说明 TiDB 的 GC worker 已重新接管。
配置一致性核对:schedule.max-merge-region-size / max-merge-region-keys 恢复后为 54 / 540000;比对 2026-07-28 的原始 PD 日志(同为 54 / 540000)后确认无漂移。注意 topology.yaml 中写的 20 / 200000 从未生效,属历史遗留差异。
8. 证据边界
pd.log在 2026-10-13 09:16 被cat /dev/null截断,2026-10-12 17:58 当次的init cluster id日志已不可见;新集群 ID 由 10-13 之后的日志与region-meta/LOG的创建时间交叉印证。[推断]rm -rf pd-2379/的当场输出(是否打印 warning)无法还原;本报告依据的是 GNU coreutils 的通用行为 + 现场复现,而非当时的终端记录。[推断,机制已复现]~/.bash_history无时间戳,两轮操作的具体时刻由文件 mtime(/opt、/tidb-data= 10-13 09:29)、region-meta/LOG(10-12 17:58)与后续reboot/debug 日志(10-13 08:30)反推。- 第一轮操作附近的行号与命令内容取自
~/.bash_history,其中mv的 cwd 由后续cd /tidb-data/、ll序列与/opt/pd-2379实际被创建所反推。[推断] - 原始 PD 的
alloc-id真实值未能取得(只有 2026-08-14 的日志下界 216,361,000),因此按官方建议取了高若干数量级的1e11。[已按保守值处理] - 该集群无 PD 快照/备份,是否存在外部 PD 备份未在主机上发现:
sudo find / -type d -path "*member/wal"、find / -name "*pd-2379*"均只命中故障目录与其备份。[主机范围内已排除]
9. 后续跟进
- 重建 TiFlash 副本规则(本次唯一需要人工补的业务侧配置):核对恢复前哪些库表使用了 TiFlash,重放
ALTER TABLE ... SET TIFLASH REPLICA。 - 核对 PD 侧其它非默认配置:
pd-ctl config show、pd-ctl scheduler show、pd-ctl config placement-rules show与历史台账比对。 - 为 PD 建立可用的备份/快照手段(本集群目前没有)。PD 元数据丢失只能走
pd-recover重建,若再次发生同类事故,仍会丢失 TiFlash 副本规则等 PD 层配置。 - 观察若干天确认 Region 健康(
miss-peer/down-peer/pending-peer恒为 0)后再清理遗留目录:/tidb-data/pd-2379.bak-20261014095823(故障 PD 目录副本)/tidb-data/pd-2379.newcluster-7695718441998114221(故障 PD 目录本体)
两者各约 62M。
- 归档本次临时安装的
pd-recover:v6.5.12使用说明,纳入仓库顶层 sops/pd-recover.md。 - 操作规范:禁止对软链接使用带尾斜杠的
rm -r/-rf;确需删除目录内容时先readlink -f解析真实路径。建议纳入团队rm使用约定。
10. 证据来源
- OS:
~/.bash_history、ls -la、ls -ld、stat、readlink -f、df -h、systemctl is-enabled、ps -ef - PD:
pd.log、pd-2026-*.log(历史init cluster id)、/tidb-data/pd-2379*/region-meta/LOG、member/wal文件名、PD REST API(/pd/api/v1/cluster、/stores、/regions/check/*、/config/rules、/cluster/status) - TiKV:启动 FATAL 文本、
StoreIdent(cluster id 体现于报错)、/data1/tikv-20160/目录清单与raft-engine/文件名(Region id 量级)、tikv.log - TiDB:
/tidb-deploy/tidb-4000/log/tidb.log(coprocessor 命中 Region)、dumpling 查询结果 - tiup:
tiup cluster list/display/start/restart、~/.tiup/logs/tiup-cluster-debug-*.log、~/.tiup/storage/cluster/clusters/datacenter-test/meta.yaml - 源码:
tikv/src/server/node.rs(check_store/try_bootstrap_store)、pd/tools/pd-recover/main.go
11. 参考资料
- PD Recover User Guide(PD 全量重建,Method 2)
- pd-recover 命令说明:明确 “Other information like region, store, peer will be reported from TiKV automatically after PD cluster is recovered.”
- tikv/pd Wiki · Metadata Management:Region 元数据由 TiKV 心跳上报维护
- Online Unsafe Recovery:
pd-recover后 store id 丢失时的--auto-detect模式 - GNU coreutils
rm(1):--preserve-root、符号链接与尾斜杠的处理语义