Troubleshooting Guide: SQL Server Error 5139
Why This Error Happens
Error 5139 occurs when SQL Server attempts to perform a file operation (such as creating, attaching, or growing a database file) and the underlying Windows operating system rejects the request. This is most commonly caused by insufficient NTFS permissions for the SQL Server service account, a file path that no longer exists or is misspelled, or the target file/disk being locked, offline, or read-only. In clustered or storage-replica environments, this can also surface when the disk volume hosting the data file has failed over or lost connectivity.
Step-by-Step Resolution Guide
Step 1: Verify the File Path and Disk Availability
- Open File Explorer or run
dirfrom Command Prompt to confirm the file path referenced in the error (e.g.,D:\SQLData\MyDB.mdf) actually exists. - Check Disk Management (
diskmgmt.msc) to ensure the volume is online, not read-only, and has sufficient free space. - If using mount points or SAN storage, confirm the LUN is attached and visible to Windows Server 2025.
Step 2: Correct NTFS and Service Account Permissions
- Open the properties of the folder containing the database files, go to the Security tab, and confirm the SQL Server service account (e.g.,
NT Service\MSSQLSERVERor a custom domain account) has Full Control. - Use PowerShell to quickly audit permissions:
Get-Acl "D:\SQLData" | Format-List
- If permissions are missing, grant access via:
icacls "D:\SQLData" /grant "NT SERVICE\MSSQLSERVER:(OI)(CI)F" /T
Step 3: Check for File Locks or Conflicting Processes
- Use Resource Monitor (
resmon.exe) orhandle.exe(Sysinternals) to confirm no other process (antivirus, backup agent, or another SQL instance) has the file locked. - Temporarily disable real-time antivirus scanning on the SQL data/log directories to rule out interference.
- Restart the SQL Server service after confirming no locks exist:
net stop MSSQLSERVER
net start MSSQLSERVER
Step 4: Review Windows Event Viewer for the Underlying OS Error Code
- Open Event Viewer > Windows Logs > System and Application to find the specific Windows error number referenced in the SQL error message (e.g., Error 5 = Access Denied, Error 32 = Sharing Violation).
- Cross-reference the OS error code with Microsoft’s documentation to pinpoint whether it’s a permissions, locking, or hardware-level issue.
- Resolve based on the specific code (e.g., re-grant permissions for Error 5, release locks for Error 32).
Alternative or Verification Steps
- After applying fixes, attempt the original operation again (e.g.,
CREATE DATABASE,ALTER DATABASE ... MODIFY FILE, or restore) and confirm it completes without Error 5139. - If the issue persists, run
DBCC CHECKDBand review the SQL Server Error Log (sqlerrlog) for additional context, or move the database files to a different, verified-healthy volume as a workaround.