MetricSign
Start free
High severityconfiguration

SQL Server Error:
5120

What does this error mean?

Error 5120 fires when the SQL Server Database Engine cannot open a .mdf, .ndf, or .ldf file during an ATTACH DATABASE, CREATE DATABASE FOR ATTACH, or ALTER DATABASE operation. The engine returns 'Unable to open the physical file … Operating system error 5 (Access is denied)' or 'Operating system error 2 (The system cannot find the file specified).' In a data-pipeline context this typically surfaces when an ADF Copy Activity, a linked-server query, or a Power BI gateway refresh tries to hit a database that was recently moved, restored, or re-attached on the SQL Server host. The symptom is immediate: the database stays in 'Recovery Pending' or does not appear at all, and every downstream query fails with a connection-level error.

Common causes

  • 1The .mdf or .ldf file path specified in CREATE DATABASE … FOR ATTACH or sp_attach_db does not exist, is misspelled, or points to a drive letter that is not mounted on this server.
  • 2The SQL Server service account (e.g. NT SERVICE\MSSQLSERVER or a gMSA) does not have read/write NTFS permission on the data file or its parent directory — common after copying files from another machine where the original ACL does not include the local service account.
  • 3The file is exclusively locked by another process: a second SQL Server instance that still has the database attached, a running backup agent (Veeam, Commvault), or real-time antivirus scanning the .mdf during attach.
  • 4The database files are on a network share (UNC path) and the SQL Server service account does not have access to that share, or the share is offline. SQL Server does not support database files on SMB shares unless the share grants full read/write to the service account and the network path is stable.
  • 5SSMS was started without 'Run as Administrator' and the attach dialog sends the command under the interactive user context instead of the SQL Server service context — the engine still runs under its own account, but the error message can be confusing because the file is readable by the logged-in user.

How to fix it

  1. 1Step 1: Confirm the file exists at the exact path. Run: EXEC xp_fileexist 'D:\SQLData\MyDB.mdf'; — column 'File Is a File' returns 1 if found. If 0, check the drive letter and directory spelling.
  2. 2Step 2: Identify the SQL Server service account. Run: SELECT servicename, service_account FROM sys.dm_server_services WHERE servicename LIKE 'SQL Server%'; — note the account name (e.g. NT SERVICE\MSSQLSERVER).
  3. 3Step 3: Grant NTFS permissions. In PowerShell: $acl = Get-Acl 'D:\SQLData'; $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('NT SERVICE\MSSQLSERVER','FullControl','ContainerInherit,ObjectInherit','None','Allow'); $acl.SetAccessRule($rule); Set-Acl 'D:\SQLData' $acl; — this grants Full Control on the directory and all files inside it.
  4. 4Step 4: Check for file locks. In an admin PowerShell prompt run: handle.exe MyDB.mdf (Sysinternals) to see which process holds the file. If another SQL Server instance holds it, detach it there first: USE master; EXEC sp_detach_db 'MyDB';
  5. 5Step 5: If the files are on a UNC share, verify the share permissions separately from NTFS. Run: net use \\fileserver\sqldata — and confirm the SQL Server service account has Change + Read permission on the share. Test with: EXEC xp_fileexist '\\fileserver\sqldata\MyDB.mdf';
  6. 6Step 6: Retry the attach: CREATE DATABASE [MyDB] ON (FILENAME = 'D:\SQLData\MyDB.mdf'), (FILENAME = 'D:\SQLData\MyDB_log.ldf') FOR ATTACH; — if it succeeds, verify the database is ONLINE: SELECT name, state_desc FROM sys.databases WHERE name = 'MyDB';
  7. 7Step 7: After the database is online, test the downstream connection. In ADF: trigger a debug run of the pipeline that reads from this database. In Power BI: go to the gateway settings and run 'Test Connection' for the data source. Confirm the refresh completes without error.

Example log output

Error: 5120, Severity: 16, State: 101. Unable to open the physical file "D:\SQLData\SalesDB.mdf". Operating system error 5: "5(Access is denied.)".
Database 'SalesDB' cannot be opened due to inaccessible files or insufficient memory or disk space. See the SQL Server errorlog for details.
ADF Activity Run failed: ErrorCode=SqlFailedToConnect, Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException, Message=Cannot connect to SQL Server: SalesDB is in recovery pending state.

Frequently asked questions

Why does error 5120 happen even when the file clearly exists?

The most common cause is a permission mismatch — the file exists but the SQL Server service account cannot read it. This often happens when files are copied from another machine and the original NTFS ACL is inherited. Fix by explicitly granting the service account permission.

How do I find the SQL Server service account name?

Open SQL Server Configuration Manager → SQL Server Services → right-click your SQL Server instance → Properties → Log On tab. The account name is shown there (e.g. NT SERVICE\MSSQLSERVER or a domain account).

Can error 5120 occur when moving a database to a new drive?

Yes — after moving .mdf/.ldf files to a new drive, the new location may not have the SQL Server service account in its ACL. Grant Full Control on the new directory before attempting to attach or start the database.

Source · learn.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-5120-database-engine-error

Other configuration errors