SQL Error 5172: What It Means and How to Repair MDF File Corruption
Each MDF file in SQL is divided into pages of 8KB. The initial page (Page 0) is known as the header page, which contains all metadata about the database file, including version, size, etc. The server first reads this header page of the MDF file to upload/attach/open it. If the header page of the database file is damaged, corrupted or unrecognized, then the Server throws Error msg: 5172, level 16, state 15. When this error occurs, you may fail to open and access MDF file. Sometimes, your SQL server crashes during startup. In this article we will discuss the methods to repair MDF file to resolve SQL error 5172.
Causes of SQL Error 5172: The header for the file is not valid. Page audit property is incorrect.
This error occurs due to corruption in MDF file header. There are several reasons that can lead to the corruption in, such as:
- Bad sectors on the hard-disk storing database file.
- Abrupt shutdown or system crash
- Lack of storage space on the hard drive where the file is located.
- Bugs in SQL Server
- Virus or malware infection
To resolve this error, restore and repair the corrupt or damaged MDF file by using the below methods:
Method 1- Restore from Backup:
SQL allows you create .bak file to backup the MDF file. If you have created this backup file and it is readable then you can easily restore the corrupt MDF file. You can restore SQL server database from bak file using SSMS, Windows Powershell or T-SQL queries.
Here's how to restore a .bak file using T-SQL queries:
Note: First, make sure your backup file is readable. To check the integrity of the bak file, run the RESTORE VERIFYONLY statement
Once you've confirmed that your backup is not corrupted, run the following RESTORE command in the Query Editor window to restore the backup file:
USE [master];
GO
BACKUP DATABASE [testing35]
TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\testing35.bak'
WITH NOFORMAT, NOINIT,
NAME = N'testing35-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10;
GO
If your backup file is corrupted then you can receive restore failed errors such as [Error] SQL Database Restore failed, database in use, or 3183. In such a case you can use the alternate repair methods to repair the corrupt MDF file.
Method 2: Use DBCC CHECKDB Command
MS SQL Server has in-built utilities such as the DBCC CHECKDB command to check and fix the integrity issues in database. This command helps to repair the pages, rows, index relationships, and other consistency issues in the SQL database file. You need to install SQL Server Management Studio (SSMS) and have Administrator privileges to use the DBCC CHECKDB command. You can use the command with different Repair options (see the example below).
DBCC CHECKDB
[ ( db_name | db_id | 0
[ , NOINDEX
| , { REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD } ]
) ]
[ WITH
{
[ ALL_ERRORMSGS ]
[ , EXTENDED_LOGICAL_CHECKS ]
[ , NO_INFOMSGS ]
[ , TABLOCK ]
[ , ESTIMATEONLY ]
[ , { PHYSICAL_ONLY | DATA_PURITY } ]
[ , MAXDOP = number_of_processors ]
}
]
]
When you use the REPAIR_REBUILD option with the DBCC CHECKDB command, it will try to rebuild the objects and recover the damaged objects. It only performs repairs. There is no possibility of data loss. Here is how to use the REPAIR_REBUILD option to repair the SQL database:
Note: First, make sure you have the ALTER permission on the database, and then set the database to SINGLE_USER mode.
DBCC CHECKDB (' testing35 ', REPAIR_REBUILD)
GO
If you want to repair the MDF file quickly, you can use the below command:
DBCC CHECKDB(' testing35 ' , REPAIR_FAST)
GO
Note: This REPAIR_FAST
option only maintains backward compatibility syntax and only checks the logical consistency.
If the above repair methods fail, then use the DBCC CHECKDB command with the REPAIR_ALLOW_DATA_LOSS
option by using the following command:
DBCC CHECKDB (N 'testing35', REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;
GO
Using DBCC CHECKDB
with the REPAIR_ALLOW_DATA_LOSS
repair option can repair highly corrupt MDF files, but it may not work in all scenarios. Sometimes, while repairing pages, it can cause data loss. Also, repairing large-sized databases using this command requires executing it multiple times, which is a time-consuming process.
Use a Professional MS SQL Repair Tool
If your backup file is not available and you don't want to risk data loss then you can use a professional SQL database repair tool, like Stellar Repair for SQL to repair the MDF file. It is a reliable and efficient tool for repairing MDF files in any condition. It can recover all the data, including pages, page headers, triggers, and stored procedures, from the corrupt SQL database without any data loss. The tool has no file-size limitations. Its demo version allows you to preview the repaired objects and check the tool's functionality. It supports all SQL versions, including 2016, 2017 and earlier.
Conclusion
SQL Error 5172 means your MDF file or its page header is corrupted. In the above article, we have discussed methods for repairing MDF files. You can restore your backup or you’re the DBCC CHECKDB command to repair the corrupt SQL database. To repair the database file without data loss, you can use a specialized MDF file repair tool like Stellar Repair for MS SQL. It can easily repair corrupt MDF/NDF file and restore all the data with complete integrity. Also, it helps recover deleted objects from corrupt MDF files. It offers flexible recovery of specific objects.