Home Database Mysql Tutorial 轻松掌握SQL Server数据库文件恢复技术

轻松掌握SQL Server数据库文件恢复技术

Jun 07, 2016 pm 03:13 PM
server sql recover technology master database document easy

SQL Server 数据库 备份有两种方式,一种是使用BACKUP DATABASE将 数据库 文件 备份出去,另外一种就是直接拷贝 数据库 文件 mdf和日志 文件 ldf的方式。 下面将主要讨论一下后者的备份与 恢复 。本文假定您能熟练使用SQL Server Enterprise Manager(SQL Ser

  SQL Server数据库备份有两种方式,一种是使用BACKUP DATABASE将数据库文件备份出去,另外一种就是直接拷贝数据库文件mdf和日志文件ldf的方式。

  下面将主要讨论一下后者的备份与恢复。本文假定您能熟练使用SQL Server Enterprise Manager(SQL Server企业管理器)和SQL Server Quwey Analyser(SQL Server查询分析器)。

  1、正常的备份、恢复方式

  正常方式下,我们要备份一个数据库,首先要先将该数据库从运行的数据服务器中断开,或者停掉整个数据库服务器,然后复制文件

以下是引用片段:
  卸下数据库的命令:Sp_detach_db 数据库
  连接数据库的命令:Sp_attach_db或者sp_attach_single_file_db
  s_attach_db [@dbname =] ′dbname′, [@filename1 =] ′filename_n′ [,...16]
  sp_attach_single_file_db [@dbname =] ′dbname′, [@physname =] ′physical_name′

  使用此方法可以正确恢复SQL Sever7.0和SQL Server 2000的数据库文件,要点是备份的时候一定要将mdf和ldf两个文件都备份下来,mdf文件数据库数据文件,ldf是数据库日志文件

  例子:

  假设数据库为test,其数据文件为test_data.mdf,日志文件为test_log.ldf。

  下面我们讨论一下如何备份、恢复数据库

以下是引用片段:
  卸下数据库:sp_detach_db 'test'
  连接数据库: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、只有mdf文件恢复技术

  由于种种原因,我们如果当时仅仅备份了mdf文件,那么恢复起来就是一件很麻烦的事情了。

  如果您的mdf文件是当前数据库产生的,那么很侥幸,也许你使用sp_attach_db或者sp_attach_single_file_db可以恢复数据库,但是会出现类似下面的提示信息

  设备激活错误。

  物理文件名 'C:\Program Files\Microsoft SQL Server\MSSQL\data\test_Log.LDF'可能有误。

  已创建名为 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\test_log.LDF' 的新日志文件

  但是,如果您的数据库文件是从其他计算机上复制过来的,那么很不幸,也许上述办法就行不通了。

  你也许会得到类似下面的错误信息:

  服务器: 消息 1813,级别 16,状态 2,行 1

  未能打开新数据库 'test'。CREATE DATABASE 将终止。

  设备激活错误。物理文件名 'd:\test_log.LDF' 可能有误。

  怎么办呢?别着急,下面我们举例说明恢复办法。

  A.我们使用默认方式建立一个供恢复使用的数据库(如test)。可以在SQL Server Enterprise Manager里面建立。

  B.停掉数据库服务器。

  C.将刚才生成的数据库的日志文件test_log.ldf删除,用要恢复数据库mdf文件覆盖刚才生成的数据库数据文件test_data.mdf。

  D.启动数据库服务器。此时会看到数据库test的状态为"置疑"。这时候不能对此数据库进行任何操作。

  E.设置数据库允许直接操作系统表。此操作可以在SQL Server Enterprise Manager里面选择数据库服务器,按右键,选择"属性",在"服务器设置"页面中将"允许对系统目录直接修改"一项选中。也可以使用如下语句来实现。

以下是引用片段:
  use master
  go
  sp_configure 'allow updates',1
  go
  reconfigure with override
  go

  F.设置test为紧急修复模式

  

以下是引用片段:
update sysdatabases set status=-32768 where dbid=DB_ID('test')

  此时可以在SQL Server Enterprise Manager里面看到该数据库处于"只读\置疑\脱机\紧急模式"可以看到数据库里面的表,但是仅仅有系统表。

  G.下面执行真正的恢复操作,重建数据库日志文件

  

以下是引用片段:
dbcc rebuild_log('test','C:\Program Files\Microsoft SQL Server\MSSQL\Data\test_log.ldf')

  执行过程中,如果遇到下列提示信息:

  服务器: 消息 5030,级别 16,状态 1,行 1

  未能排它地锁定数据库以执行该操作。

  DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。

  说明您的其他程序正在使用该数据库,如果刚才您在F步骤中使用SQL Server Enterprise Manager打开了test库的系统表,那么退出SQL Server Enterprise Manager就可以了。

  正确执行完成的提示应该类似于:

  警告: 数据库 'test' 的日志已重建。已失去事务的一致性。应运行 DBCC CHECKDB 以验证物理一致性。将必须重置数据库选项,并且可能需要删除多余的日志文件

  DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。

  此时打开在SQL Server Enterprise Manager里面会看到数据库的状态为"只供DBO使用"。此时可以访问数据库里面的用户表了。

  H.验证数据库一致性(可省略)

  

以下是引用片段:
dbcc checkdb('test')

  一般执行结果如下:

  CHECKDB 发现了 0 个分配错误和 0 个一致性错误(在数据库 'test' 中)。

  DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。

  I.设置数据库为正常状态

  sp_dboption 'test','dbo use only','false'

  如果没有出错,那么恭喜,现在就可以正常的使用恢复后的数据库啦。

  J.最后一步,我们要将步骤E中设置的"允许对系统目录直接修改"一项恢复。因为平时直接操作系统表是一件比较危险的事情。当然,我们可以在SQL Server Enterprise Manager里面恢复,也可以使用如下语句完成。

以下是引用片段:
  sp_configure 'allow updates',0
  go
  reconfigure with override
  go
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

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())

More than just 3D Gaussian! Latest overview of state-of-the-art 3D reconstruction techniques More than just 3D Gaussian! Latest overview of state-of-the-art 3D reconstruction techniques Jun 02, 2024 pm 06:57 PM

Written above & The author’s personal understanding is that image-based 3D reconstruction is a challenging task that involves inferring the 3D shape of an object or scene from a set of input images. Learning-based methods have attracted attention for their ability to directly estimate 3D shapes. This review paper focuses on state-of-the-art 3D reconstruction techniques, including generating novel, unseen views. An overview of recent developments in Gaussian splash methods is provided, including input types, model structures, output representations, and training strategies. Unresolved challenges and future directions are also discussed. Given the rapid progress in this field and the numerous opportunities to enhance 3D reconstruction methods, a thorough examination of the algorithm seems crucial. Therefore, this study provides a comprehensive overview of recent advances in Gaussian scattering. (Swipe your thumb up

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.

Revolutionary GPT-4o: Reshaping the human-computer interaction experience Revolutionary GPT-4o: Reshaping the human-computer interaction experience Jun 07, 2024 pm 09:02 PM

The GPT-4o model released by OpenAI is undoubtedly a huge breakthrough, especially in its ability to process multiple input media (text, audio, images) and generate corresponding output. This ability makes human-computer interaction more natural and intuitive, greatly improving the practicality and usability of AI. Several key highlights of GPT-4o include: high scalability, multimedia input and output, further improvements in natural language understanding capabilities, etc. 1. Cross-media input/output: GPT-4o+ can accept any combination of text, audio, and images as input and directly generate output from these media. This breaks the limitation of traditional AI models that only process a single input type, making human-computer interaction more flexible and diverse. This innovation helps power smart assistants

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

See all articles