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行,可以根据表的字段多少调整。

总结

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

相关推荐
Wzx19801231 分钟前
游标分页 + 数据删除:游标被删的完整解决方案
mysql
丿小王同学37 分钟前
快速集群安装mysql
数据库·mysql
北风toto1 小时前
通过Entity 创建数据库中的表,目前只支持mysql,A.CTable使用mybatis/mybatis-plus自动创建表
数据库·mysql·mybatis
念恒123063 小时前
MySQL表的约束(上)
数据库·mysql
海市公约3 小时前
MySQL核心概念及SQL语句与数据类型详解
mysql·sql语句·数据类型·运算符·ddl·dml·数据库入门
x***r1513 小时前
heidisql数据库客户端使用步骤详解(附HeidiSQL连接MySQL与SQL执行教程)
数据库·sql·mysql
万里侯4 小时前
Kubernetes多租户管理:实现资源隔离与安全的完整指南
微服务·容器·k8s
Nontee4 小时前
如何用 MySQL 实现一个可重入的锁?
数据库·mysql
坚定学代码5 小时前
如何在c++中使用MySQL
开发语言·c++·mysql
万里侯5 小时前
云原生数据库管理:在Kubernetes上运行数据库的完整指南
微服务·容器·k8s