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

Troubleshooting Guide: SQL Server Error 5125 – “File ‘%ls'”

Why This Error Happens

Error 5125 occurs when SQL Server 2025 attempts to access a database file (.mdf, .ndf, or .ldf) that either does not exist at the specified path, is locked by another process, or cannot be read due to insufficient NTFS permissions for the SQL Server service account. This commonly happens after migrating databases between servers, restoring from a backup with an incorrect file path, or when the underlying storage volume has been disconnected or renamed. In some cases, the file header itself is corrupted, preventing SQL Server from validating it as a legitimate database file.

Step-by-Step Resolution Guide

Step 1: Verify the File Path and Existence

  • Open File Explorer or PowerShell and navigate to the exact path referenced in the error message.
  • Run Test-Path "D:\SQLData\YourDatabase.mdf" in PowerShell to confirm the file exists.
  • Check for typos in the path if the database was recently attached or restored using a MOVE clause in RESTORE DATABASE.

Step 2: Check NTFS Permissions on the File and Folder

  • Right-click the data/log folder (e.g., D:\SQLData\), go to Properties > Security.
  • Ensure the SQL Server service account (e.g., NT Service\MSSQLSERVER or a custom domain account) has Full Control or at minimum Read/Write/Modify permissions.
  • Use icacls "D:\SQLData" /grant "NT SERVICE\MSSQLSERVER":(OI)(CI)F in an elevated Command Prompt to apply correct permissions recursively.

Step 3: Confirm the File Is Not Locked by Another Process

  • Open Task Manager or Resource Monitor and check if another instance of SQL Server, an antivirus scanner, or a backup tool has the file open.
  • Use Handle.exe (Sysinternals) with handle64.exe YourDatabase.mdf to identify the locking process.
  • Stop the conflicting process or add an antivirus exclusion for the SQL Server data directory (D:\SQLData\*.mdf, *.ldf, *.ndf).

Step 4: Rebuild or Reattach the Database Correctly

  • If the file path changed, run ALTER DATABASE YourDB MODIFY FILE (NAME = YourDB_Data, FILENAME = 'D:\SQLData\YourDB.mdf') to update the catalog reference.
  • Restart the SQL Server service via services.msc or Restart-Service MSSQLSERVER for changes to take effect.
  • If the file is corrupted, attempt DBCC CHECKDB('YourDB') WITH NO_INFOMSGS, ALL_ERRORMSGS to assess damage.

Alternative or Verification Steps

  • Run sp_helpdb YourDB or query sys.master_files to confirm SQL Server now recognizes the correct file path and status is ONLINE.
  • If permissions and paths are correct but the error persists, restore the database from the most recent verified backup using RESTORE DATABASE YourDB FROM DISK = 'D:\Backups\YourDB.bak' WITH REPLACE.