Home Database Mysql Tutorial 数据库恢复:对pageheader的恢复

数据库恢复:对pageheader的恢复

Jun 07, 2016 pm 03:28 PM
recover database forum

前两天在论坛,看到有个网友提问,说是: 格式化磁盘前把.mdf和.ldf拷贝出来了,然后格式化完成后在拷贝回去(拷贝前后都没有错误提示,文件大小也一样),在企业管理器中附加数据库出错,提示错误823,附加数据库失败。从网上搜了搜方案:重建同名数据库之

前两天在论坛,看到有个网友提问,说是:

格式化磁盘前把.mdf和.ldf拷贝出来了,然后格式化完成后在拷贝回去(拷贝前后都没有错误提示,文件大小也一样),在企业管理器中附加数据库出错,提示“错误823”,附加数据库失败。从网上搜了搜方案:重建同名数据库之类的做法都试过了,都不能解决问题。请问版主或各位高手,有什么解决方案吗?
注:拷贝之前数据库使用正常,并且以前都是此种方法附加的,这次不知如何出现这样的问题了。

帮他尝试恢复:

1、他已经按照网上的方法,新建了一个同名的数据库。

2、停止服务,然后把原始的文件拷贝到目录中。

3、启动服务,发现数据库处于质疑状态。

4、运行如下命令,都报错:打不开数据库

dbcc checkdb(xxx);

dbcc checkdb(xxx,repair_allow_data_loss);
Copy after login

5、运行如下命令成功, 设置数据库为紧急模式

 Use Master  
 GO  
 
 sp_configure 'allow updates', 1  
 reconfigure with override  
 GO  
 
 UPDATE sysdatabases SET status = 32768 where name = 'xxx'  
 GO  
Copy after login
6、再次运行命令,报错:数据库必须处于单用户模式
dbcc checkdb(xxx,repair_allow_data_loss);
Copy after login
Copy after login
7、如果如下命令,设置数据库为单用户模式:
alter database test set single_user
Copy after login
8、再次运行命令,还是报错:数据库必须处于单用户模式
dbcc checkdb(xxx,repair_allow_data_loss);
Copy after login
Copy after login
在这里就觉得很奇怪,明明已经设置为单用户模式了,怎么还报这个错误呢?

在网上找了一下,发现有很多人都有这个问题,但是没有解决办法。

9、尝试运行如下命令,但类似这样的报错:

服务器: 消息 8909,级别 16,状态 1,行 1
表错误: 对象 ID 0,索引 ID 0,页 ID 0。
 服务器: 消息 8966,级别 16,状态 1,行 1
未能读取并闩锁页 (1:123456)(用闩锁类型 SH)。sysobjects 失败。
Copy after login
在网上找到一篇文章:

sql server 系统表损坏修复方法 http://wenku.baidu.com/view/901cd511f18583d04964592e

这个文章中提到的解决方法就是,新建一个数据库,通过dts,把原来数据库的数据,导出到新的数据库中,但前提是其中的表都能访问,也就是能够select * from 表,否则就没用了。

那么有没有什么好的办法,能修复呢?

在网上找了一下,发现有专业的数据恢复公司,能恢复这种错误,从文章中提到的信息来看,应该通过直接构造损坏的页面来修复的。

由于这种情况比较难重现,所以这里模拟当某个数据页的page header损坏时,如何手动修复这个数据页,其实就是一个复制粘帖的过程。

1、先创建一个数据库,创建表,插入数据:

create database w
go

use w
go


if OBJECT_ID('test') is not null
   drop table test

select * into test
from sys.objects
go


insert into test
select * 
from test
Copy after login
2、分离数据库w,把数据文件和日志文件,直接复制到一个备份目录。

3、本来通过dbcc writelog来修改数据,但是应该是没有修改好。

dbcc writepage的语法为:

dbcc writepage 
({ dbid,'dbname' }, fileid, pageid, offset, length, data)

--生成96个字节的数据
select '0x'+replicate('00',96)

dbcc writepage('w',1,310,0,96,
0x000000000000000000000000000000000000000
00000000000000000000000000000000000000000
00000000000000000000000000000000000000000
00000000000000000000000000000000000000000
000000000000000000000000000000)
/*
消息 8939,级别 16,状态 5,第 1 行
表错误: 对象 ID 0,索引 ID -1,分区 ID 0,分配单元 ID 0 (类型为 Unknown),页 (0:0)。测试(m_headerVersion == HEADER_7_0)失败。值为 0 和 1。
消息 8939,级别 16,状态 6,第 1 行
表错误: 对象 ID 0,索引 ID -1,分区 ID 0,分配单元 ID 0 (类型为 Unknown),页 (0:0)。测试((m_type >= DATA_PAGE && m_type <= UNDOFILE_HEADER_PAGE) || (m_type == UNKNOWN_PAGE && level == BASIC_HEADER))失败。值为 0 和 0。
消息 8939,级别 16,状态 7,第 1 行
表错误: 对象 ID 0,索引 ID -1,分区 ID 0,分配单元 ID 0 (类型为 Unknown),页 (0:0)。测试(m_freeData >= PageHeaderOverhead () && m_freeData <= (UINT)PAGESIZE - m_slotCnt * sizeof (Slot))失败。值为 0 和 8192。
DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
*/
Copy after login
所以,改用二进制编辑工具,输入:0x0026C000,定位到这个物理偏移,然后修改了数据库w中的fileID为1,pageID为310的,前96个字节全为00,这个正好是page header部分:
select 8*1024*310 as &#39;310页在文件中的物理偏移&#39;,
       CAST(8*1024*310 as varbinary)  &#39;转化为16进制&#39;
/*
310页在文件中的物理偏移	转化为16进制
2539520	0x0026C000
*/
Copy after login

\


\

4、重新附加这个数据库

dbcc checkdb(w)
/*
消息 8909,级别 16,状态 1,第 1 行
表错误: 对象 ID 0,索引 ID -1,分区 ID 0,分配单元 ID 0 (类型为 Unknown),页 ID (1:310) 在其页头中包含错误的页 ID。页头中的 PageId = (0:0)。
CHECKDB 发现有 0 个分配错误和 1 个一致性错误与任何单个的对象都没有关联。

消息 8928,级别 16,状态 1,第 1 行
对象 ID 981578535,索引 ID 0,分区 ID 72057594038910976,分配单元 ID 72057594042580992 (类型为 In-row data): 无法处理页 (1:310)。有关详细信息,请参阅其他错误消息。

CHECKDB 在数据库 &#39;w&#39; 中发现 0 个分配错误和 2 个一致性错误。
对于由 DBCC CHECKDB (w)发现的错误,repair_allow_data_loss 是最低的修复级别。
DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。
*/
Copy after login
5、把数据库分离,从备份的文件中,复制page header到这个文件

\

6、再次附加数据库,就可以查询了。

不过,使用repair_allow_data_loss也可以恢复。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to restore deleted comments on Xiaohongshu? Any tips for deleted comments? How to restore deleted comments on Xiaohongshu? Any tips for deleted comments? Mar 27, 2024 am 11:56 AM

Xiaohongshu is a popular social e-commerce platform where users can share shopping experiences, life details, etc. During use, some users may experience their comments being deleted. So, how to restore deleted comments on Xiaohongshu? 1. How to restore deleted comments on Xiaohongshu? If it is found that a comment has been deleted by mistake, users can choose to wait for the official Xiaohongshu team to restore it. In this case, it’s best to be patient and wait as the official team may automatically process and resume comments after a while. If you find that a comment has been deleted, consider republishing similar content. But when reposting, please make sure the content complies with Xiaohongshu’s community guidelines to avoid being removed again. 3. Contact Xiaohongshu customer service: If you think your comment has been mistakenly

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

See all articles