MySQL K8S日志分析与数据还原

前提

二进制日志配置 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行,可以根据表的字段多少调整。

总结

镜像一定要用老版本,新版本里面没有工具,会导致不能用。最终还是通过归档日志还原出数据了。搞数据还是一定要做好备份。备份重于一切。连存储都有可能出现故障,异地存储也很重要。数据安全同样重要备份文件做好加密,防止数据泄露。

相关推荐
谅望者20 小时前
在 macOS 上使用 Homebrew 安装 MySQL 8.0 完整指南
数据库·sql·mysql
程序员卷卷狗21 小时前
MySQL 页结构与数据存储原理全解析》
数据库·mysql
hweiyu0021 小时前
MySQL 从入门到精通(视频教程)
数据库·mysql
小跌—21 小时前
MySQL:数据库基础
数据库·mysql
赵文宇1 天前
站在MySQL肩膀上快速入门PostgreSQL,开源社区最喜爱的关系数据库
mysql·postgresql
Gold Steps.1 天前
数据库正常运行但是端口变成了0?
数据库·mysql
愤怒的苹果ext1 天前
MySQL JSON查询与索引
mysql·json·虚拟列·多值索引
翻斗花园牛图图-1 天前
MySQL——库的操作
数据库·mysql
-指短琴长-1 天前
MySQL快速入门——内置函数
android·数据库·mysql
nvd111 天前
GKE 部署 - 从`kubectl` 到 Helm Chart 的演进
k8s