Skip to content
Microsoft SQL Server

SQL Server Error 5120

Unable to open the physical file (operating system error)

Applies to: All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
Last technically reviewed 23 October 2026 on SQL Server 2025 (17.0.1145.4)
The full message
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "D:\SqlData\AppDb.mdf". Operating system error 2: "2(The system cannot find the file specified.)".
Other forms of this message
Followed by (CREATE DATABASE ... FOR ATTACH)
Msg 1802, Level 16, State 7, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Followed by (ALTER DATABASE ... SET ONLINE, reproduced on SQL Server 2025)
Msg 5181, Level 16, State 5, Line 1
Could not restart database "AppDb". Reverting to the previous status.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.
Permission problem, as documented by Microsoft (operating system error 5)
Unable to open the physical file "\\servername\sharename\filename.mdf". Operating system error 5: "5(Access is denied.)".
Folder does not exist (a different error, reproduced on SQL Server 2025)
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "C:\no_such_folder\AppDb.mdf" failed with the operating system error 2(The system cannot find the file specified.).
Error number
5120
Severity
16 (user-correctable error)
State
101
Product
Microsoft SQL Server Database Engine
Versions
All supported SQL Server versions (2016 to 2025), Azure SQL Managed Instance
Last reviewed
23 October 2026

What it means

Error 5120 means SQL Server tried to open an existing database file (to attach it, bring a database online or start it) and Windows refused; the operating system error number in the message says why.

SQL Server opens database files itself, on the server, as the SQL Server service account (for an attach, sometimes while impersonating your Windows login). When Windows returns an error, SQL Server reports 5120 with the path and the operating system error, and the statement that needed the file fails: an attach is followed by 1802, a SET ONLINE by 5181 and 5069. At service startup the same failure leaves the database unusable, and every later attempt to use it fails with error 945.

Read the number after Operating system error: 2 means the file is not at that path, 5 means access is denied (the most common case after copying files from another server or a user folder), and 32 means another process has the file open. If the folder itself does not exist, SQL Server reports 5133 instead.

Common causes

  • The path is wrong or the file was moved, renamed or deleted (OS error 2). Remember the path is on the server, not on your workstation.
  • The SQL Server service account (or your impersonated login) has no permission on the file, for example .mdf files copied from another server, downloaded, or kept in a user profile folder (OS error 5).
  • A file that was detached keeps restrictive permissions, so reattaching it from a network share fails (OS error 5, documented by Microsoft).
  • The file is open in another process: another instance still has it attached, or a backup or antivirus tool holds it (OS error 32).
  • The file is marked read-only or is on read-only or compressed media.

Diagnose it

Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.

Which account opens the files?

SELECT servicename, service_account
FROM sys.dm_server_services;
What to look for

The account of the SQL Server service. It needs read and write permission on the file and its folder; compare with the permissions on the instance default data folder.

Where are the files the database expects?

SELECT DB_NAME(database_id) AS database_name, type_desc, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'AppDb');
What to look for

For an existing database, the exact paths SQL Server opens. Compare them with where the files really are on the server.

Read every 5120 in the error log

EXEC sp_readerrorlog 0, 1, N'Unable to open the physical file';
What to look for

Each failed open with its path and operating system error, including failures at service startup that no client saw.

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Correct the path

Client or network side

For OS error 2, check on the server that the file exists at exactly the path in the message, and fix typos or the drive letter. For an existing database whose files were moved, point the catalogue at the new path with ALTER DATABASE ... MODIFY FILE and then SET ONLINE (see error 945).

Give the service account access to the file

Changes permissions

For OS error 5, grant the SQL Server service account Full control on the .mdf, .ndf and .ldf files and their folder, or move the files into the instance data folder, which already has the right permissions. Remove the read-only attribute if it is set.

Caution: Grant access to the service account only. Opening database files to Everyone or Users exposes all the data in them.

Release the file from the other process

Client or network side

For OS error 32, find what has the file open: another SQL Server instance (detach it there first), a copy still running, or an antivirus or backup agent. Exclude database files from antivirus scanning.

Retry the attach

Changes database state

Once the files are in place and accessible, attach them again, listing the data file and the log file.

CREATE DATABASE AppDb
ON (FILENAME = N'D:\SqlData\AppDb.mdf'),
   (FILENAME = N'D:\SqlData\AppDb_log.ldf')
FOR ATTACH;

Examples

Attaching a file that is not in the folder

-- C:\Users\Public exists, but AppDbArchive.mdf is not in it
CREATE DATABASE AppDbArchive
ON (FILENAME = N'C:\Users\Public\AppDbArchive.mdf')
FOR ATTACH;
Result
Msg 5120, Level 16, State 101, Line 2
Unable to open the physical file "C:\Users\Public\AppDbArchive.mdf". Operating system error 2: "2(The system cannot find the file specified.)".
Msg 1802, Level 16, State 7, Line 2
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.

How to prevent it

Keep database files in folders created for SQL Server (the instance default data and log folders, or folders given the same permissions), never in user profiles or download folders. When moving databases between servers, prefer BACKUP and RESTORE over copying files: a restore creates files with the right owner and permissions.

Change the service account only through SQL Server Configuration Manager, which updates file permissions, and alert on databases that are not ONLINE after a restart.

Common questions

Why do I get access denied when I can open the file myself?

SQL Server opens the file as its service account (or, for some attach operations, by impersonating your login), on the server. Your own access in Explorer does not count; give the service account permission on the file and folder.

Can I attach a .mdf file without its .ldf file?

Only if the database was shut down cleanly; then CREATE DATABASE ... FOR ATTACH_REBUILD_LOG can create a new log. If the database was not shut down cleanly, the missing log means lost transactions, so restore from backup instead.

What is the difference between 5120 and 5133?

Error 5120 is raised when an existing file cannot be opened. Error 5133 is raised when the folder in the path cannot be looked up, for example because it does not exist.

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.

Still stuck on a SQL Server error?

Browse the full SQL Server error library, or get hands-on help from an experienced SQL professional.