Troubleshooting Guide: Error 5129 – Log Cannot Be Rebuilt When Primary File Is Read-Only
Why This Error Happens
Error 5129 occurs when SQL Server 2025 attempts to rebuild a missing or corrupted transaction log but cannot write to the primary data file (.mdf) because it is marked as read-only. This typically happens when the file resides on a read-only volume, has restrictive NTFS permissions, or the database was set to READ_ONLY state before the log file was lost or detached. SQL Server requires write access to the primary file to update header information during log rebuild operations, so the operation fails immediately when this access is denied.
Step-by-Step Resolution Guide
Step 1: Verify the Read-Only Status of the Primary File
- Navigate to the data file location, typically
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\ - Right-click the
.mdffile, select Properties, and check the Read-only checkbox under the General tab. - Alternatively, run PowerShell:
Get-ItemProperty -Path "D:\SQLData\YourDB.mdf" | Select-Object IsReadOnly
Step 2: Remove the Read-Only Attribute
- Uncheck the Read-only box in file Properties, or run:
attrib -r "D:\SQLData\YourDB.mdf" - If the file resides on a read-only mounted volume (e.g., a SAN snapshot or DVD-based storage), move it to a writable NTFS volume instead.
- Confirm the SQL Server service account (e.g.,
NT Service\MSSQLSERVER) has Modify and Write permissions on the folder via the Security tab.
Step 3: Check and Reset the Database READ_ONLY Property
- Open SQL Server Management Studio (SSMS) and connect to the instance.
- Run the following T-SQL to confirm the database state:
SELECT name, is_read_only FROM sys.databases WHERE name = 'YourDB';
- If
is_read_onlyreturns1, switch it off with:
ALTER DATABASE YourDB SET READ_WRITE WITH ROLLBACK IMMEDIATE;
Step 4: Rebuild the Transaction Log
- With the file now writable, attempt the rebuild using:
ALTER DATABASE YourDB REBUILD LOG ON (NAME = YourDB_Log, FILENAME = 'D:\SQLData\YourDB_log.ldf');
- If the database was in EMERGENCY mode prior to this, ensure it is set using:
ALTER DATABASE YourDB SET EMERGENCY;
- Restart SQL Server service if file locks persist:
Restart-Service MSSQLSERVER
Step 5: Validate File System and Storage Policies
- On Windows Server 2025, check Storage Replica or ReFS configurations that may enforce read-only states on replicated volumes.
- Use
fsutil fsinfo volumeinfo D:to confirm the volume isn’t flagged with read-only media attributes.
Alternative or Verification Steps
- After rebuilding, run
DBCC CHECKDB('YourDB') WITH NO_INFOMSGSto confirm database integrity and ensure no residual corruption remains. - If the issue persists, restore the database from the most recent full backup and apply transaction log backups instead of attempting an in-place rebuild, especially if the file permissions issue stems from a corrupted ACL that cannot be easily reset.