Sql
SQL Server Operating system error 5 5Access is denied
Encountering the dreaded SQL Server Operating system error 5: “5(Access is denied.)” can bring your database operations to a screeching halt. This persistent error message typically indicates that the SQL Server service account lacks the necessary permissions to access a specific file or resource on the operating system. Whether it’s a critical data file, a log file, or even a temporary database file, the inability to access these components prevents SQL Server from starting, performing backups, or even processing routine transactions. Understanding the root causes and implementing effective solutions is paramount for maintaining a healthy and responsive SQL Server environment. This guide will walk you through diagnosing and resolving these permission-related roadblocks, ensuring your SQL Server instance runs smoothly.
Understanding SQL Server Operating System Error 5: “Access is denied.”
The “Access is denied” error in SQL Server, specifically operating system error 5, is a fundamental security issue. It means the Windows user account under which the SQL Server service is running does not have the necessary privileges to read, write, or modify a file or directory it needs to access. This isn’t just a minor glitch; it’s a clear signal from the operating system that a security boundary has been crossed or, more accurately, not properly configured. This usually happens when SQL Server tries to interact with its data files (.mdf), log files (.ldf), or even the tempdb files, and the service account lacks the appropriate NTFS permissions on the underlying file system.
This error often manifests during SQL Server startup, database attachment, backup/restore operations, or even routine database maintenance tasks. For instance, if you move database files to a new drive and forget to configure the permissions, SQL Server won’t be able to open them. Similarly, if the service account’s privileges are inadvertently revoked or if the service account itself is changed without updating the file system permissions, you’ll inevitably face this error. The SQL Server service account operates within a specific security context, and it’s this context that needs proper authorization at the operating system level.
According to a study by SolarWinds, permission-related issues are among the top five most common causes of SQL Server downtime, emphasizing the importance of correctly managing service account privileges. It’s not enough to just install SQL Server; ongoing vigilance regarding its operational permissions is crucial. When you encounter SQL Server Operating system error 5, your first thought should always be: “What files or directories is SQL Server trying to access, and does its service account have permission to do so?”
Diagnosing the Source of “Access is denied.”
Pinpointing the exact file or resource causing the “Access is denied” error is the first critical step towards resolution. SQL Server often provides clues in its error logs, but sometimes a deeper investigation is required. The SQL Server Error Log is your primary source of information. You can access it through SQL Server Management Studio (SSMS) under Management > SQL Server Logs. Look for entries around the time the error occurred, specifically messages containing “Operating system error 5” or “Access is denied.” These messages often explicitly state the path to the file that SQL Server failed to access.
For example, you might see an entry like: “Error: 17204, Severity: 16, State: 1. FCB::Open: Could not open file C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\YourDatabase.mdf for file number 1. OS error: 5(Access is denied.).” This message directly tells you the problematic file and the nature of the issue. If the error log isn’t specific enough, or if the problem is intermittent, Windows Event Viewer can offer additional insights. Check both the “Application” and “System” logs under “Windows Logs.” Filter for “Error” or “Warning” events from the “MSSQLSERVER” source (or your named instance) or “Service Control Manager” to see if the SQL Server service failed to start due to a permission issue.
In more complex scenarios where the file path isn’t clear, tools like Process Monitor (Procmon) from Sysinternals can be invaluable. Procmon allows you to monitor file system, registry, and network activity in real-time. You can filter its output to show only events related to the SQL Server process (sqlservr.exe) and look for “ACCESS DENIED” results. This can help identify files that SQL Server attempts to access but is blocked from, even if they aren’t directly data or log files, such as backup locations or user-defined file groups. This meticulous diagnostic approach ensures you’re addressing the actual cause rather than just guessing.
Once you’ve identified the problematic files or directories, the next step is to grant the correct NTFS permissions to your SQL Server service account. This is the most common resolution for SQL Server Operating system error 5. The SQL Server service account is the Windows user account (e.g., “NT Service\MSSQLSERVER” for default instance or a domain account like “DOMAIN\SQLServiceAcct”) under which the SQL Server database engine service runs. It’s crucial to grant permissions to this specific account, not just “Everyone” or “Users,” which would be a significant security risk.
Here’s a structured approach to applying the correct permissions:
-
Identify the SQL Server Service Account: Open SQL Server Configuration Manager. Under “SQL Server Services,” locate your SQL Server instance. Note the account listed in the “Log On As” column. This is your target service account. For default instances, it’s often “NT Service\MSSQLSERVER”; for named instances, it’s “NT Service\MSSQL$INSTANCENAME”. If it’s a domain account, use “DOMAIN\UserName”.
-
Locate the Affected Directory/File: Using the information from your diagnosis (SQL Server Error Log or Event Viewer), navigate to the directory containing the file that SQL Server couldn’t access. Examples include the default data directory (e.g.,
C:\Program Files\Microsoft SQL Server\MSSQLXX.MSSQLSERVER\MSSQL\DATA), log directories, or custom data/log file locations. -
Grant Permissions:
- Right-click on the problematic folder (or file, if it’s a single file issue) and select “Properties.”
- Go to the “Security” tab and click “Edit.”
- Click “Add…” to add a new user or group.
- In the “Select Users, Computers, Service Accounts, or Groups” dialog, enter the SQL Server service account name you identified in step 1. For “NT Service\” accounts, you might need to change the “Location…” to your local machine first. Click “Check Names” to verify and then “OK.”
- With the service account selected in the “Group or user names” box, grant “Full control” permissions. While “Modify,” “Read & execute,” “List folder contents,” “Read,” and “Write” are often sufficient for data and log files, “Full control” simplifies troubleshooting and is generally recommended for the main data and log directories.
- Click “Apply” and then “OK” to save the changes. Ensure you apply these permissions to subfolders and files if granting on a directory.
-
Restart SQL Server Service: After modifying permissions, restart the Question & Answer :
I am starting to learn SQL and I have a book that provides a database to work on. These files below are in the directory but the problem is that when I run the query, it gives me this error:Msg 5120, Level 16, State 101, Line 1 Unable to open the physical file “C:\Murach\SQL Server 2008\Databases\AP.mdf”. Operating system error 5: “5(Access is denied.)”.
CREATE DATABASE AP ON PRIMARY (FILENAME = 'C:\Murach\SQL Server 2008\Databases\AP.mdf') LOG ON (FILENAME = 'C:\Murach\SQL Server 2008\Databases\AP_log.ldf') FOR ATTACH GOIn the book the author says it should work, but it is not working in my case. I searched but I do not know exactly what the problem is, so I posted this question.
The SQL Server database engine service account must have permissions to read/write in the new folder.
Check out the blog post Attaching Database – Unable to Open Physical File (Access is Denied)
To fix, I did the following:
Added the Administrators Group to the file security permissions with full control for the Data file (S:) and the Log File (T:).
Attached the database and it works fine.

