How to Fix SQL Server Error 5120 (Msg 5120)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get sql server error 5120 when the database engine cannot open a physical file. You fix it by granting the database service account file rights on that folder. This failure blocks database attach tasks, restores, and startups. It is a severity 16 error from the database engine. That level means the user or admin can fix the problem. The message states that the engine cannot open the physical file. It also shows a specific operating system error code and message. What causes sql server err

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get sql server error 5120 when the database engine cannot open a physical file. You fix it by granting the database service account file rights on that folder.

This failure blocks database attach tasks, restores, and startups. It is a severity 16 error from the database engine. That level means the user or admin can fix the problem. The message states that the engine cannot open the physical file. It also shows a specific operating system error code and message.

What causes sql server error 5120 in the engine

The database engine runs as a specific operating system service account. When you attach or restore a database, this service account opens the data files. It also opens the log files on the storage volume. If the operating system blocks the service account, the engine throws code 5120.

This issue often happens when a user copies files from another folder. A file moved from a personal desktop keeps the old user file rules. The database service account does not have rights to read or write there. Network shares and new storage drives also cause this file problem. If a second instance locks the file, the operating system also blocks access.

Sometimes admins confuse operating system file rights with database roles. Inside the server, a login connects and maps to a database user. Admins often use fixed roles to control data queries. For example, the datareader role lets a user read all user tables and views. The datawriter role lets a user add, delete, or change table data. Still, internal database roles do not control physical file access on the disk.

How to diagnose and fix the failure safely

First, read the full error message to find the exact file path. Look at the operating system error code inside the message text. This code tells you if the file is missing or locked. Most of the time, the code points to an access denied error.

Next, check which service account runs the database engine on your host. Open your host service manager to see the exact account name. Then check the folder that holds the data and log files. Make sure the file is not open in another program or instance.

Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix the problem, grant file access to the database service account. Give the service account read and write rights on the target folder. Do not grant full disk control to every user on the machine. Giving everyone full rights on the folder creates a huge security risk. Keep file access restricted to the specific engine service account and system admins.

How to prevent file errors during database operations

You can stop this error before it happens by using standard data folders. When you set up the server, the installer sets safe folder rules. Store new data and log files inside those default database directories. The engine service account already has the right access in those paths.

When you move files between hosts, check folder rights before running attach tasks. You should also keep human users off the database host operating system. Most team members only need to read or write data inside tables. Give them a standard SQL login mapped to a database user. For access to a few databases, you can use a contained database user. This keeps engineers out of the host file system and prevents broken file rules.

Add runtime guardrails beside SQL Server

Role grants and network rules are needed setup for SQL Server. Still, an allowed user or agent tool can send a bad delete or read raw customer fields over a valid session. In practice, hoop.dev sits inline in the TDS protocol beside the database as a sidecar for your SQL Server. For every query an agent or human runs in production, hoop.dev decides whether to pass, mask, block, or escalate it for review.

With hoop.dev SQL Server controls, rules refuse banned statements before they run, hold risky queries for human review, and mask matched sensitive values in supported result columns in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for HIPAA, PCI DSS, SOX, and GDPR audits. See the source on GitHub.

Frequently asked questions

Does the database owner role fix error 5120?

No, the database owner role only controls actions inside the database engine. You must fix operating system file permissions for the server service account.

Why does error 5120 happen when I attach a file from my desktop?

Your personal desktop folder only grants file access to your own user account. Move the database file to the standard data folder so the engine can read it.

Can a locked file trigger message 5120?

Yes, another database instance or backup tool might hold a lock on the file. Check the operating system error code in the message to confirm a file lock.

The query runs. The emails stay hidden.