Summary: Corruption in transaction log file or a log database (.ldf) file makes SQL Server database inaccessible. This article outlines the plausible reasons behind log file corruption and errors you may encounter because of corruption. Further, it describes methods to resolve the issue. These methods include restoring database from backup, rebuilding corrupt log file and using a specialized SQL recovery software.
A database in SQL Server comprises three types of files: primary data file (.mdf), secondary data file (.ndf) and transaction log file or log database file (.ldf).
The primary and secondary data files are used for storing information about database objects like tables, indexes, triggers, views, etc. On the other hand, log file records all transactions and changes made to the database by each transaction. Corruption in any of the data files or log file can make the database inaccessible.
In this article, however, we will restrict our discussion on log file corruption and methods to fix a corrupt .ldf file.
Before We Begin
Before discussing methods to fix transaction log corruption issue, it is important to determine the reasons that led to such an issue in the first place.
What causes corruption in transaction log file?
Below are some plausible reasons that may result in log file corruption:
Abnormal System Shutdown: The system terminated abnormally, due to power outage or other reasons, without clean shutdown of the database.
Storage Size Issue: Transaction log file has limited storage space, and exceeding its limit increases chances of corruption.
Hardware Issue or Faulty Memory: Hardware fault occurred with the I/O subsystem used for hosting the system and database files, or faulty memory.
Virus Infection: The system hosting SQL server database got affected by virus attack, resulting in corrupt log file.
Errors You May Encounter Due To Log File Corruption
Error 1 – Error Message 15105
Operating system error 23 (failed to retrieve text for this error. Reason: 15105) on file “C:\Program Files\Microsoft SQL Server\MSSQL10_50.MYSERVER\MSSQL\DATA\MY_DATABASE.ldf” during CheckLogBlockReadComplete
Error 2 – Failed to Attach Log File
When trying to attach log file on new SQL Server, you receive
“Could not open new database ‘Your_DatabaseName’. CREATE Database is aborted”.
File activation failure. The physical file name ‘C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\XXX.ldf’ may be incorrect. Diagnose and correct additional errors, and retry the operation.
Error 4 – SQL Server Service Startup Failure
FileMgr::StartLogFiles: Operating system error 2 (The system cannot find the file specified.) occurred while creating or opening file ‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\xxx.ldf’. Diagnose and correct the operating system error, and retry the operation.
Methods to Repair SQL Server Database
NOTE: Check the SQL Server error logs and event logs of Windows system and application hosting the database. If you find any hardware problem, get it fixed and see whether the corruption issue has been resolved or not. If not, apply the following methods to repair the damaged log file.
Method 1 – Restore Database from a Good Backup
NOTE: If you don’t have a proper backup strategy configured or the backup files are lost, skip to method 2.
The simple and safe approach to fix log file corruption issue is restoring the database from the last healthy, point-in-time, database backup.
Let’s look at the steps to restore SQL Server database from full database backup:
Step 1: Open SQL Server Management Studio (SSMS), and connect to SQL Server.
Step 2: Expand Databases folder in Object Explorer.
Step 3: Right-click the Database, select Restore Database.
Step 4: In Restore Database window, under Source for restore section, select From device, and then click the button next to it to specify database file location.
Step 5: In Specify backup window, select Backup media type, and then click Add button to insert the backup file location.
Step 6: Select the backup file you need to restore and click OK.
Step 7: Again, click OK.
Step 8: The backup file will be listed on the Database restore window.
Step 9: Before restoring the backup, select Options under Select a page section in the Restore Database window.
Step 10: Select one of the checkboxes under Restore Options section. Click OK.
Method 2 – Rebuild Corrupt Transaction Log File
Try rebuilding the corrupt log file to make the database accessible again. To do so, follow these steps:
Step 1: Put the database in EMERGENCY MODE by using the following command:
ALTER DATABASE SET EMERGENCY, SINGLE_USER
Running the above command will help bring the database up without transaction log file.
Step 2: Once the database set to a single user mode, run DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS option to repair the corrupt transaction log file:
NOTE: Before proceeding with the repair process, make sure to create backup copies of your database. This is because REPAIR_ALLOW_DATA_LOSS option involves data loss risk.
DBCC CHECKDB ('', REPAIR_ALLOW_DATA_LOSS)
If corruption is still there then, use the following command to rebuild the log file. Bring the database in Offline mode and change the name of the corrupted Log file associated with it. Now, run the following command:
ALTER DATABASE [original_log_file_name] REBUILD LOG ON (NAME= logicalname, FILENAME='C:\Program Files\Microsoft SQL Server\MSSQLn.MSSQLSERVER\MSSQL\DATA\file_name.ldf')
Method 3 – Use Stellar Repair for MS SQL Software
If you still cannot access your database, using SQL database repair tool may help. The software uses advanced algorithms to fix corrupt .ldf file by repairing the database (.mdf file).
Steps to use Stellar Repair for MS SQL Software
Step 1: Download, install and launch Stellar Repair for MS SQL software.
Step 2: The Instruction window is displayed. Follow the instructions and click OK.
Step 3: In Select Database window, click Browse to select the corrupt MDF file you want to repair.
NOTE: If you are not aware of the MDF file location, click ‘Search’ to find and select the file.
Step 4: Once the file is selected, click Repair to initiate the repair process.
Step 5: The software provides preview of repaired database file in a tree-view list on the left panel of Preview window.
Step 6: Select all or specific database objects you want to recover, and then click Save on Home menu.
Step 7: In Save As window, select MDF under Save As section. Next, choose whether you want to save the repaired database file in ‘New Database’ or ‘Live Database’.
Step 8: Fill in the required details under Connect to Server section, and then click Save.
SQL Server database transaction log file becomes corrupt due to several reasons, including unclean shutdown of the database, hardware fault, large-sized LDF file and virus attack. A corrupt log file can make the database inaccessible.
You can try restoring the database from the last known good backup. But, keep in mind, restoring database from backup may cause data loss depending on the recovery strategy in place.
If the backup is not available or has failed, you can try rebuilding the corrupt log file using DBCC CHECKDB command. But this command has certain limitations. It may fail to fix major corruption in SQL Server database and returns an error. For instance, you may receive the following error when trying to repair a severely corrupt database file:
Also, running DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS option results in data loss.
If none of the method works, use Stellar Repair for SQL software to repair corrupt log file and fix SQL database issues, without data loss risk.
Q. I have been receiving an error ‘Transaction log backup’ failed. Can I use DBCC CHECKDB command to check for issues with transaction log backup?
Answer. No, DBCC CHECKDB does not check the transaction log file.
Q. Does taking a full backup reset the log backup?
Answer. No, doing a full backup does not reset the log backup.
Q. What is the difference between ‘DBCC CHECKDB command used with REPAIR_ALLOW_DATA_LOSS option’ and ‘ALTER DATABASE REBUILD LOG ON command’?
Answer: The DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS option checks the database for any inconsistency error. The command first attempts to use the log file to recover from any database inconsistencies. Secondly, if the log is missing it helps rebuild the transaction log file.
The ALTER DATABASE REBUILD LOG command won’t work if there were open transactions (not written to disk) when the log file turns inaccessible.
Priyanka is a technology expert working for key technology domains that revolve around Data Recovery and related software's. She got expertise on related subjects like SQL Database, Access Database, QuickBooks, and Microsoft Excel. Loves to write on different technology and data recovery subjects on regular basis. Technology freak who always found exploring neo-tech subjects, when not writing, research is something that keeps her going in life.