ChatGPT解决这个技术问题 Extra ChatGPT

mssql '5 (Access is denied.)' error during restoring database

I want to restore a database from a file (Tasks → Restore → Database; after I select from device and select file) via SQL Server Management Studio.

After that, I get this error:

The operating system returned the error '5(Access is denied.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'E:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\XXXXXX.mdf'. Msg 3156, Level 16, State 8, Server XXXX, Line 2

How do I fix this problem? Is it a security error?

The operating system returned the error '5(access denied.)' when restoring database in sql server can be solved by enabling the Relocate all files to folder in the Files options as follows:i.stack.imgur.com/BVhpc.png

U
Uwe Keim

I recently had this problem. The fix for me was to go to the Files page of the Restore Database dialog and check "Relocate all files to folder".

https://i.stack.imgur.com/llPjc.png


Thanks for this. This was the best solution in my case, because the source database was on a drive that I intend to remove from the system. Relocation was a necessary part of the move.
Thank you! This resolved the issue. I was experience this problem from the same file on multiple user accounts and running in admin mode.
Works great for me, Thanks!
Thanks for your help, you saved my day!
This is a good catch when restoring to a new SQL version on the same box. I didn't notice that I was trying to restore into an old version directory that the new service account didn't have access to. (MSSQL14.MSSQLSERVER vs MSSQL15.MSSQLSERVER)
G
Greg Bray

The account that sql server is running under does not have access to the location where you have the backup file or are trying to restore the database to. You can use SQL Server Configuration Manager to find which account is used to run the SQL Server instance, and then make sure that account has full control over the .BAK file and the folder where the MDF will be restored to.

https://i.stack.imgur.com/rA58U.png


I added "NT Service\MSSQLSERVER" user to the list of "Group or user names" allowed to and it worked
I needed to assign this user to the security of the FILE , the folder wasn't enough.
V
Vikash Pandey

Well, In my case the solution was quite simple and straight.

I had to change just the value of log On As value.

Steps to Resolve-

Open Sql Server Configuration manager Right click on SQL Server (MSSQLSERVER) Go to Properties

https://i.stack.imgur.com/y53oV.png

change log On As value to LocalSystem

https://i.stack.imgur.com/C0Lbq.png

Hoping this will help you too :)


After hours of messing around with different permissions. This (changing the log on as value) fixed my problem. Thank you!
R
Ramone Hamilton

I just ran into this same problem but had a different fix. Essentially I had both SQL Server and SQL Server Express installed on my computer. This wouldn't work when I attempted to restore to SQL Express, but worked correctly when I restored it to SQL Server.


+1 This can be a particularly nasty scenario which leads to wasting too much time in debugging the error
D
DaFois

A good solution that can work is go to files > and check the reallocate all files

https://i.stack.imgur.com/2yp41.png


N
Nathan Tuggy

I tried the above scenario and got the same error 5 (access denied). I did a deep dive and found that the file .bak should have access to the SQL service account. If you are not sure, type services.msc in Start -> Run then check for SQL Service logon account.

Then go to the file, right-click and select Security tab in Properties, then edit to add the new user.

Finally then give full permission to it in order to give full access.

Then from SSMS try to restore the backup.


You don't have enough rep yet to attach a screenshot directly, so paste the link in as a comment on this post and someone will put it in for you.
This is the only answer with context that works for an Entity framework style instance of the issue where I believe there is no .BAK file. ( Code First - entity framework - initial database seed migration ) You have much gratitude - I spent after spending hours searching for an answer that made sense!
R
Raja Sekhar

I was getting the same error while trying to restore SQL 2008 R2 backup db in SQL 2012 DB. I guess the error is due to insufficient permissions to place .mdf and .ldf files in C drive. I tried one simple thing then I succeeded in restoring it successfully.

Try this:

In the Restore DB wizard windows, go to Files tab, change the restore destination from C: to some other drive. Then proceed with the regular restore process. It will definitely get restores successfully!

Hope this helps you too. Cheers :)


c
cheriejw

There are several causes for this error, I got this error because I checked "Reallocate all files to folder" in the Files tab of Restore Database window but the default path did not exist on my local machine. I had the ldf/mdf files in another folder, once I changed that I was able to restore.


Simple. Worked.
P
P.Githinji

https://i.stack.imgur.com/BVhpc.png


T
Tom Stickel

I found this, and it worked for me:

CREATE LOGIN BackupRestoreAdmin WITH PASSWORD='$tr0ngP@$$w0rd'
GO
CREATE USER BackupRestoreAdmin FOR LOGIN BackupRestoreAdmin
GO
EXEC sp_addsrvrolemember 'BackupRestoreAdmin', 'dbcreator'
GO
EXEC sp_addrolemember 'db_owner','BackupRestoreAdmin'
GO

Can you elaborate a bit more what that does?
Its been 8 years... but it creates a login with a password, then a user for that login, and then added roles from the sp_ commands of member role and db owner , allows the BackRestoreAdmin to be a dbcreator , and a db_owner
N
Newbee

In my case I had to check the box in Overwrite the existing database (WITH REPLACE) under Options tab on Restore Database page.

The reason I was getting this error: because there was already an MDF file present for the database and it was not getting overwritten.

Hope this will help someone.


j
jgo

If you're attaching a database, take a look at the "Databases to attach" grid, and specifically in the Owner column after you've specified your .mdf file. Note the account and give Full Permissions to it for both mdf and ldf files.


R
Radoslaw Jurewicz

https://i.stack.imgur.com/TZaUo.png


I'm not sure why this answer got down voted. I had the same issue. Permissions were all ok, but because the file was marked encrypted, SQL was raising access denied on reading contents from file.
u
user1465073

this happened to me earlier today, i was a member of the local server's admin group and have unimpeded access, or i thought so. I also ticked the "replace" option, even though there is no such DB in the instance.

Found out that there used to be DB of the same name there, and the MDF and LDF files are still physically located at the data and log folders of the server, but the actual metadata is missing in the sys.databases. the service account of SQL server also can't ovewrwrite the existing files. Found out also that the files' owner is "unknown", i had to change ownership, to the 2 files above so that it is now owned by the local server's admin group, then renamed it.

Then finally, it worked.


r
reza.bm

The account does not have access to the location for backup file. Take the following steps to access the SQL Server Configuration Manager via Computer Manager easily

Click the Windows key + R to open the Run window. Type compmgmt.msc in the Open: box. Click OK. Expand Services and Applications. Expand SQL Server Configuration Manager. Change User Account in Log On As tab .

Now you can Restore Data Base easily


D
David A

The fix for me was to go into Options when trying to Restore the database and change the path to the new path. Here is the screenshot