MySQL数据文件头解析与Oracle BBED工具对比
============================
一、MySQL数据文件简介
1.1 数据文件类型
| 文件类型 | 描述 | 位置 |
| ibdata1 | 系统表空间 | /var/lib/mysql/ |
| .ibd | 独立表空间文件 | /var/lib/mysql/database/ |
| ib_logfile* | Redo日志文件 | /var/lib/mysql/ |
| #innodb_redo | 新版Redo日志 | /var/lib/mysql/ |
| .frm (8.0已弃用) | 表结构定义 | /var/lib/mysql/database/ |
1.2 InnoDB关键参数
datadir = /var/lib/mysql/
innodb_data_file_path = ibdata1:12M:autoextend
innodb_file_per_table = ON
innodb_page_size = 16384 (16KB)
二、InnoDB页结构详解
2.1 页结构概览
┌─────────────────────────────────────────────────────────────────────┐
│ InnoDB页结构 (16KB) │
├─────────────────────────────────────────────────────────────────────┤
│ File Header (38字节) │
│ Page Header (56字节) │
│ Infimum + Supremum Records (26字节) │
│ User Records (变长) │
│ Free Space (变长) │
│ Page Directory (变长) │
│ File Trailer (8字节) │
└─────────────────────────────────────────────────────────────────────┘
2.2 文件头部结构(38字节)
| 偏移 | 字段名称 | 大小 | 说明 |
| 0-3 | FIL_PAGE_SPACE | 4字节 | 表空间ID |
| 4-7 | FIL_PAGE_OFFSET | 4字节 | 页号(从0开始) |
| 8-11 | FIL_PAGE_PREV | 4字节 | 前一页号 |
| 12-15 | FIL_PAGE_NEXT | 4字节 | 后一页号 |
| 16-23 | FIL_PAGE_LSN | 8字节 | 最后修改LSN |
| 24-25 | FIL_PAGE_TYPE | 2字节 | 页类型 |
| 26-33 | FIL_PAGE_FILE_FLUSH_LSN | 8字节 | 文件刷新LSN |
| 34-37 | FIL_PAGE_ARCH_LOG_NO | 4字节 | 归档日志号 |
三、实验:查看MySQL数据文件头
3.1 创建测试表
CREATE TABLE test_tb (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
value INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
INSERT INTO test_tb (name, value) VALUES ('Record1', 100), ('Record2', 200);
3.2 使用hexdump查看文件头
sudo hexdump -C /var/lib/mysql/test_db/test_tb.ibd | head -n 32
3.3 解析文件头字段
# 提取表空间ID(前4字节)
sudo od -A x -t x1 -N 4 /var/lib/mysql/test_db/test_tb.ibd
# 提取页类型(偏移24,2字节)
sudo od -A x -j 24 -t x1 -N 2 /var/lib/mysql/test_db/test_tb.ibd
四、MySQL数据文件查看工具
4.1 使用information_schema视图
SELECT * FROM information_schema.INNODB_TABLESPACES WHERE NAME LIKE '%test_tb%';
SELECT * FROM information_schema.INNODB_COLUMNS WHERE TABLE_ID = (SELECT TABLE_ID FROM INNODB_TABLES WHERE NAME = 'test_db/test_tb');
4.2 使用hexdump/od命令
hexdump -C file.ibd | head -n 50
hexdump -C file.ibd -s 49152 -n 1024
od -A x -j 0 -t x4 -N 4 file.ibd # 表空间ID
od -A x -j 4 -t x4 -N 4 file.ibd # 页号
od -A x -j 24 -t x2 -N 2 file.ibd # 页类型
五、与Oracle BBED对比
5.1 功能对比
| 功能 | Oracle BBED | MySQL工具 |
| 查看数据块 | SET BLOCK | hexdump -s offset |
| 查看块头 | MAP | hexdump -C |
| 校验和验证 | SUM APPLY | File Trailer |
| 块转储 | DUMP | hexdump/od |
| 查找数据 | FIND | grep/hexdump |
5.2 数据块结构对比
| 比较项 | Oracle | MySQL InnoDB |
| 块大小 | 8KB(可配置) | 16KB(可配置) |
| 块头大小 | 100字节 | 38字节(File Header) |
| 行格式 | Variable Length | Dynamic/Compact |
| 事务槽 | ITL | MVCC在Undo |
5.3 命令对比
| 操作 | Oracle BBED | MySQL |
| 查看块 | SET BLOCK 10 | hexdump -s 163840 -n 16384 |
| 查看偏移 | SET OFFSET 100 | hexdump -s 100 -n 16 |
| 转储块 | DUMP | hexdump -C |
| 查找值 | FIND /x 1234 | grep -boa -P '\x12\x34' file.ibd |