Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mtal18
Starting Member
4 Posts |
Posted - 2006-12-22 : 18:37:41
|
I'm a network Admin so I'm new to DBA and I made a mistake. I deleted the log file (ldf) from the SQL data folder. Then while trying restore anyway I could I took the DB offline and detached hoping that would let me reattach the DB. I still have the original MDF data file but I cant find a way to restore the DB. Does anyone have any ideas? |
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-12-22 : 18:45:31
|
Have you tried with sp_attach_single_file_db and sp_attach_db? |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2006-12-22 : 18:48:35
|
quote: Originally posted by mtal18 I'm a network Admin so I'm new to DBA and I made a mistake. I deleted the log file (ldf) from the SQL data folder. Then while trying restore anyway I could I took the DB offline and detached hoping that would let me reattach the DB. I still have the original MDF data file but I cant find a way to restore the DB. Does anyone have any ideas?
we have had about 5 questions like this in the last week. what the hell is going on? How did you delete a file that was open in the first place? I am assuming you have no actual backups (using the TSQL backup command) Anyway, you are going to want to use a procedure called sp_attach_single_file_db to fix your problem.Here is the documentation on the procedure. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_ae-az_4wrm.aspMake sure you make a copy of your MDF before you do anything.-ecEDIT:fixed link |
|
|
MohammedU
Posting Yak Master
145 Posts |
Posted - 2006-12-22 : 18:57:13
|
You can use EM also to attach the db using single file, sql automatically creates ldf file if the db is closed properly otherwise it will not create the file and you get the error...Same thing happens with sp_attach_single_file_db procedure also...In worst case scenario you can rebuild the log using DBCC REBUILD_LOG...MohammedU |
|
|
mtal18
Starting Member
4 Posts |
Posted - 2006-12-22 : 23:46:19
|
Ok I think the DB did not close properly. Can you explain the DBCC REBUILD_LOG procedure a little more?Thx for the help |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2006-12-22 : 23:57:38
|
quote: Originally posted by mtal18 Ok I think the DB did not close properly. Can you explain the DBCC REBUILD_LOG procedure a little more?
what error do you get when you try sp_attach_single_file_db?-ec |
|
|
MohammedU
Posting Yak Master
145 Posts |
Posted - 2006-12-23 : 00:10:53
|
Backup .mdf file...1. Rename database .mdf file .mdf_old2. Create new database with same name and same mdf and ldf files3. Stop sql services4. Rename new db .mdf file to .mdf_new and .ldf file to .ldf_new5. Rename .mdf_old file to .mdf...6. Restart sql and you db will show up as suspect...7. Change the database context to Master and allow updates to systemtables: Use Master Go sp_configure 'allow updates', 1 reconfigure with override Go8. Set the database in Emergency (bypass recovery) mode: -- note the value of the status column for later use select * from sysdatabases where name = '<db_name>' begin tran update sysdatabases set status = 32768 where name = '<db_name>' -- Verify one row is updated before committing commit tranIf you run DBCC REBUILD_LOG without setting the database in Emergencymode, the command does not work. You do not receive an error, but thelog is not rebuilt either.9. Stop and restart SQL server.If you run DBCC REBUILD_LOG without recycling the server, the followingmessage displays:Server: Msg 5023, Level 16, State 2, Line 1 Database must be put inbypass recovery mode to rebuild the log. DBCC execution completed. IfDBCC printed error messages, contact your system administrator.10. The syntax for DBCC REBUILD_LOG is as follows: DBCC REBUILD_LOG('<db_name>','<log_filename>')where <db_name> is the name of the database and <log_filename> is thephysical path to the new log file, not a logical file name. If you donot specify the full path, the new log is created in the Windows NTsystem root directory (by default, this is the Winnt\System32directory).11. Rebuild the log with this code: DBCC TRACEON (3604) DBCC REBUILD_LOG('<db_name>','<log_filename>') GoIf the command is successful, the following message appears:Warning: The log for database '<db_name>' has been rebuilt.Transactional consistency has been lost. DBCC CHECKDB should be run tovalidate physical consistency. Database options will have to be reset,and extra log files may need to be deleted.After the log is successfully rebuilt, the database is placed in DBO UseOnly mode. That is, the status of the database is 2048 irrespective ofwhat the status was previously. You must reset the status usingsp_dboption or through the SEM.12. Set the database in single-user mode and run DBCC CHECKDB to validatephysical consistency: sp_dboption '<db_name>', 'single user', 'true' DBCC CHECKDB('<db_name>') Go begin tran update sysdatabases set status = <prior value> where name ='<db_name>' -- verify one row is updated before committing commit tran Go13. Turn off the updates to system tables by using: sp_configure 'allow updates', 0 reconfigure with override GoWARNING: After verifying the consistency of the database by running DBCCCHECKDB, and fixing any errors, please make sure to check the databasefor logical consistency as well. Because a new log has been built, thetransactions in theold log are lost, hence you must also verify the logical consistency ofthe data as well.After you successfully complete the preceding steps, you may use thedatabase as normal.MohammedU |
|
|
mtal18
Starting Member
4 Posts |
Posted - 2006-12-23 : 02:04:39
|
quote: Originally posted by MohammedU Backup .mdf file...1. Rename database .mdf file .mdf_old2. Create new database with same name and same mdf and ldf files3. Stop sql services4. Rename new db .mdf file to .mdf_new and .ldf file to .ldf_new5. Rename .mdf_old file to .mdf...6. Restart sql and you db will show up as suspect...7. Change the database context to Master and allow updates to systemtables: Use Master Go sp_configure 'allow updates', 1 reconfigure with override Go8. Set the database in Emergency (bypass recovery) mode: -- note the value of the status column for later use select * from sysdatabases where name = '<db_name>' begin tran update sysdatabases set status = 32768 where name = '<db_name>' -- Verify one row is updated before committing commit tranIf you run DBCC REBUILD_LOG without setting the database in Emergencymode, the command does not work. You do not receive an error, but thelog is not rebuilt either.9. Stop and restart SQL server.If you run DBCC REBUILD_LOG without recycling the server, the followingmessage displays:Server: Msg 5023, Level 16, State 2, Line 1 Database must be put inbypass recovery mode to rebuild the log. DBCC execution completed. IfDBCC printed error messages, contact your system administrator.10. The syntax for DBCC REBUILD_LOG is as follows: DBCC REBUILD_LOG('<db_name>','<log_filename>')where <db_name> is the name of the database and <log_filename> is thephysical path to the new log file, not a logical file name. If you donot specify the full path, the new log is created in the Windows NTsystem root directory (by default, this is the Winnt\System32directory).11. Rebuild the log with this code: DBCC TRACEON (3604) DBCC REBUILD_LOG('<db_name>','<log_filename>') GoIf the command is successful, the following message appears:Warning: The log for database '<db_name>' has been rebuilt.Transactional consistency has been lost. DBCC CHECKDB should be run tovalidate physical consistency. Database options will have to be reset,and extra log files may need to be deleted.After the log is successfully rebuilt, the database is placed in DBO UseOnly mode. That is, the status of the database is 2048 irrespective ofwhat the status was previously. You must reset the status usingsp_dboption or through the SEM.12. Set the database in single-user mode and run DBCC CHECKDB to validatephysical consistency: sp_dboption '<db_name>', 'single user', 'true' DBCC CHECKDB('<db_name>') Go begin tran update sysdatabases set status = <prior value> where name ='<db_name>' -- verify one row is updated before committing commit tran Go13. Turn off the updates to system tables by using: sp_configure 'allow updates', 0 reconfigure with override GoWARNING: After verifying the consistency of the database by running DBCCCHECKDB, and fixing any errors, please make sure to check the databasefor logical consistency as well. Because a new log has been built, thetransactions in theold log are lost, hence you must also verify the logical consistency ofthe data as well.After you successfully complete the preceding steps, you may use thedatabase as normal.MohammedU
I made it to getting the DB to "DBO use only mode" When I get to this code...sp_dboption '<db_name>', 'single user', 'true' DBCC CHECKDB('<db_name>') Go begin tran update sysdatabases set status = <prior value> where name ='<db_name>' -- verify one row is updated before committing commit tran GoI'm not sure what to place in the <prior value> tag and if I leave it as <prior value> it tells me this when I run the code... "Database options single user and dbo use only cannot be set at the same time."Also how do I check the databasefor logical consistency once completed? |
|
|
sleaklight
Starting Member
11 Posts |
Posted - 2006-12-23 : 04:45:17
|
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76613try the method I got mine up and running again after only downloading the mdf and not the ldf, you're basically in the same situation as I was and got it working with the code I posted in one of my last replies |
|
|
mtal18
Starting Member
4 Posts |
Posted - 2006-12-23 : 05:51:24
|
quote: Originally posted by sleaklight http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76613try the method I got mine up and running again after only downloading the mdf and not the ldf, you're basically in the same situation as I was and got it working with the code I posted in one of my last replies
Nice I think that got me back on track. Good code for setting up a DB with only your MDF file. Thank You |
|
|
|
|
|
|
|