首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

SQL Server 資料檔 (MDF) 頁面檔頭結構深度剖析:第 0 頁 96 Bytes 手工解析與災難修復應用

     編輯:關於SqlServer

SQL Server 資料庫檔案 (MDF) 以 8KB 頁面為基本儲存單位,第 0 頁 (Page 0) 為檔案首頁,前 96 Bytes 為關鍵的 頁面檔頭,記錄檔案層級的元資料。本文實測 SQL Server 2005 環境,使用 DBCC PAGE 輸出原始十六進制資料,逐 Byte 手工解析前 64 Bytes 結構,並探討檔頭損壞時的災難修復思路。

一、 取得頁面原始資料指令

DBCC TRACEON(3604); -- 將 DBCC 輸出導向用戶端
GO
DBCC PAGE(master, 1, 0, 2); -- 資料庫名/ID, 檔案ID, 頁碼, 輸出選項(2=完整輸出)

二、 頁面檔頭關鍵欄位輸出範例


PAGE HEADER:
Page @0x03FA0000
m_pageId = (1:0) m_headerVersion = 1 m_type = 15
m_typeFlagBits = 0x0 m_level = 0 m_flagBits = 0x8
m_objId (AllocUnitId.idObj) = 99 m_indexId (AllocUnitId.idInd) = 0 Metadata: AllocUnitId = 6488064
Metadata: PartitionId = 0 Metadata: IndexId = 0 Metadata: ObjectId = 99
m_prevPage = (0:0) m_nextPage = (0:0) pminlen = 0
m_slotCnt = 1 m_freeCnt = 7937 m_freeData = 3059
m_reservedCnt = 0 m_lsn = (149:448:1) m_xactReserved = 0
m_xdesId = (0:0) m_ghostRecCnt = 0 m_tornBits = -1073741694

三、 記憶體原始傾印 (前 96 Bytes / 0x00-0x5F)


Memory Dump @0x62FEC000
62FEC000: 010f0000 08000000 00000000 00000000  ...
62FEC010: 00000000 00000100 63000000 011ff30b  ...
62FEC020: 00000000 01000000 95000000 c0010000  ...
62FEC030: 01000000 00000000 00000000 820000c0  ...
62FEC040: 00000000 00000000 00000000 00000000  ... (0x40-0x5F 全為 0)
62FEC050: 00000000 00000000 00000000 00000000  ...

四、 前 64 Bytes (0x00-0x3F) 逐 Byte 結構對照表

每行 4 Bytes (Little-Endian),輔以 Windows 計算機與 C# BitConverter.GetBytes 驗證:

位移Bytes對應欄位說明
0x00-0x0301 0F 00 00m_headerVersion(1), m_type(15), m_typeFlagBits(0), m_level(0)頁面類型 15 = 檔案首頁 (File Header)
0x04-0x0708 00 00 00m_flagBits0x8 = 有最小長度資訊
0x08-0x0B00 00 00 00m_objId / AllocUnitId.idObj (低 4 Bytes)Object ID = 99 (sys.sysfiles)
0x0C-0x0F00 00 01 00m_indexId / AllocUnitId.idInd (低 2 Bytes) + 未用Index ID = 0 (堆疊)
0x10-0x1363 00 00 00m_prevPage (FileID:PageID)(0:0) 無上一頁
0x14-0x1701 1F F3 0Bm_nextPage (FileID:PageID)PageID = 0x0BF31F01 (下一頁)
0x18-0x1B00 00 00 00pminlen最小記錄長度
0x1C-0x1F01 00 00 00m_slotCnt槽位數 = 1
0x20-0x2395 00 00 00m_freeCnt可用空間 = 7937 Bytes
0x24-0x27C0 01 00 00m_freeData自由資料起始位移 = 3059
0x28-0x2B01 00 00 00m_pageId (FileID:PageID 低 4 Bytes)PageID = 1 (第 1 頁,檔案首頁本身)
0x2C-0x2F00 00 00 00m_reservedCnt保留計數
0x30-0x3300 00 00 82m_lsn (Log Sequence Number 部分)LSN = (149:448:1)
0x34-0x3700 00 C0 00m_xactReserved + m_xdesId 部分交易保留 / 描述符 ID
0x38-0x3B00 00 00 00m_ghostRecCnt鬼魂記錄計數 = 0
0x3C-0x3F82 00 00 C0m_tornBits (頁面撕裂偵測)Checksum / Torn Page Detection 位元

五、 0x40-0x5F (64-95 Bytes) 觀察

實測環境下此 32 Bytes 全為 0。僅當 m_pageId 檔案 ID ≠ 1 時會寫入資料,具體含義尚待深入研究 (可能與檔案群組擴展相關)。

六、 災難修復應用場景

  • 檔頭損壞症狀:資料庫標示為 SUSPECT、無法掛載、DBCC CHECKDB 報告頁面 0 錯誤。
  • 修復思路:使用十六進制編輯器 (如 WinHex, HxD) 開啟 MDF,依上表結構手工修正關鍵欄位 (m_pageId, m_type, m_objId, m_lsn, m_tornBits),嘗試讓資料庫進入 EMERGENCY 模式匯出資料。
  • 風險警示:極高風險操作,**嚴禁在生產環境直接嘗試**,必須在離線複本上驗證。專業資料修復服務商擁有完整頁面重組工具鏈,建議優先尋求專業協助。

七、 參考資源

  • Paul Randal (SQLskills) 部落格:Inside the Storage Engine 系列
  • Microsoft 官方文件:DBCC PAGE 參數說明
  • 《SQL Server 2005 Internals》Chapter 2: Pages and Extents
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved