Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL SERVER FIX Error 5120 Database is in Read Only Mode After Attaching Fi ...

$
0
0

Sometimes you need to know more about permissions on file, service account etc. to fix certain errors. In my recent visit to a customer location, one of the database developers came to me with a problem. He said that when he attaches MDF and LDF file to the SQL instance, attach is successful, but the issue was next to database name it was saying read-only. Let us see how we canFIX Error 5120.

His problem was interesting, so I asked him to follow-up with me. I asked him to go through try below.

USE [master] GO ALTER DATABASE [SQLAuthority] SET READ_WRITE WITH NO_WAIT GO

But he told that this is not helping and he is getting error.

Error 5120 Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file “D:\MySoftware\Data\Data_Data.MDF”. Operating system error 5: “5(Access is denied.)”.
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file “D:\MySoftware\Data\Data_Log.LDF”. Operating system error 5: “5(Access is denied.)”.
File activation failure. The physical file name “D:\MySoftware\Data\Data_Log.LDF” may be incorrect.
Msg 945, Level 14, State 2, Line 1
Database ‘Lis’ cannot be opened due to inaccessible files or insufficient memory or disk space. See the SQL Server errorlog for details.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.

The databases failed to attach due to permissions issues and the cases being marked as read only. We were able to resolve the issue by granting SQL exclusive rights to the database files and unselecting the read-only attribute. Here are the steps which helped him.

FIXError 5120

First step was to find the Service account, which can be done by opening configuration manager.


SQL SERVER   FIX Error 5120   Database is in Read Only Mode After Attaching Fi ...

So, in my lab machine, its “NT Service\mssql$SQL2016” for SQL Server 2016 instance and NT AUTHORITY\NetworkService for SQL Server 2014 instance. Here are the steps to give permission to files/folder.

Right click the database (mdf/ldf) file or folder and select “Properties”. Select “Security” tab and click the “Edit” button. Click the “Add” button. Enter the object name to select as NT Service\MSSQL$SQL2016′ and click “Check Names” button. It would become MSSQL$SQL2016 Click “OK” button. Give this service account “Full control” to the file or folder. Click “OK” button.

Then if you try to attach, it would work fine. I was able to reproduce the error by marking files as read-only.


SQL SERVER   FIX Error 5120   Database is in Read Only Mode After Attaching Fi ...

Have you faced such permission errors? These are common and sometimes can be hard to even identify what the problem is. Do let me know via the comments section.

Reference: Pinal Dave ( http://blog.SQLAuthority.com )


Viewing all articles
Browse latest Browse all 3160