Troubleshooting Guide: SQL Server Error 5179 — “Cannot use file ‘%.*ls'”
Why This Error Happens
Error 5179 occurs when SQL Server 2025 attempts to mount a primary data file (.mdf) that is either corrupted, truncated, or does not match the header information SQL Server expects for a valid database file. This frequently happens after an improper file copy, an incomplete disk transfer, antivirus interference, or if the file was mistakenly renamed from a secondary (.ndf) or log (.ldf) file into a primary data file role. It can also surface when NTFS permissions prevent the SQL Server service account from reading the full file header on Windows Server 2025 or Windows 11.
Step-by-Step Resolution Guide
Step 1: Verify File Integrity and Header Information
- Confirm the file wasn’t copied using a method that breaks sparse/NTFS attributes (avoid simple drag-and-drop over network shares).
- Use
fsutil file queryextents <path-to-mdf>in an elevated Command Prompt to check if the file structure is intact. - Compare the file size against the original source file (if available) to rule out truncation during transfer.
Step 2: Check NTFS Permissions for the SQL Server Service Account
- Navigate to the folder location (e.g.,
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA). - Right-click the file, select Properties > Security, and ensure the service account (e.g.,
NT Service\MSSQLSERVER) has Full Control. - Run the following in PowerShell to grant permissions if missing:
icacls "C:\SQLData\YourDatabase.mdf" /grant "NT Service\MSSQLSERVER:(F)"
Step 3: Confirm the File Is Not Being Blocked by Antivirus or Windows Defender
- Open Windows Security on Windows Server 2025 / Windows 11 and add an exclusion for the SQL Server Data directory.
- Use PowerShell to add an exclusion directly:
Add-MpPreference -ExclusionPath "C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA"
- Restart the SQL Server service via
services.mscand attempt to reattach the database.
Step 4: Attempt a Database Attach Using T-SQL with Correct File Mapping
- Ensure you are referencing the correct .mdf as PRIMARY and not accidentally pointing to an .ndf file.
- Run the following query, substituting your actual file paths:
CREATE DATABASE YourDB
ON (FILENAME = 'C:\SQLData\YourDB.mdf')
FOR ATTACH;
- If the log file (.ldf) is missing, use
FOR ATTACH_REBUILD_LOGinstead (only for user databases, not system databases).
Step 5: Restore From Backup if the File Is Confirmed Corrupted
- If Steps 1–4 fail and the header is unreadable, the .mdf file is likely corrupted beyond repair.
- Restore the most recent valid
.bakfile usingRESTORE DATABASE YourDB FROM DISK = 'C:\Backups\YourDB.bak'.
Alternative or Verification Steps
- Verify the fix by running
sp_helpdb YourDBor checking SQL Server Management Studio (SSMS) > Databases to confirm the database shows status “Online” rather than “Suspect” or “Recovery Pending.” - If attaching still fails, use
DBCC CHECKDB('YourDB') WITH PHYSICAL_ONLYafter a successful restore to rule out any underlying disk-level corruption before resuming production use.