How to fix Error 3151 on Windows Server 2025 / Windows 11

Why This Error Happens

Error 3151 occurs when SQL Server attempts to restore the master database from a backup but encounters a mismatch or corruption that prevents the engine from completing the operation safely. This is typically caused by an incompatible backup file (wrong SQL Server version/build), a corrupted .bak file, or insufficient file system permissions on the data/log directories where master.mdf and mastlog.ldf reside. Because master is critical to instance startup, SQL Server immediately shuts down rather than running with a partially restored system database.

Step-by-Step Resolution Guide

Step 1: Verify the Backup File Integrity and Compatibility

  • Confirm the backup was taken from the same SQL Server 2025 build (check SELECT @@VERSION on the source if available).
  • Run RESTORE VERIFYONLY FROM DISK = 'C:\Backups\master.bak' to check for corruption before attempting a full restore.
  • If the file originated from an older version (e.g., SQL Server 2019/2022), discard it — cross-version master restores are not supported.

Step 2: Start SQL Server in Single-User Mode with Minimal Configuration

  • Open an elevated Command Prompt and navigate to the Binn directory, typically:
    C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Binn
  • Start the engine manually with: sqlservr.exe -m -T3608
  • The -T3608 trace flag starts SQL Server without automatically recovering any database except master, isolating the issue.

Step 3: Validate File Permissions on System Database Files

  • Navigate to C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\.
  • Right-click master.mdf and mastlog.ldf, select Properties > Security, and confirm the service account (e.g., NT Service\MSSQLSERVER) has Full Control.
  • Reapply permissions using PowerShell if needed:
    icacls "C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA" /grant "NT SERVICE\MSSQLSERVER:(OI)(CI)F"

Step 4: Rebuild System Databases if Restore Continues to Fail

  • Run Setup.exe from the SQL Server 2025 installation media with the /ACTION=REBUILDDATABASE parameter.
  • Specify the instance name, SA password, and current collation settings to match your original configuration.
  • Once rebuilt, reattempt the master restore from a verified, compatible backup.

Step 5: Restore Master from a Known-Good Backup

  • Connect via sqlcmd -S . -E while the instance is in single-user mode.
  • Execute: RESTORE DATABASE master FROM DISK = 'C:\Backups\master.bak' WITH REPLACE
  • Allow SQL Server to shut down automatically after the restore completes, then restart the SQL Server service normally.

Alternative or Verification Steps

  • After restarting the SQL Server service normally, check Event Viewer > Application Log and the SQL Server Error Log (ERRORLOG file in the LOG folder) to confirm no further 3151 errors appear and the instance reports “SQL Server is now ready for client connections.”
  • If restoration continues to fail, consider a clean reinstall of the instance and restoring only user databases from backup, since master corruption at the file level sometimes indicates underlying disk or storage subsystem issues that should be checked with chkdsk or hardware diagnostics.