Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Operating system error 5: "5(Access is denied.)"

Tags:

sql

sql-server

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
    GO

In 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.

like image 780
dijai jijfs Avatar asked Oct 03 '22 21:10

dijai jijfs


People also ask

Could not open connection to SQL Server error 5 access is denied?

The problem is due to lack of permissions for SQL Server to access the mdf & ldf files. All these procedures will work : you can directly change the MSSQLSERVER service startup user account, with the user account who have better privileges on the files. Then try to attach the database.

What is operating system error 5?

System error 5 is an error access denied code which means that your access is denied because you don't have the required privileges to run a certain command. For example, when you try to run a command which is supposed to stop the print spooler, you will get an error message.


2 Answers

SQL Server database engine service account must have permissions to read/write in the new folder.

Check out this

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.

enter image description here

enter image description here

like image 150
Rahul Tripathi Avatar answered Oct 08 '22 21:10

Rahul Tripathi


An old post, but here is a step by step that worked for SQL Server 2014 running under windows 7:

  • Control Panel ->
  • System and Security ->
  • Administrative Tools ->
  • Services ->
  • Double Click SQL Server (SQLEXPRESS) -> right click, Properties
  • Select Log On Tab
  • Select "Local System Account" (the default was some obtuse Windows System account)
  • -> OK
  • right click, Stop
  • right click, Start

Voilá !

I think setting the logon account may have been an option in the installation, but if so it was not the default, and was easy to miss if you were not already aware of this issue.

like image 139
MickeyfAgain_BeforeExitOfSO Avatar answered Oct 08 '22 20:10

MickeyfAgain_BeforeExitOfSO