SQL Server: How to attach / repair a detached / da

2020-02-13 02:19发布

A database server crashed. I was able to get the mdf and the log file and I am trying to mount it on another server. I don't have backup as it was a development database.

When I try to attach the mdf and ldf files, Sql Server Management Studio give me this error:

    TITLE: Microsoft SQL Server Management Studio
    ------------------------------

    Attach database failed for Server

    ------------------------------

    Could not redo log record (457:14202:19), for transaction ID (0:478674), on page (1:519205), database 'WSS_Content_5555' (database ID 15). Page: LSN = (370:463:113), type = 1. Log: OpCode = 2, context 2, PrevPageLSN: (298:40524:64).
    Restore from a backup of the database, or repair the database.
    During redoing of a logged operation in database 'WSS_Content_5555', an error occurred at log record ID (457:14202:19).
    Typically, the specific failure is previously logged as an error in the Windows Event Log service.
    Restore the database from a full backup, or repair the database.
    Could not open new database 'WSS_Content_5555'.
    CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 3456)

I don't know how to repair the database. Does it need to be attached before beeing repaired? In that case, how can I attach it?

2条回答
做自己的国王
2楼-- · 2020-02-13 02:50

You can try a workaround. In short:

  1. Create a dummy DB with the same name (may need to remove the real DB first, save the original files or rename then).
  2. Take the dummy off line (detach and (set offline or stop the SQL service)).
  3. Delete dummy files, replace then with the real DB files.
  4. Try to re-attach de DB

Edit

As by OP comment note you also can need to rebuild the log (if you lost transactions)

  1. ALTER DATABASE [MyDatabase ] REBUILD LOG ON (NAME=’MyDatabaseLog’,FILENAME=’D:\Microsoft SQL Server\YourDataPath\Data\Logfile.ldf’)

    and put the DB in multiple users log (taking the DB off can require you to put it in single use mode)

  2. ALTER DATABASE [nomdb] SET MULTI_USER

For all the gore details you can refer to the Paul Randal Article

(Note in this article the author uses EMERGENCY MODE to (attempt) repair the transaction log)

I already used it with success but depending on the extent of the damage or others details it can be a impossible task. Consider restoring a backup.

Note this stunts are fine in a development server but you really need to plan (and drill) for disaster recovery in a prodution server.

查看更多
我只想做你的唯一
3楼-- · 2020-02-13 03:08

You can use SQL Database Recovery Tool Repairs

I had a database file (Diamond.mdf) which was corrupted and I was not able to attach into SQL Server DB and which contains most of the business data. I was no idea about the error and unable to recover the corrupted DB. I did so many manual mechanism to overcome this issue but unable to do.

  • Select MDF file: Open Select Database dialog box by choosing Select Database icon from File Menu. In Select Database dialog box, click Browse.
  • Open dialog box will appear. Browse and select the desired file and then click Open and Select 'Include Deleted Records' check box to recover deleted records (if found) during the repairing process.
  • Open Select Database dialog box by choosing Select Database icon from File Ribbon. From Search section, select the drive you want to search for MDF files. Click OK.

enter image description here

  • After the search is finished, a list of MS SQL Database files found in the selected drive is shown. Select file you wish to repair from this list and click Repair button to start the repairing process.

enter image description here

  • After the search is finished, a list of MS SQL Database files found in the selected drive is shown. Select file you wish to repair from this list and click Repair button to start the repairing process.

enter image description here

  • This will repair your corrupted mdf file and load the database object on left side pane. In that, we can view all the tables, stored procedure and all objects.
  • After you complete the repairing process successfully, Stellar Repair for MS SQL shows preview of corrupted database files. The left pane displays list of items (Tables, Views, Synonyms, Defaults, Data Types and more) in a tree like structure, upper right pane displays the contents of the item selected in the tree view and bottom pane shows message log. A sample screen is displayed below

enter image description here

  • After the repairing process gets completed, click Save option from File ribbon. Specify whether you want to save the repaired database in MDF, CSV, HTML or XLS format by selecting the appropriate radio button. Saving repaired database as MDF Saving repaired database as CSV, HTML or XLS If you want to save your repaired database as an MDF, you can either save your database as a New database or you can insert the data to an existing Live database.
  • To save the repaired database as a completely New database, follow the steps mentioned below: Select New Database radio button. Enter Server / Instance Name. Choose the Authentication method from the drop down list for logging in to the server.
  • If the authentication method selected is Windows Authentication, you need not enter any user name and password. Enter the server user name and password, if the selected authentication method is SQL Server Authentication.

enter image description here

  • Select a destination for saving the repaired MDF file. Click Save button to save the repaired MS SQL Database file. A message is displayed after the process is completed successfully.

enter image description here

  • The repair was completely done and I was able to open the database from SQL Server Db as below. enter image description here

Note: This is not free tool.

查看更多
登录 后发表回答