This article will cover corruption and recovery scenarios in the context of SQL Server FILESTREAM including missing data, incompatible files types, DBCC checks, orphan files and garbage collection

本文将介绍SQL Server FILESTREAM上下文中的损坏和恢复方案,包括丢失数据,不兼容的文件类型,DBCC检查,孤立文件和垃圾回收。

It is the primary responsibility of the DBA is maximum database uptime. Database corruption can be very problematic because you cannot predict data corruption and, therefore, prepare the solution for it proactively. Most of the time database corruption occurs ia due to external reasons such as power failure, storage corruption etc. With FILESTREAM, we are dealing with SQL Server database files (MDF, LDF) along with file system objects as well. Therefore, we need to be aware of the possible scenarios that may occur in the FILESTREAM database wich can cause database ccorruption. In this article, my focus is to highlight such common scenarios specific for the FILESTREAM database and solutions for it.

DBA的主要职责是最大程度地延长数据库的正常运行时间。 数据库损坏可能会造成很大的问题,因为您无法预测数据损坏,因此需要主动为其准备解决方案。 大多数情况下,数据库损坏是由于外部原因(例如电源故障,存储损坏等)而发生的。使用FILESTREAM,我们还将处理SQL Server数据库文件(MDF,LDF)以及文件系统对象。 因此,我们需要注意FILESTREAM数据库中可能发生的可能导致数据库损坏的情况。 在本文中,我的重点是重点介绍针对FILESTREAM数据库及其解决方案的常见方案。

Before we move further, I will assume that you have a FILESTREAM database and the objects in the FILESTREAM container. You can take reference from the articles in the TOC section (see bottom) if you need to build your example environment to follow along

在继续之前,我将假定您具有FILESTREAM数据库,并且该对象位于FILESTREAM容器中。 如果您需要构建示例环境以进行后续学习,可以参考TOC部分(参见底部)中的文章作为参考。

检查腐败 (Checking for corruption)

Below is the screenshot for our example FILESTREAM database table.

以下是我们的示例FILESTREAM数据库表的屏幕截图。

There are 12 files in the FILESTREAM container.

FILESTREAM容器中有12个文件。

We can use the DBCC CHECKDB command to detect any corruption or consistency issues. It can check the physical and logical consistency error on the database. Let’s run this command, and in the following screenshot, you can notice that we do not have any consistency issues for the FILESTREAM database.

我们可以使用DBCC CHECKDB命令来检测任何损坏或一致性问题。 它可以检查数据库上的物理和逻辑一致性错误。 让我们运行此命令,在下面的屏幕快照中,您可以注意到FILESTREAM数据库没有任何一致性问题。

“CHECKDB found 0 allocation errors and 0 consistency errors in database ‘FileStreamDemoDB_test’.”

“ CHECKDB在数据库'FileStreamDemoDB_test'中发现了0个分配错误和0个一致性错误。”

FILESTREAM容器中的文件丢失或删除 (Missing or deleted files from a FILESTREAM container)

Let’s view some FILESTREAM database corruption scenarios.

让我们查看一些FILESTREAM数据库损坏的情况。

Note: We might perform steps to deliberately corrupt the FILESTREAM database, you should not run these examples in the production database.

注意:我们可能会执行有意破坏FILESTREAM数据库的步骤,因此您不应在生产数据库中运行这些示例。

We should not modify or delete any files in the FILESTREAM container directly at the OS level. If we want to do the changes or delete the object, we should do it from the query. SQL Server removes the physical file once the garbage collector process runs depending upon the recovery model and the transaction log backups.

我们不应该直接在操作系统级别上修改或删除FILESTREAM容器中的任何文件。 如果要更改或删除对象,则应从查询中执行。 一旦运行垃圾回收器进程,SQL Server就会删除物理文件,具体取决于恢复模型和事务日志备份。

Let us remove the highlighted file manually from the FILESTREAM container.

让我们从FILESTREAM容器中手动删除突出显示的文件。

We are not going to delete this file. Instead, we will move this file to a backup folder so that it can be used later.

我们不会删除此文件。 相反,我们会将文件移到备份文件夹中,以便以后使用。

Let us try to access the database. We cannot access it now, below is the error message.

让我们尝试访问数据库。 我们现在无法访问它,下面是错误消息。

We cannot access the database now, let us try to run the DBCC CHECKDB on this database and observe the output.

我们现在无法访问数据库,让我们尝试在该数据库上运行DBCC CHECKDB并观察输出。

DBCC CHECKDB reported consistency error in table ‘Tbl_Support_Document. It also gives a message that cannot find the FILESTREAM file. You can see in the following screenshot that the error is for the same file we moved from the FILESTREAM container earlier.

DBCC CHECKDB在表'Tbl_Support_Document中报告了一致性错误。 它还提供了一条消息,找不到FILESTREAM文件。 您可以在下面的屏幕截图中看到,该错误与我们之前从FILESTREAM容器中移出的文件相同。

恢复损坏的FILESTREAM数据库 (Recovering the corrupt FILESTREAM database )

We can recover this FILESTREAM database with consistency error in the following ways

我们可以通过以下方式恢复具有一致性错误的FILESTREAM数据库

  1. Recover the deleted object from the previous backup

    从先前的备份中恢复已删除的对象

    If we are taking regular backups, we can restore the FILESTREAM database in another location and replace the problematic file. In this example, let us consider that you found the file by restoring the FILESTREAM database backup so that we will copy that file into FILESTREAM container.

    如果我们要进行定期备份,则可以将FILESTREAM数据库还原到另一个位置并替换有问题的文件。 在此示例中,让我们考虑通过还原FILESTREAM数据库备份找到了文件,以便将文件复制到FILESTREAM容器中。

    We have all the files in the FILESTREAM container let us perform the DBCC CHECKDB again. We do not get any consistency error in the FILESTREAM database.

    我们将所有文件保存在FILESTREAM容器中,让我们再次执行DBCC CHECKDB。 我们在FILESTREAM数据库中没有收到任何一致性错误。

  2. Recover the FILESTREAM database without a backup of the removed object

    在不备份已删除对象的情况下恢复FILESTREAM数据库

    In the previous step, we recovered the database by replacing the problematic file in the FILESTREAM container. Suppose we do not have any backup for the FILESTREAM database or the file is not present in the backup as well. We can still recover the FILESTREAM database by running DBCC CHECKDB with repair_allow_data_loss. It will delete the corresponding row from the FILESTREAM table.

    在上一步中,我们通过替换FILESTREAM容器中有问题的文件来恢复数据库。 假设我们没有FILESTREAM数据库的任何备份,或者备份中也没有该文件。 我们仍然可以通过运行带有repair_allow_data_loss的DBCC CHECKDB来恢复FILESTREAM数据库。 它将从FILESTREAM表中删除相应的行。

    We already recovered the database in the previous step, therefore, remove the same file again from the FILESTREAM container manually.

    我们已经在上一步中恢复了数据库,因此,请再次从FILESTREAM容器中手动删除同一文件。

    We need to put the database in the single user mode using the Alter Database command.

    我们需要使用Alter Database命令将数据库置于单用户模式。

    ALTER DATABASE FileStreamDemoDB_test SET SINGLE_USER  WITH ROLLBACK IMMEDIATE GO
    

    This command kills all of the existing connections in the database and put the database into an existing mode.

    此命令将杀死数据库中的所有现有连接,并将数据库置于现有模式。

    In the single user mode, only one connection can be active at a time. Therefore, we need to run a further command in the same connection window. If you try to open another connection, you get the below error message.

    在单用户模式下,一次只能建立一个连接。 因此,我们需要在同一连接窗口中运行其他命令。 如果您尝试打开另一个连接,则会收到以下错误消息。

    Now, run the below command in a previous connection window to repair the database using the DBCC CHECKDB. You get the following output of this command execution.

    现在,在以前的连接窗口中运行以下命令,以使用DBCC CHECKDB修复数据库。 您将获得此命令执行的以下输出。

    DBCC CHECKDB('FileStreamDemoDB_test', repair_allow_data_loss)
    

    Let us understand this output message. In first part, it deletes the row from the FILESTREAM table for the deleted file.

    让我们了解此输出消息。 在第一部分中,它从FILESTREAM表中删除已删除文件的行。

    In the second step, it successfully rebuilds the non-clustered index as well.

    在第二步中,它也成功地重建了非聚集索引。

    In the bottom part of the message, you can see that the database repaired the consistency error successfully.

    在消息的底部,您可以看到数据库成功修复了一致性错误。

    Let us put the database in the MULTI_USER using the below command.

    让我们使用以下命令将数据库放入MULTI_USER中。

    ALTER DATABASE FileStreamDemoDB_test SET MULTI_USERGO
    

    We can access the database now. Previously we were having 12 rows while the number of rows in the FILESTREAM table is 11, it shows that row is deleted by the DBCC CHECKDB command.

    我们现在可以访问数据库。 以前我们有12行,而FILESTREAM表中的行数为11,这表明该行已被DBCC CHECKDB命令删除。

FILESTREAM容器中的孤立文件 (Orphan files in a FILESTREAM container)

We should not use the FILESTREAM container for storing files other than the FILESTREAM objects. In the following image, you can see the file in the container that is not part of the FILESTREAM data.

我们不应该使用FILESTREAM容器来存储FILESTREAM对象以外的文件。 在下图中,您可以看到容器中的文件不属于FILESTREAM数据。

Now we can run the DBCC CHECKDB in the FILESTREAM database, and we get the error message as following.

现在,我们可以在FILESTREAM数据库中运行DBCC CHECKDB,并且收到以下错误消息。

We get the consistency error again in the FILESTREAM database, and it gives the message that the file ‘Rajendra.PNG’ is not a valid FILESTREAM file in the container.

我们在FILESTREAM数据库中再次收到一致性错误,它提示消息“ Rajendra.PNG”不是容器中的有效FILESTREAM文件。

FILESTREAM container files do not have any extension. It also has the name in the form of metadata. Now let us rename the existing file ‘Rajendra.PNG’ in the FILESTREAM format. I renamed the file to the highlighted name ‘0000002d-00000368-000c’. We can note here that it does not have any extension with its name.

FILESTREAM容器文件没有任何扩展名。 它还具有元数据形式的名称。 现在,让我们以FILESTREAM格式重命名现有文件“ Rajendra.PNG”。 我将文件重命名为突出显示的名称“ 0000002d-00000368-000c”。 我们可以在这里注意到,它的名称没有任何扩展名。

Let us rerun the DBCC CHECKDB command and observe the error message. It again complains about the consistency error message however, there is a difference in the error message.

让我们重新运行DBCC CHECKDB命令并观察错误消息。 它再次抱怨一致性错误消息,但是错误消息有所不同。

  • Previous error message: Table error: The file “\fea3feda-924f-4aee-b320-e33669d27d18\rajendra.png” in the rowset directory ID 0b05ad18-6dc6-4391-b644-413b27535e16 is not a valid FILESTREAM file in container ID 65537.
  • 先前的错误消息: 表错误:行集目录ID 0b05ad18-6dc6-4391-b644-413b27535e16中的文件“ \ fea3feda-924f-4aee-b320-e33669d27d18 \ rajendra.png” 不是容器ID 65537中的有效FILESTREAM文件
  • Current error message: The 当前错误消息:在FILESTREAM目录ID fea3feda-924f-4aee-b320-e33669d27d18中找到orphaned file “0000002d-00000368-000c” was found in the FILESTREAM directory ID fea3feda-924f-4aee-b320-e33669d27d18 孤立的文件“ 0000002d-00000368-000c”
    Container ID 65537 for object ID 1237579447, index ID 0, partition ID 72057594043695104, column ID 4.
    对象ID 1237579447,索引ID 0,分区ID 72057594043695104,列ID 4的容器ID 65537。

We can fix this error message by removing the file from the FILESTREAM container. We cannot fix it by running the DBCC CHECKDB with repair_allow_data_loss.

我们可以通过从FILESTREAM容器中删除文件来修复此错误消息。 我们无法通过使用repair_allow_data_loss运行DBCC CHECKDB来修复它。

However, let us try it. In the following image, we get the same error message. It does not fix the error.

但是,让我们尝试一下。 在下图中,我们得到了相同的错误消息。 它不能解决错误。

Therefore, we need to remove the unwanted file from the FILESTREAM container and execute the DBCC CHECKDB again. We did not get any consistency error message after removing the file manually from the FILESTREAM container.

因此,我们需要从FILESTREAM容器中删除不需要的文件,然后再次执行DBCC CHECKDB。 从FILESTREAM容器中手动删除文件后,我们没有收到任何一致性错误消息。

从SQL Server FILESTREAM容器中删除垃圾文件 (Removing the Garbage files from the SQL Server FILESTREAM container)

We can do DML (insert and update) activity in the FILESTREAM table. Consider we have performed an update on the FILESTREAM table, SQL Server does not remove the old file from the container immediately. SQL Server removes these files once the garbage collector process runs and it removes the files if it is not required in the recovery process.

我们可以在FILESTREAM表中执行DML(插入和更新)活动。 考虑到我们已经在FILESTREAM表上执行了更新,SQL Server不会立即从容器中删除旧文件。 一旦垃圾收集器进程运行,SQL Server就会删除这些文件,如果恢复过程中不需要它,它将删除文件。

In the following query, we replaced one image in the existing FILESTREAM table. The old image file becomes a garbage file.

在以下查询中,我们替换了现有FILESTREAM表中的一张图像。 旧的图像文件成为垃圾文件。

UPDATE [Tbl_Support_Documents]
SET [DocumentBin] = (SELECT *
FROM OPENROWSET(
BULK 'C:\Users\rajen_000\Pictures\Durdle door\IMG_20140316_161337.jpg',
SINGLE_BLOB) AS Document)
WHERE [Document_ID] = '44E16841-D5BB-4495-A27D-2BC877E41F8F'
GO

In the following image, the highlighted file is the garbage file.

在下图中,突出显示的文件是垃圾文件。

Let us remove this garbage file manually and run the DBCC CHECKDB command. We do not receive any consistency error message despite by removing the garbage file manually.

让我们手动删除该垃圾文件,然后运行DBCC CHECKDB命令。 尽管手动删除了垃圾文件,但我们没有收到任何一致性错误消息。

We can access the FILESTREAM database without any issues however, I would recommend not doing any changes in the FILESTREAM container directly at the OS level. It might cause serious corruption issues for the database that might impact the database availability.

我们可以毫无问题地访问FILESTREAM数据库,但是我建议不要直接在OS级别对FILESTREAM容器进行任何更改。 这可能会导致数据库出现严重的损坏问题,从而可能影响数据库的可用性。

结论: (Conclusion:)

In this article, we wrote about the various consistency issues for the SQL Server FILESTREAM and the solution of them to fix various FILESTREAM related corruption issues.

在本文中,我们讨论了SQL Server FILESTREAM的各种一致性问题,以及解决各种与FILESTREAM相关的损坏问题的解决方案。

目录 (Table of contents)

FILESTREAM in SQL Server
Managing data with SQL Server FILESTREAM tables
SQL Server FILESTREAM Database backup overview
Restoring a SQL Server FILESTREAM enabled database
SQL Server FILESTREAM database recovery scenarios
Working with SQL Server FILESTREAM – Adding columns and moving databases
SQL Server FILESTREAM internals overview
Importing SQL Server FILESTREAM data with SSIS packages
SQL Server FILESTREAM queries and Filegroups
Viewing SQL Server FILESTREAM data with SSRS
SQL Server FILESTREAM Database Corruption and Remediation
Export SQL Server FILESTREAM Objects with PowerShell and SSIS
SQL FILESTREAM and SQL Server Full Text search
SQL Server FILESTREAM and Replication
SQL Server FILESTREAM with Change Data Capture
Transaction log backups in a SQL FILESTREAM database
SQL FILESTREAM Compatibility with Database Snapshot, Mirroring, TDE and Log Shipping
SQL Server FILETABLE – the next generation of SQL FILESTREAM
Managing Data in SQL Server FILETABLEs
SQL Server FILETABLE Use Cases
SQL Server中的文件流
使用SQL Server FILESTREAM表管理数据
SQL Server FILESTREAM数据库备份概述
还原启用了SQL Server FILESTREAM的数据库
SQL Server FILESTREAM数据库恢复方案
使用SQL Server FILESTREAM –添加列和移动数据库
SQL Server FILESTREAM内部概述
使用SSIS包导入SQL Server FILESTREAM数据
SQL Server FILESTREAM查询和文件组
使用SSRS查看SQL Server FILESTREAM数据
SQL Server FILESTREAM数据库损坏和修复
使用PowerShell和SSIS导出SQL Server FILESTREAM对象
SQL FILESTREAM和SQL Server全文搜索
SQL Server FILESTREAM和复制
具有更改数据捕获功能SQL Server FILESTREAM
SQL FILESTREAM数据库中的事务日志备份
SQL FILESTREAM与数据库快照,镜像,TDE和日志传送的兼容性
SQL Server FILETABLE –下一代SQL FILESTREAM
在SQL Server FILETABLEs中管理数据
SQL Server FILETABLE用例

翻译自: https://www.sqlshack.com/sql-server-filestream-database-corruption-and-remediation/

SQL Server FILESTREAM数据库损坏和修复相关推荐

  1. SQL Server系统数据库损坏时的快速恢复使用

    1.假设data文件夹中master,masterlog,model,modellog,msdbdata,msdblog的系统数据库文件已损坏,复制Binn/template中的同名文件替换. 2.管 ...

  2. SQL Server FILESTREAM查询和文件组

    In this series of the SQL Server FILESTREAM (see TOC at bottom), We have gone through various aspect ...

  3. 使用SSIS包导入SQL Server FILESTREAM数据

    初始配置 (Initial configuration) We have been exploring the SQL Server FILESTREAM feature in this ongoin ...

  4. 如何手工完全卸载SQL SERVER 2000数据库

    手工卸载SQL SERVER 2000数据库 (转自http://blog.sina.com.cn/s/blog_5d57ca730100b07d.html)           部分用户的SQL S ...

  5. 修复SQL Server Compact数据库时遇到找不到指定的模块的异常

    如果你在调用 SqlCeEngine.Repair 方法对 SQL Server Compact 数据库进行修复时遇到以下异常,可以通过在 Windows Mobile 设备上安装复制组件解决.SQL ...

  6. SQL Server 2005数据库日志文件损坏的情况下如何恢复数据库

    在某些偶然的情况下,会引起SQL Server 2005数据库日志文件的损坏,比如:硬件故障.计算机非正常重启或关机. 当SQL Server 2005数据库日志文件损坏时,可能会出现以下情况: 1. ...

  7. SQL Server HA - 数据库镜像 (Mirroring)

    数据库镜像简介 From: http://mssqlmct.blog.51cto.com/9951484/1641831 11.3  数据库镜像简介 11.3.1 数据库镜像会话 数据库镜像(Data ...

  8. SQL Server附加数据库失败,823错误。

    SQL Server附加数据库失败,823错误. 服务器在正常运行的情况下突然断电,导致数据库文件严重损坏.附加或还原数据库无果,在附加数据库过程中出现823字样的错误. 查阅SQL server 联 ...

  9. 如何使用 SQL Server FILESTREAM 存储非结构化数据?这篇文章告诉你!

    作者 | ALEN İBRIÇ 译者 | 火火酱,责编 | Carol 封图 | CSDN 付费下载于视觉中国 在本文中,我将解释如何使用SQL Server FILESTREAM来存储非结构化数据. ...

最新文章

  1. SQL Server 表分区实战系列(文章索引)
  2. 博客园如何使用MarkDown
  3. 设计模式--1(设计模式基础,设计模式基本原则,设计模式分类)
  4. Extjs Grid 中给已经添加过Filter的列增加标识
  5. vs2013创建c++控制台应用程序
  6. 思科华为命令没有对比就没有伤害
  7. 深入理解同步/异步与阻塞/非阻塞区别 (转)
  8. 会javascript,你才是前端工程师!
  9. android 自定义pickerview,Simple PickerView for Android - 这是一个高仿 IOS PickerView 控件的库...
  10. 破解校园网“一人一号” 高校学子必备秘籍
  11. 谷歌浏览器访问端口限制
  12. 什么是软件外包公司?要不要去外包公司?
  13. Oracle树形结构拖拽之插队重新排序
  14. 来自腾讯的高性能服务器架构思路
  15. 使用python开发多人一对一聊天系统(文字聊天,视频通讯,文件传输)
  16. Oracle 考试题 原题
  17. 电子邮件服务器的ip地址_可用的不同类型的IP地址
  18. EXCEL2003版无法运行2007以上版本VBA程序
  19. NOIp初赛模拟题及标准答案(提高组)
  20. (力扣)LeetCode19. 删除链表的倒数第 N 个结点(C++/C语言)

热门文章

  1. 公司顾问岗位职责_【热门推荐】泉州奥育特教育科技有限公司
  2. head first设计模式 pdf_设计模式之观察者模式
  3. python批量修改文件名_python实现批量改文件名称的方法
  4. 复制两个类的相同属性
  5. C# 中 ? 和 ??
  6. 【Python3_进阶系列_010】Python3-生成器
  7. 用haslib给字符加密
  8. 关于$.ajax的请求格式
  9. [轉]MS SQL 显示表结构
  10. 数据库原理—DBS的物理组成和结构模式(五)