Rebulding MSDB in SQL Server 2005
Again I spent a gorgeous day in front of the computer trying to solve a major problem.
Seems that because of some diskspace issures we had in April, my MSSQL configure got hosed and the server hasn’t been able to perform any backups nor has the sql server agent been running. This was due to the fact that the MSDB database has been corrupted.
The following are the steps that I took to get everything back up and running. I will warn you that by doing these steps, you will lose all you Maintenance Plans that you had and you will have to recreate them. Figure that this is a small price to pay because let’s face it, you got to have backups.
I would strongly suggest that you don’t be an idiot like me and make sure that you backup your msdb database. However if you are like me and didn’t do this, these steps should get everything back on track.
- Put MSSQL into single user mode
- Restart SQL Server
- Connect to SQL server through the Management Console. From this point on we will be using TSQL to issue the commands so click the New Query button on the top left. At this point you should be in the master database inside the query window.
- Detach the MSDB database using the following commands:
use master
go
sp_detach_db ‘msdb’
goand click Execute
- We need to move (or rename, I prefer moving them) the existing MDF and LDF files for the MSDB database so that we can recreate it.
- Usually these files are located in the following directory:
C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data
Your’s might differ. - Move (or rename) the MSDBDATA.mdf and MSDBLOG.ldf files.
- Usually these files are located in the following directory:
- Back to the Management Studio. Open up the instmsdb.sql file in a new query window. This file is usually located in the following directory:
C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Install - Execute the file. If you see any errors about xp_cmdshell, just ignore them. They are common and the script will take care of it.
- At this point you should have your MSDB database restored. The only thing left is cleanup.
- Execute the following command to make sure that xp_cmdshell is once again set to disable for security reasons:
EXEC sp_configure ‘show advanced options’, 1
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure ‘xp_cmdshell’, 0
GO
RECONFIGURE WITH OVERRIDE
GO - Shutdown SQL Server
- Go back into your Startup Paremeters for your server in the SQL Server Configuration Manager and removed the -c;-m;-T3608 parameters we added earlier.
- Restart SQL Server
Everything should be cool at this point and you’ll be able to recreate any Maintenance Plans and Jobs.
Let me know if you have a better way or a way to restore the configuration information from the old msdb database.
Hi:
Thanks for this Post Rebuilding MSDB.
Saved me a complete re-install.
I forced the install of Master and MSDB from another server. I did this so I could restore production databases from the Production server to a backup server with out breaking the Logins to Database user SID’s.
The problem I had was all the Maintenance Plans on the backup machine still pointed at the Production server, and if I deleted a Maintenance plan it deleted it from Both Servers ??? MSDB must have the severs name hard coded in it. Everything I did was on the Production server.
What I really wanted to tell you about your Post Rebuilding MSDB is; When I was done I couldn’t start the SQL Agent and Got errors trying to look or create any Maintenance plans.
Had to re-run SQL Srver 2005 Hotfix 4458, After that I appear to be OK !!!! You might add a note about rerunning the Hotfix to your Post.
Thanks — Tom Gilmour
Super article and saved my bacon more than once. However, your editor has pasted “smart-quotes” into the article, so that copy-paste of the commands doesn’t exactly work without editing.
I also found that once SQL server was in single user mode, that I could not really use Management Studio effectively (it seems to want to open 1 connection for the brower window, and 1 for the query editor window). So I found it easier to use sqlcmd.exe. YMMV
I know it’s been awhile since this was posted but I wanted to thank you for the write up. Saved me a lot of trouble and from having to reinstall.
One thing I wasn’t able to do though was to log into the Management Studio once in single user mode. Like the previous poster stated, that was fixed with sqlcmd though.
Thanks again.
Almost a year later this post is still top-notch :D.
Thanks alot Rip – this really saved the day.
Before finding this solution I came across the official Microsoft solution for this sort of situations:
http://msdn.microsoft.com/en-us/library/dd207003.aspx
Needless to say that that one seemed far more general and far more time-consuming than this.
All I can add is that the execution time for the solution in this blos is: 5 minutes (litteraly)
// However it seems to be restricted only to problems with MSDB. Having trouble with the master DB might require a different solution.
Finally, I didn’t run any hotfixes after this – only did a DBCC CHECKDB to make sure that all of the databases were OK.
PS: I detached all the non-system databases I could before applying the solution just in case.
PPS: Thanks again 🙂
To all,
You are most welcome. Can’t believe this post has been up for almost a year and people still find it helpful. God I love blogging 🙂
Thanks to the author… this article saved my extra work. I tried many other options and was planning to re-install.. but this article saved me from that.
I am facing a similar kind of problem so any link available for SQL Srver 2005 Hotfix 4458 as i could not find it 😦
You have done a great job. It saved me a lot of pain and was smooth for me. I had a corrupt master and msdb and wanted to migrate production server at the same time.
Thanks a lot!!!!
Rajesh
It works! Thanks.
Hi,
I tried this to get my msdb database back, but though there is no other sessions opened, I cant rename or delete the msdb and so when i try to install it , its saying the file cant be installed because its already there.
Any idea how can i rectify this.
I checked all the sessions and no users in that.
Thanks
did you make sure you followed the instructions in this post and setup sqlserver to run in single user mode?
Hi,
Yes, I followed all the same step..
Now, I tried again to restart the server in the trace mode and when i tried the detach command for the msdb database, it is giving me the error-
Msg 15010, Level 16, State 1, Procedure sp_detach_db, Line 34
The database ‘msdb’ does not exist. Supply a valid database name. To see available databases, use sys.databases.
and I tried to rename, delete the msdb data file , but still it is saying the data file is in use.
No application is connected to it.
There was a disk space issue which corrupted the databse, is the the reason why it is saying its in use??
Lol..
hmmmm… sounds like you’ve got something funky going on. try this: stop the sql services altogether. Go and manually rename the msdb.mdf and msdb.ldf file, then bring up sql server in single user mode and see what happens. another thing you can do is install sql server to another instance or on a separate box and then copy those two file from the new instance or machine.
Hi,
I tried to manually rename the file , but still it is saying it is used by another program and so couldnt rename.
I copied the databases needed to another server and its working, but I need this installation to be up ad running again…
so trying to stop each and every services to rename it.but still saying its in use.
if the file is corrupted whether it will say that its in use??
its making crazy…
Lol
Just to add one more thing. I can rename msdb.ldf file but just the .mdf file is saying it is in use.
tried to copy from another server, but still cant rename it.
thanks
Lol
Thanks for the post. I just restored MSDB and it worked.
Thank you!!! Worked!!
How do you access management studios in single user mode? How do you use ‘sqlcmd.exe’ ?
@pleasehelpthis’tard
you’re not putting management studio into single user mode, you’re putting sql server itself.
Yep! Still works like a charm. Thanks!
many thanks!!
this solve my day with a customer!
Worked like a charm, Many Thanks! I found out my users were running a rogue SQL Server 2005 instance on a BizTalk build server. The server had a hard drive crash which took out MSDB. While they may or may not appreciate that I was able to “fix it”, I was happy to learn something!
HI;
I want Rebulding MODEL DATABASE in SQL Server 2005,,
Thanks for the Post Rebuilding MSDB.
Thanks
Ganeshan.k
Thank you!!!
you solve me a big problem with a subject I have in university!!! It was taking me nuts!!!
thnxs!!!!
hello, i have problem when i execute instmsdb.sql file in a new query window, when instmsdb.sql open on sql management studio, the login page is appear, when i choose login with sql server authorication, error messages is appear, and these message : Login failed for user ‘Lenovo-NB\Studenti’. Reason: Server is in single user mode. Only one administrator can connect at this time. (.Net SqlClient Data Provider)..
hello all, whats wrong about me ? i was follow all step…
thanks..
am waiting for reply…
regards.
James, in order to login with SQL Management Studio, you have to stop all the other SQL services using the Windows Service Manager, because one of them is logging into the database before you do, and only one can connect in Single User Mode. After stopping all the other services, you should be able to login.
Thanks, it’s work fine and solve my problem.
You need to login as sa in management studio before you change to single user mode
It’s already 3 years after this post….
Today I’m glad to announce at last I’m able to recover my msdb which in RECOVERY MODE since 3 days ago. But it’s not that straight forward to detach and rebuild the msdb. I have several times, after each step/command listed here, to restart the service. I used SQLCMD to execute the command since it’s faster and more straight forward. Each time hit error “restricted mode”,had to restart the services. And at last, SUCCESS.
Thanks rip747 for this great post.
Yep, this still works, bailed me out big time today, everything was nice and smooth. The only issue I ran in to was the “only one admin can access at a time”, stopped SQL Services and worked without issue but I could only keep one quarry window open at a time.
I guess as long as SQL 2005 is around, we’ll be using this post. It saved my tukas today! As long as you disconnect from the object browser, SSMS works fine in single user mode. Thanks!!
Excellent, thank you. Saved me a lot of time when my msdb got damaged on my laptop.Thanks to this post I was able to resume development work on a critical task quickly.
Great post! Love it!
nice post with screenshots!! very useful !!
I observed below error when I put startup parameters as shown in screenshot:
Location: dbmgr.cpp:6728
Expression: !FindDB(dbid)
SPID: 58
Process ID: 4720
Msg 3624, Level 20, State 1, Line 1
A system assertion check has failed. Check the SQL Server error log for details
logically..it does matter where you put your trace flag (-T3608).
Ref: http://www.sqlservercentral.com/Forums/Topic255477-146-1.aspx#bm291392
But It went fine after putting -T option at last in startup parameters as :
-dC:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\DATA\master.mdf;-eC:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\LOG\ERRORLOG;-lC:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\DATA\mastlog.ldf;-c;-T3608
Thanks RIP
Thank you!
For those having trouble with SQL Mgmt Studio, you may have tried to double-click instmsdb.sql. Instead open it in Notepad and paste contents into the New Query window you opened when you ran sp_detach_db…
THANK YOU!!!!!!! – saved my butt – not a SQL DBA but play one one TV for Work. either way this really save me getting our application up and running on this new server we shipped to a customer. not sure how the msdb got corrupted, but these steps did the trick and we are moving forward.
thank you so much it worked perfectly