前提
二进制日志配置 my.cnf
server-id = 1
log-bin = mysql-bin
binlog_format = ROW
sync_binlog = 1
思路
复制日志文件,并备份到其他目录
启动一个新的容器来分析线上的binlog
根据数据特征找到对应的binlog文件
再再文件中提取对应的数据
yaml
apiVersion: v1
kind: Pod
metadata:
name: mysqlbinlog
namespace: default
labels:
app: mysqlbinlog
spec:
# ✅ 指定运行在 master 节点(修改为实际 hostname)
nodeSelector:
kubernetes.io/hostname: master1 # 替换为实际 master 节点名
# ✅ 允许调度到 control-plane / master 节点
tolerations:
- key: "node-role.kubernetes.io/control-plane"
operator: "Exists"
effect: "NoSchedule"
- key: "node-role.kubernetes.io/master"
operator: "Exists"
effect: "NoSchedule"
containers:
- name: mysqlbinlog
image: mysql:5.7.36
command: ["/bin/bash"]
args: ["-c", "tail -f /dev/null"] # 保持容器运行
tty: true
stdin: true
volumeMounts:
- name: binlog-dir
mountPath: /mnt/binlog
volumes:
- name: binlog-dir
hostPath:
path: /root/data/nfs/mysql-pvc-cc841a70-a293-4a6c-950a-f21f69c4ed7c/. # 备份下来的binlog目录
type: Directory
restartPolicy: Never
容器内操作
kubectl exec -it mysqlbinlog -- bash # 进入到容器
bash
# 找到有数据的文件
cd /mnt/binlog
for i in $(seq -f "%06g" 1 17); do
echo "=== Checking mysql-bin.${i} ==="
mysqlbinlog --base64-output=decode-rows -v ./mysql-bin.${i} \
| grep -n "数据特征ID" && echo "Found in ${i}"
done
# 输出
=== Checking mysql-bin.000001 ===
=== Checking mysql-bin.000002 ===
=== Checking mysql-bin.000003 ===
=== Checking mysql-bin.000004 ===
=== Checking mysql-bin.000005 ===
=== Checking mysql-bin.000006 ===
=== Checking mysql-bin.000007 ===
=== Checking mysql-bin.000008 ===
=== Checking mysql-bin.000009 ===
=== Checking mysql-bin.000010 ===
=== Checking mysql-bin.000011 ===
=== Checking mysql-bin.000012 ===
=== Checking mysql-bin.000013 ===
=== Checking mysql-bin.000014 ===
=== Checking mysql-bin.000015 ===
=== Checking mysql-bin.000016 ===
=== Checking mysql-bin.000017 ===
# 在文件里面定位需要的信息
mysqlbinlog --base64-output=decode-rows -v ./mysql-bin.000018 | grep -n -C10 "数据特征ID"
# 这边可以根据数据列数调整 -C10 这个是前后10行,可以根据表的字段多少调整。
总结
镜像一定要用老版本,新版本里面没有工具,会导致不能用。最终还是通过归档日志还原出数据了。搞数据还是一定要做好备份。备份重于一切。连存储都有可能出现故障,异地存储也很重要。数据安全同样重要备份文件做好加密,防止数据泄露。