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:MurachSQL Server 2008DatabasesAP.mdf». Operating system error
5: «5(Access is denied.)».
CREATE DATABASE AP
ON PRIMARY (FILENAME = 'C:MurachSQL Server 2008DatabasesAP.mdf')
LOG ON (FILENAME = 'C:MurachSQL Server 2008DatabasesAP_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.
MAbraham1
1,7174 gold badges28 silver badges45 bronze badges
asked Aug 17, 2013 at 8:30
1
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.
answered Aug 17, 2013 at 8:37
Rahul TripathiRahul Tripathi
167k31 gold badges277 silver badges331 bronze badges
12
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.
answered Feb 17, 2016 at 18:38
10
To get around the access denied issue, I started SSMS as administrator and that allowed me to attach a database from my local drive. The database was created in another SQL and windows instance.
answered Feb 23, 2015 at 11:13
JayKayOf4JayKayOf4
1,2021 gold badge12 silver badges15 bronze badges
4
This is Windows related issue where SQL Server does not have the appropriate permission to the folder that contains .bak file and hence this error.
The easiest work around is to copy your .bak file to default SQL backup location which has all the necessary permissions. You do not need to fiddle with anything else. In SQL SERVER 2012, this location is
D:Program FilesMicrosoft SQL ServerMSSQL11.MSSQLSERVERMSSQLBackup (SQL 2012)
C:Program FilesMicrosoft SQL ServerMSSQL12.MSSQLSERVERMSSQLBackup (SQL 2014)
C:Program FilesMicrosoft SQL ServerMSSQL13.SQLEXPRESSMSSQLBackup (SQL 2016)
answered Oct 9, 2014 at 8:25
TheTechGuyTheTechGuy
16.4k16 gold badges114 silver badges135 bronze badges
6
I had this problem. Just run SQL Server as administrator
Adrian Mole
49.5k155 gold badges49 silver badges79 bronze badges
answered Jul 5, 2017 at 0:55
3
Yes,It’s right.The first you should find out your service account of sqlserver,you can see it in Task Manager when you press ctrl+alt+delete at the same time;Then,you must give the read/write privilege of «C:MurachSQL Server 2008Databases» to the service account.
answered Aug 17, 2013 at 8:48
TimTim
1491 silver badge9 bronze badges
0
I solve this problem by adding Full control
permission for both .mdf
and .ldf
files for Users
group.
answered Dec 2, 2016 at 15:26
1
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.
- Or you can assign the user to the file in security tab of the mdf & ldf files properties with read and and write privileges checked.
- Startup with windows administrator account, and open SQL Server with run as administrator option and try to login with windows authentication and now try to attach the database.
Manu
4,3746 gold badges43 silver badges77 bronze badges
answered Mar 13, 2015 at 12:12
For me it was solved in the following way with SQL Server Management studio
-Log in as admin (I logged in as windows authentication)
-Attach the mdf file (right click Database | attach | Add )
-Log out as admin
-Log in as normal user
answered May 23, 2016 at 13:03
0
The actual server permissions will not matter at this point; all looks ok.
SQL Server itself needs folder permissions.
depending on your version, you can add SERVERNAME$MSSQLSERVER permissions to touch your folder. Othewise, it has to be in the default BACKUP directory (either where you installed it or default to c:programfiles(x)MSSQLBACKUP.
answered Sep 11, 2014 at 19:27
ClaudiaClaudia
511 silver badge1 bronze badge
Even if you do the following steps you COULD get the same error message.
1. login as SA user (SSMS)
2. Edit the file permissions to say "everyone" full access (windows folder)
3. Delete the Log file (Windows Exploring (this was what I had done per advise from some msdn forum)
I still GOT the permission error, but then I noticed that in the Attach screen, the bottom section STILL showed the LOG file, and the error message remained the same.
Hope this helps someone who did the same thing.
answered May 21, 2014 at 0:18
Tom StickelTom Stickel
19.5k6 gold badges111 silver badges113 bronze badges
It means the SSMS login user does not have permission on the .mdf file. This is how it has worked for me:
I had opened the SSMS (Run as administrator) and login as an administrator user, database right-click attach, click add, select the .mdf file, click Ok. Done.
answered Jul 15, 2020 at 22:44
GoldfishGoldfish
5845 silver badges11 bronze badges
I had this issue when I try to backup a database.
System.Data.SqlClient.SqlError:
Cannot open backup device 'C:xxxxx.bak'.
Operating system error 5 (Access is denied.). (Microsoft.SqlServer.Smo)
When I had this issue I thought that the user which I’m connecting to database don’t have rights to access to the backup location. I gave full control to that user but nothing changed. This is because the service for SQL Server is running with another user.
At this point you may choose changing user of the service to local system account or add access rights for the current user of the service.
I choose the 2nd one.
After that change backup succeeded.
answered May 20, 2022 at 9:47
Ozan BAYRAMOzan BAYRAM
2,6871 gold badge27 silver badges34 bronze badges
1
Very Simple Solution.
- Login with System admin
- copy your mdf and ldf files in «C:Program Files (x86)Microsoft SQL ServerMSSQL11.MSSQLSERVERMSSQLDATA» Where all other data file recides.
- Now attach from there it will work
Draken
3,13413 gold badges33 silver badges53 bronze badges
answered Sep 20, 2016 at 10:19
1
I used Entity framework in my application and had this problem,I seted any permission in folders and windows services and not work,
after that I start my application as administrator (right click in exe file and select «run as admin») and that works fine.
answered Jul 17, 2017 at 7:45
Ali YousefiAli Yousefi
2,3552 gold badges32 silver badges47 bronze badges
If you get this error on an .MDF
file in the APP_DATA
folder (or where ever you put it) for a Visual Studio project, the way I did it was to simply copy permissions from the existing DATA
folder here (I’m using SQL Express 2014 to support an older app):
C:Program FilesMicrosoft SQL ServerMSSQL12.SQLEXPRESS2014MSSQLDATA
(note: your actual install path my vary — especially if your instance name is different)
Double click on theDATA
folder first as administrator to make sure you have access, then open the properties on the folder and mimic the same for the APP_DATA
folder. In my case the missing user was MSSQL$SQLEXPRESS2014
(because I named the instance SQLEXPRESS2014
— yours may be different). That also happens to be the SQL Server service username.
answered Jan 24, 2019 at 1:37
James WilkinsJames Wilkins
6,7252 gold badges47 silver badges73 bronze badges
For some reason, setting all the correct permissions did not help in my case. I had a file db.bak
that I was not able to restore due to the 5(Access is denied.)
error. The file was placed in the same folder as several other backup files and all the permissions were identical to other files. I was able to restore all the other files except this db.bak
file. I even tried to change the SQL Server service log on user — still the same result. I’ve tried copying the file with no effect.
Then I attempted to just create an identical file by executing
type db.bak > db2.bak
instead of copying the file. And voila it worked! db2.bak
restored successfully.
I suspect that some other problems with reading the backup file may be erroniously reported as 5(Access is denied.)
by MS SQL.
answered Nov 27, 2019 at 8:49
ps_ttfps_ttf
1,0967 silver badges15 bronze badges
In linux, I went to /var/opt/mssql/data/
folder and opened a terminal with sudo
then, changed my *.mdf and *.ldf file permissions as below in which you replace yourDB
with your Database file name and myUser
to currently logged username:
chmod 755 yourDB.mdf
chown myUser yourDB.mdf
chmod 755 yourDB.ldf
chown myUser yourDB.ldf
After that, it was reconnected without any issue.
answered Jan 8, 2020 at 8:07
If the database you are trying to attach is compressed it may show error message.
First you have to decompress the file. For that go to properties of mdf/ldf file >> then «Advanced» >> Uncheck «Compress Contents to save disk space» >> Press «Apply».
After that give it a try.
Dharman♦
30.4k22 gold badges84 silver badges132 bronze badges
answered May 20, 2021 at 19:47
1
- Run SQL Server management studio as Administrator
- Log in as Windows user
- Remove log file if you have only MDF file (haven’t log file)
With 3 items in the above checklist, you will remove almost problems related with attach database task.
answered May 22, 2021 at 5:17
If you’re using Storage Gateway — SMB (S3)
Do this from the management studio
-
EXEC xp_cmdshell ‘net use X: 100.155.16.6mystoragegatewayfolder xxmysuperpassxx /user:sgw-445577smbguest /persistent:yes /y’
-
EXEC XP_CMDSHELL ‘Dir X:’ (this should show you the Directory info, serial no etc)
-
Mount the drive (This PC — > Mount network drive, using the same info above)
Test run a backup job using scheduled backup, this will force to use the sql server agent and you can see where it’s writing and whats the issue if any.
answered Aug 9, 2022 at 4:51
Alexey181 |
|
1 |
|
27.05.2011, 10:42. Показов 44118. Ответов 6
уважаемые, как избавиться от ошибочки? C:oraclexeapporacleproduct10.2.0serverBIN>net start OracleServiceXE Отказано в доступе. |
Programming Эксперт 94731 / 64177 / 26122 Регистрация: 12.04.2006 Сообщений: 116,782 |
27.05.2011, 10:42 |
6 |
Почетный модератор 28040 / 15773 / 982 Регистрация: 15.09.2009 Сообщений: 67,752 Записей в блоге: 78 |
|
27.05.2011, 17:30 |
2 |
ошибка 5 говорит о недостатке прав. копайте в эту сторону. (не смотря на юзера с правами админа.)
1 |
$ su 1605 / 520 / 97 Регистрация: 18.11.2010 Сообщений: 2,807 Записей в блоге: 2 |
|
27.05.2011, 22:41 |
3 |
клацни по свойствам этой проги и нажми всегда запускать от имени администратора….
1 |
1 / 1 / 0 Регистрация: 23.12.2011 Сообщений: 9 |
|
30.08.2012, 11:42 |
4 |
Правой кнопкой на cmd.exe (или cmd)->Запуск от мени администратора и повтори попытку
0 |
Почетный модератор 28040 / 15773 / 982 Регистрация: 15.09.2009 Сообщений: 67,752 Записей в блоге: 78 |
|
30.08.2012, 11:44 |
5 |
ProgMaster, спасибо, твоего то совета ТС и ждал 15 месяцев.
0 |
Yudgin |
29.04.2014, 23:34
|
Не по теме: magirus, подколол так подколол!:D
0 |
magirus |
30.04.2014, 07:56
|
Не по теме: Yudgin, а вот и горячая эстонская кавалерия пожаловала, спустя еще 20 месяцев.
0 |
I’m trying to execute the following script in SQL Server Management Studio:
USE [master]
GO
CREATE DATABASE [test1] ON PRIMARY (
NAME = N'test1',
FILENAME =
N'C:Program FilesMicrosoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATAtest1.mdf',
SIZE = 70656KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB)
LOG ON (
NAME = N'test1_log',
FILENAME =
N'C:Program FilesMicrosoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATAtest1_log.ldf',
SIZE = 164672KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
But I’m getting the error:
Msg 5123, Level 16, State 1, Line 2
CREATE FILE encountered operating system error 5 (Access is denied.)
while attempting to open or create the physical file
‘C:Program FilesMicrosoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATAtest1.mdf’.Msg 1802, Level 16, State 4, Line 2
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Already have all role permissions for my user, any ideas on what’s wrong?
November 26, 2019
MSSQL
This is because the user account that the SQL Server service is running on is not authorized in the MSSQL Registry files.
Solution
-> Run -> Regedit
-> HKEY_LOCAL_MACHINE
-> SOFTWARE
-> Microsoft
-> Microsoft SQL Server
Right-click on the corresponding instance Name, grant Full Control authorization to the MSSQL Service account from the permissions tab. The problem will be solved.
Note: If Instance has been upgraded before, the Microsoft SQL Server folder will have reg keys for multiple versions. If you grant authorization to the keys of the different version, the error will continue and you must grant full control on the correct file.
(I reviewed the comments from this post, but I was not able to resolve my issue).
I am trying to attach a mdf file that I placed in other drive, and I got this error:
I am aware of the permission issue of the folder that I am pointing the file (to attach).
I gave the full control on that folder for both myself and SQL Server account (NT ServiceMSSQL$TEST1).
I am not sure this extra screenshot would help:
What can I do to fix this?