Quick Answer
SQL Server error 5120 means the Database Engine could not open a database file. The last line of the message gives an operating system error number and that number names the cause. Error 2 and 3 point to a wrong file name or path. Error 5 points to missing access on the file. Error 32 points to a file another program has open. Our test returned error 5 for a file that an online database already used, so check for a second attach before you edit any permission.
Error 5120 stops an attach, a restore or the start of one database and the first line looks the same in all three cases. Only the operating system error at the end tells you what to fix.
This guide shows the exact text we got on SQL Server 2022, which operating system error maps to which fix and the statements that prove every cause. You can finish the check in a few minutes with tools you already have.
What Does SQL Server Error 5120 Look Like?
We executed an attach for an MDF path that does not exist. SQL Server returned error 5120 and then error 1802.
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "/var/opt/mssql/data/nothere.mdf". Operating system error 2: "2(The system cannot find the file specified.)".
Msg 1802, Level 16, State 7, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Error 1802 only says the attach failed. Error 5120 carries the cause. Microsoft notes that errors 17204 and 17207 appear alongside 5120 on the same file.
Which Operating System Error Sits Behind Your 5120?
| OS error | Windows text | What it means | What to do |
|---|---|---|---|
| 2 | The system cannot find the file specified | The file name is wrong or the file moved | Use Method #1 |
| 3 | The system cannot find the path specified | A folder in the path does not exist | Use Method #1 |
| 5 | Access is denied | The service account lacks rights on the file, or the file already belongs to an online database | Use Method #2 |
| 32 | The process cannot access the file because it is being used by another process | Another program has the file open | Use Method #3 |
Microsoft’s error 5120 page does not define these numbers. They are standard Windows system error codes. The same page names a fifth case, an attach from a network share, which Method #4 covers.
Which Permissions and Versions Apply?
System Specifications
| Component | Requirement |
|---|---|
| Account that opens the file | The SQL Server service startup account or the service SID |
| Where to find that account | Windows Task Manager, User Name column of sqlservr.exe |
| Folder permission group | SQLServerMSSQLUser$ComputerName$MSSQLSERVER for a default instance and SQLServerMSSQLUser$ComputerName$InstanceName for a named instance |
| File location | Local server, a SAN or an iSCSI network according to Microsoft |
Software Specifications
| Item | Requirement |
|---|---|
| Your permission to attach | CREATE DATABASE, CREATE ANY DATABASE or ALTER ANY DATABASE |
| Attach statement | CREATE DATABASE ... FOR ATTACH. Microsoft states the attach sets permissions on the data and log files |
| Tool to find the account that opens a file | Process Monitor, which Microsoft names for this check |
| Tested on | SQL Server 2022 (RTM CU26) Express on Linux |
How do You Fix SQL Server Error 5120?
Method #1: Fix the File Name or Path (OS Error 2 or 3)
Use this method when the message ends with error 2 or 3. SQL Server opens the file on the server, so check the path on the server and not on the PC that opens SSMS.
- Copy the exact path from the error text.
- Open that folder on the server and confirm the file name and the extension.
- Pass the new path in the attach statement when the file moved. Microsoft states you need a file entry only for files whose path differs from the last attach.
CREATE DATABASE YourDB
ON (FILENAME = N'D:\Data\YourDB.mdf'),
(FILENAME = N'E:\Logs\YourDB_log.ldf')
FOR ATTACH;
Check the registered paths of a database that is already attached with this query.
SELECT name, physical_name, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'YourDB');
When the error names the LDF file and the database shut down cleanly, you can attach without that file. Microsoft states FOR ATTACH rebuilds a single missing log file in that case. Our guide on how to attach an MDF file without an LDF file shows the options.
Method #2: Give the SQL Server Account Access (OS Error 5)
Use this method when the message ends with error 5 and no other database uses the file. First find the account that sqlservr.exe uses in Task Manager.
- Open the properties of the file and select the Security tab.
- Add the SQL Server service account or the SQLServerMSSQLUser group of your instance.
- Give it read and write access to the file and its folder.
- Execute the attach again.
Microsoft states the account that touches the file changes with the operation. Startup, attach and restore can use a different account. Open Process Monitor to see which account fails.
Check for a Second Attach First
We attached a detached database and then attached the same MDF a second time. SQL Server returned error 5120 with OS error 5 and the text Access is denied, then error 1802. The first attach was online and the same service account made both attempts, so permissions were not the cause. Execute the query below before you edit any permission.
SELECT d.name, d.state_desc, mf.physical_name
FROM sys.master_files AS mf
JOIN sys.databases AS d ON d.database_id = mf.database_id
WHERE mf.physical_name LIKE N'%YourDB%';
A row in the result means a database already uses the file. Detach that database or use a different file copy.
Method #3: Close the Program That Has the File Open (OS Error 32)
Use this method when the message ends with error 32. Windows reports this error when another process has the file open.
- Open Resource Monitor, select the CPU tab and search the name of the MDF file in Associated Handles.
- Stop the program that shows up. A backup agent, a copy job or a second SQL Server instance are the usual owners.
- Execute the attach again.
Method #4: Attach From a Network Share (State 101 on a Share)
Microsoft states SQL Server resets file permissions on detach. A reattach from a share with limited permissions can then fail with error 5120. Microsoft documents this fix.
- Start SQL Server with trace flag 1802 as the
-T1802startup option. - Detach the database.
- Attach it again with
sp_attach_db.
EXEC sp_detach_db N'YourDB';
EXEC sp_attach_db N'YourDB', N'\\server\share\YourDB.mdf', N'\\server\share\YourDB_log.ldf';
Move the files to a local disk when you can. Microsoft lists local server, SAN and iSCSI as the supported locations for attached files.
How do You Confirm the Attach Worked?
Query the database state. Our first attach in the test returned ONLINE.
SELECT name, state_desc FROM sys.databases WHERE name = N'YourDB';
When the file opens and a different error follows, for example 5171 or 824, the problem moved from access to file damage. Our guides on error 5171 and on how to recover data from a corrupt MDF file cover that next step.
FAQs
What Does Operating System Error 5 Mean in Error 5120?
It means access denied. SQL Server tried to open the file and the operating system refused. The cause is a missing permission for the service account or a file that another database already uses, as our test showed.
Is Error 5120 a Corruption Error?
No. Error 5120 reports that SQL Server could not open the file. Corruption errors like 824 appear after SQL Server opens the file and reads a bad page.
Why Does Error 5120 Appear When the File Exists?
The file exists but SQL Server cannot open it. Check three things in order. A second database can already use the file, the service account can lack access or another program can have the file open.
Does Error 5120 Always Come With Error 1802?
Not always. Error 1802 appeared in both of our attach tests because the attach failed. Microsoft notes that errors 17204 and 17207 also appear alongside 5120.
Can You Attach a Database Without the Log File After a 5120 on the LDF?
Yes when the database shut down cleanly. Microsoft states FOR ATTACH rebuilds a single missing log file of a read and write database. A read only database needs its log files.
About this guide
We checked this guide against Microsoft documentation on 2 Dec 2026. We executed the attach statements for a missing file and for a file that an online database already used on SQL Server 2022 (RTM CU26) Express on Linux.
Microsoft documents the error on its error 5120 page and the attach options on its CREATE DATABASE page.
