database attached is read only

后端 未结 12 1120
借酒劲吻你
借酒劲吻你 2020-12-08 19:03

I used the following the script to attach a database. But the database created is read only.
What modifications should I make in the script to make it read-write. Pl

相关标签:
12条回答
  • 2020-12-08 19:27
    ALTER DATABASE [DatabaseName] SET READ_WRITE
    
    0 讨论(0)
  • 2020-12-08 19:29

    There are 3 (at least) parts to this.

    Part 1: As everyone else suggested...Ensure the folder and containing files are not read only. You will read about a phantom bug in windows where you remove read only from folders and containing items, only to open the properties again and see it still clicked. This is not a bug. Honestly, its a feature. You see back in the early days. The System and Read Only attributes had specific meanings. Now that windows has evolved and uses a different file system these attributes no longer make sense on folders. So they have been "repurposed" as a marker for the OS to identify folders that have special meaning or customisations (and as such contain the desktop.ini file). Folders such as those containing fonts or special icons and customisations etc. So even though this attribute is still turned on, it doesn't affect the files within them. So it can be ignored once you have turned it off the first time.

    Part 2: Again, as others have suggested, right click the database, and properties, find options, ensure that the read only property is set to false. You generally wont be able to change this manually anyway unless you are lucky. But before you go searching for magic commands (sql or powershell), take a look at part 3.

    Part 3: Check the permissions on the folder. Ensure your SQL Server user has full access to it. In most cases this user for a default installation is either MSSQLSERVER or MSSQLEXPRESS with "NT Service" prefixed. You'll find them in the security\logins section of the database. Open the properties of the folder, go to the security tab, and add that user to the list.

    In all 3 cases you may (or may not) have to detach and reattach to see the read only status removed.

    If I find a situation where these 3 solutions don't work for me, and I find another alternative, I will add it here in time. Hope this helps.

    0 讨论(0)
  • 2020-12-08 19:33

    Make sure the files are writeable (not read-only), and that your user has write permissions on them.

    Also, on most recent systems, the Program Files directory is read-only. Try to place the files in another directory.

    0 讨论(0)
  • 2020-12-08 19:36

    Another Way which worked for me is:

    After dettaching before you attach

    • -> go to the .mdf file -> right click & select properties on the file -> security tab -> Check Group or usernames:

      for your name\account (optional) and for "NT SERVICE\MSSQLSERVER"(NB)

    List item

    -> if not there than click on edit button -> click on add button

      and enter\search NT SERVICE\MSSQLSERVER
    
    • -> click on OK -> give full rights -> apply then ok

      then ok again do this for .ldf file too.

    then attach

    0 讨论(0)
  • 2020-12-08 19:42

    If SQL Server Service is running as Local System, than make sure that the folder containing databases should have FULL CONTROL PERMISSION for the Local System account.

    This worked for me.

    0 讨论(0)
  • 2020-12-08 19:46

    Giving the sql service account 'NT SERVICE\MSSQLSERVER' "Full Control" of the database files

    If you have access to the server files/folders you can try this solution that worked for me:

    SQL Server 2012 on Windows Server 2008 R2

    1. Right click the database (mdf/ldf) file or folder and select "Properties".
    2. Select "Security" tab and click the "Edit" button.
    3. Click the "Add" button.
    4. Enter the object name to select as 'NT SERVICE\MSSQLSERVER' and click "Check Names" button.
    5. Select the MSSQLSERVER (RDN) and click the "OK" button twice.
    6. Give this service account "Full control" to the file or folder.
    7. Back in SSMS, right click the database and select "Properties".
    8. Under "Options", scroll down to the "State" section and change "Database Read-Only" from "True" to "False".
    0 讨论(0)
提交回复
热议问题