SQL Server database file Recovery Technology

Source: Internet
Author: User
There are two ways to back up SQL Server databases: one is to back up database files using backup database, and the other is to directly copy the database files MDF and log files LDF. The following describes the backup and recovery of the latter. This document assumes that you are familiar with SQL Server Enterprise Manager and SQL Server quwey analyser)
1 Normal backup and recovery methods
Normally, we need to back up a database. First, we need to disconnect the database from the running data server, or stop the entire database server, and then copy the file.
Command for removing the database: sp_detach_db Database Name
Command for connecting to the database: sp_attach_db or sp_attach_single_file_db
S_attach_db [ @ Dbname = ] 'Dbname ′, [ @ Filename1 = ] 'Filename_n ′ [ , 16 ]
Sp_attach_single_file_db [ @ Dbname = ] 'Dbname ′, [ @ Physname = ] 'Physical_name ′
This method can be used to correctly restore the database files of SQL sever7.0 and SQL Server 2000. The key point is that the MDF and LDF files must be backed up during backup. The MDF file is the database data file, LDF is a database log file.
Example:
Assume that the database is test, the data file is test_data.mdf, and the log file is test_log.ldf. Next we will discuss how to back up and restore the database.
Remove Database: sp_detach_db ' Test '
Connect to the database: sp_attach_db ' Test ' , ' C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ test_data.mdf ' , ' C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ test_log.ldf '
Sp_attach_single_file_db ' Test ' , ' C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ test_data.mdf '
2 Only MDF file Restoration Technology
For various reasons, if we only backed up the MDF file at that time, it would be very troublesome to restore it.
If your MDF file is generated by the current database, you may be lucky to use sp_attach_db or sp_attach_single_file_db to restore the database, but the following prompt will appear:
Device activation error. Physical file name ' C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ test_log.ldf ' It may be incorrect.
You have created a project named ' C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ test_log.ldf ' .
However, if your database files are copied from other computers, the above method may not work. You may get an error message similar to the following.
Server: Message 1813 , Level 16 , Status 2 , Line 1
Failed to open new database ' Test ' . Create   Database Will be terminated.
Device activation error. Physical file name ' D: \ test_log.ldf ' It may be incorrect.
What should we do? Don't worry. The following is an example of the restoration method.
A. We use the default method to create a database for recovery (such as test ). It can be created in SQL Server Enterprise Manager.
B. Stop the database server.
C. Delete the log file test_log.ldf of the database you just generated, and overwrite the generated database data file test_data.mdf with the MDF file to be restored.
D. Start the database server. In this case, the database test status is "Suspect ". At this time, you cannot perform any operations on this database.
E. Set the database to allow direct operation of system tables. In SQL Server Enterprise Manager, right-click the database server and select "properties". On the "Server Settings" Page, select "allow direct modification to system directory. You can also use the following statement.
Use Master
Go
Sp_configure ' Allow updates ' , 1
Go  
Reconfigure   With Override
Go
F. Set test to emergency repair mode.
Update Sysdatabases Set Status =- 32768   Where Dbid = Db_id ( ' Test ' )
At this time, you can see in SQL Server Enterprise Manager that the database is in "read-only, suspicious, offline, and emergency mode" to see the tables in the database, but only the system tables
G. Execute the true recovery operation below to recreate the database log file.
DBCC Rebuild_log ( ' Test ' , ' C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ test_log.ldf ' )
If the following prompt is displayed during execution:
Server: Message 5030 , Level 16 , Status 1 , Line 1
The database cannot be locked for this operation.
DBCC The execution is complete. If DBCC An error message is output. Contact the system administrator.
This indicates that other programs are using this database. If you opened the system table of the test database using SQL Server Enterprise Manager in step f, you can exit SQL Server Enterprise Manager.
The prompt for correct execution should be similar:
Warning: Database ' Test ' The log has been rebuilt. Transaction consistency is lost. Should Run DBCC Checkdb to verify physical consistency. The database must be reset and redundant log files may need to be deleted.
DBCC The execution is complete. If DBCC An error message is output. Contact the system administrator.
In SQL Server Enterprise Manager, you can see that the database status is "only for DBO ". Now you can access the user tables in the database.
H. Verify Database Consistency (omitted)
DBCC Checkdb ( ' Test ' )
The general execution result is as follows:
Checkdb found 0 Allocation errors and 0 Consistency errors (in the database ' Test ' ).
DBCC The execution is complete. If DBCC An error message is output. Contact the system administrator.
I. Set the database to normal
Sp_dboption ' Test ' , ' DBO use only ' , ' False '
If no error occurs, Congratulations. Now we can use the recovered database normally.
J. In the last step, we need to restore the "allow direct modification to the system directory" set in Step E. It is dangerous to directly operate system tables. Of course, we can recover the data in SQL Server Enterprise Manager or use the following statement.
Sp_configure ' Allow updates ' , 0
Go  
Reconfigure   With Override
Go
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.