SQL Server Error 5120 ("Unable to open the physical file: 'filename.mdf'") occurs when the system fails to access the underlying disk or file where your database data or log files are stored. This blocks all database operations, from login attempts to queries, and requires careful handling to avoid permanent data loss. The error typically stems from disk space exhaustion, file permission issues, corrupt file structures, or filesystem corruption—none of which can be resolved with a simple restart.
What You'll Need
- Administrative access to the server or workstation running SQL Server
- Backup access (if the database is critical and no recovery exists)
- Disk space to temporarily move files (if needed)
- SQL Server Management Studio (SSMS) or query tools (e.g., cmd.exe for Basic SQL)
- A secondary drive or network share with free space to relocate files (optional)
- Knowledge of the database file locations (check SQL Server Error Log for paths)
Step-by-Step Guide to Resolve Error 5120
- Verify the exact error details
- Open SQL Server Error Logs by running in SSMS:
SELECT @@ERROR, XEventData FROM sys.fn_xe_file_target_read_file('C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\LOG\SQLServer.xel', NULL, NULL, NULL, NULL, NULL) - Identify the missing file (e.g., 'D:\SQLData\YourDB.mdf' or 'D:\SQLLogs\YourDB_log.ldf') and note its permissions.
- Check disk space on the affected drive using:
wmic logicaldisk get size,freespace,caption - If files are in use, check for:
- SQL Server services (right-click Task Manager > Services > SQL Server)
- Open processes listing Disk I/O (Task Manager > Performance tab)
- Open SQL Server Error Logs by running in SSMS:
- Restart the SQL Server service
- Pause the SQL Server service via Services.msc or:
net stop MSSQLSERVER - Wait 30 seconds, then restart:
net start MSSQLSERVER - If the error persists, restart the entire server (shutdown and reboot) to clear lockouts.
- Pause the SQL Server service via Services.msc or:
- Check file permissions and ownership
- Open File Explorer and navigate to the database file location (e.g., D:\SQLData). Right-click the file > Properties > Security.
- Ensure the 'NT Service\SQLSERVER' account has Read & Execute, List Folder Contents permissions.
- If permissions are misconfigured:
- Take ownership via
takeown /f "D:\SQLData\YourDB.mdf" - Grant permissions to the SQL Server service account (run as Administrator).
- Take ownership via
- If the file is owned by the wrong user, assign it to the SQL Server service account (e.g., SQLWorker$SERVERNAME).
- Check for filesystem corruption
- Run CHKDSK on the drive (right-click volume > Properties > Tools > Check).
- For critical drives, use the /R option to scan for bad sectors:
chkdsk C: /R /Fchkdsk C: /F (press Y when prompted) - If corruption is detected, restore from backup or recreate the file.
- If the file is severely corrupted and no backup exists:
- Create an empty file in the same location (e.g., D:\SQLData\YourDB.mdf).
- Set the file size to the original size using PowerShell:
New-Item -Path "D:\SQLData\YourDB.mdf" -ItemType File -Length 5000000000 (5GB example) - Attach a new database via SSMS:
- Right-click databases > Attach.
- Select the empty file and add it to the new database.
- Wait for SQL Server to initialize the file (may take minutes).
- Add the database to emergency recovery mode (if no backup exists):
- Force the database to accept the corrupted file (SQL Server will attempt to repair).
- Check the log for errors; if no recovery is possible, recreate the file.
ALTER DATABASE YourDB SET EMERGENCY
Common Mistakes
- Ignoring disk space exhaustion: SQL Server cannot allocate space for files. Free up space before attempting fixes.
- Forgetting to restart services: Some fixes require restarting SQL Server or the OS to take effect.
- Attempting repairs without backups: If the file is corrupted beyond repair, recreating it risks data loss if no backup exists.
- Inappropriate permissions: Granting too broad access (e.g., 'Full Control') to system accounts can introduce security risks.
- Skipping filesystem checks: Corrupt filesystems can cause cascading errors.
Tips for Prevention
- Set up alerts for disk space approaching 80% usage.
- Use SQL Server Agent to regularly back up critical databases.
- Enable SQL Server's filegroup separation for load-balanced writes.
- Monitor filesystem health with tools like Windows Disk Manager or third-party utilities.
- Avoid writing large transactions during filesystem operations (e.g., CHKDSK).
Troubleshooting If Steps Fail
- Error persists after CHKDSK: The drive may be physically damaged
- Test the drive on another machine or in a desktop PC.
- If corrupted, replace the drive and restore from backup.
- Files still inaccessible despite permissions fixes
- Check for antivirus real-time scanning blocking access (temporarily disable during repair).
- Run SQL Server Configuration Manager > check if the Data Directories are correctly mapped.
- SQL Server crashes after recreating files
- Verify file sizes match those in SQL Server Configuration Manager.
- Check the SQL Server Error Log for additional errors (e.g., file locking conflicts).
- Database is locked by another user
- Force-close the session via SSMS: right-click the database > Properties > Options > Set recovery model to SIMPLE.
- Wait 5 minutes for locks to release.
Frequently Asked Questions
Why does SQL Server error 5120 keep appearing?
Error 5120 typically recurs due to underlying filesystem issues (e.g., bad sectors, permission conflicts) or persistent disk space constraints. Ensure your filesystem is healthy, drive permissions are correct, and no processes are locking files. Check both SQL Server Error Logs and Windows Event Viewer for related disk I/O errors.
Can I use a third-party tool to fix corrupted database files?
While tools like DB Repair or SQL Diagnostic Manager claim to fix corruption, Microsoft does not endorse third-party utilities for database recovery. Instead, recreate the file (as shown in Step 5) or restore from backup—third-party tools may not handle SQL Server's proprietary file formats correctly and risk further damage.
What if I have no backup and the file is corrupted?
If no backup exists and the file is beyond recovery, you must recreate it. Start by creating an empty file in the same location, setting its size to match the original, and attaching it to a new database. This is a last-resort step—data will be lost if the file was never backed up.
How do I prevent this error in the future?
Maintain sufficient disk space (aim for 10–20% free), regularly back up databases, monitor filesystem performance, and avoid writing large transactions during filesystem operations. Enable SQL Server's filegroup separation and consider using a dedicated storage tier for database files.
My database file is on an external drive. Should I move it?
If the external drive is unreliable or fails frequently, relocate the database files to a local drive first. Move them to a secondary drive with 3x the required space. Ensure the destination drive meets SQL Server's minimum performance requirements (e.g., 16GB+ RAM if handling large databases).
What if the error occurs during a transfer to a new server?
When transferring databases, verify the destination drive's permissions and filesystem health before attaching. Use the ALTER DATABASE YourDB SET SINGLE_USER command to prevent concurrent access during transfer. If the error persists, check the transfer tool's logging for filesystem issues.