日期:2014-05-16  浏览次数:20349 次

数据库修复系列Part4:重建数据库日志文件

我们可能会遇到日志文件损坏或者被不小心删除的问题,导致数据库无法访问。在没有备份的情况下,可以用重建日志文件恢复数据库,但是可能会有数据丢失。 另外本篇文件还通过举例证明了日志文件的重要性(许多人说日志文件是可以删除的,这个是完全错误的)。

 

1.  select * from Test(有两条记录1,2)

查询结果:

idTest

-----------

1

2

 

2.  更新一条记录(将1更改为3),但是不提交事务。

 

begin tran

update Test set idTest=3where idTest = 1

 

checkpoint

 

select * from Test

查询结果:

idTest

-----------

2

3

 

(2 row(s) affected)

 

3.       关闭SQL Server服务器

 

4.       删掉ldf文件重启Service,并且访问数据库会得到下面的错误:

 

Msg 945, Level 14, State 2,Line 2

Database 'FNDBLogTest' cannotbe opened due to inaccessible files or insufficient memory or disk space.  See the SQL Server errorlog for details.

 

5.       重建日志文件:

 

ALTER DATABASE FNDBLogTestREBUILDLOGON (NAME=FNDBLogTest_Log,

FILENAME='D:\ProgramFiles\Microsoft SQL Server\MSSQL10_50.R2\MSSQL\DATA\FNDBLogTest_Log.ldf')

 

Warning: The log for database'FNDBLogTest' has been rebuilt. Transactional consistency has been lost.The RESTORE chain was broken, and the server no longer has context on theprevious log files, so you will need to know what they were. You should run DBCCCHECKDB to validate physical consistency. The database has been put in dbo-onlymode. When you are ready to make the database available for use, you will needto reset database options and delete any extra log files.

 

6.       查询test表数据:

 

select * from Test

 

idTest

-----------

2

3

 

(2 row(s) affected)

 

 

注意:这里我们看到数据是