The best way one can learn SQL Server is by trying out things on their own and I am no different. I constantly am trying to explore the various options one can use when working with SQL Server. In the same context, when I was playing around with backup restore commands, I made a mistake and unfortunately restarted SQL Server. After that I was unable to start SQL Service. If I start the service, it doesn’t give any error but gets stop automatically.
Whenever I have any weird problems with SQL, I always look at ERRORLOG files for that instance. If you don’t know the location of Errorlog, you should refer Balmukund’s blog (Help : Where is SQL Server ErrorLog?)
This is what I found in ERROLROG just before the stop.
2014-10-28 002039.02 spid9s Starting up database 'model'. 2014-10-28 002040.01 spid9s The database 'model' is marked RESTORING and is in a state that does not allow recovery to be run. 2014-10-28 002040.04 spid9s Error 927, Severity 14, State 2. 2014-10-28 002040.04 spid9s Database 'model' cannot be opened. It is in the middle of a restore.
The error and behavior which I am seeing makes sense because to start SQL Server, we need master, model and tempdb database. You might think that MSDB is also a system database and would be needed for SQL Engine? Well, you might have been tricked. MSDB is needed for SQL Server Agent Service, not SQL Server Service. So, my master is fine, model has some problem. Every new database is created using model, including TempDB so SQL Service is refusing to start. Since the model database is not recovered successfully, SQL Server cannot create the tempdb database, and the instance of SQL Server does not start understandably.
So I called up Balmukund – these are the perks of having a good friend to rely. He never says “no” but he also doesn’t give complete solution to the problem. He gives hint and asks me to research further. This time also magical words were – “use trace flag 3608 and restore model with recovery”.
I followed his advice and performed below steps.
1. Start SQL Server with trace flag 3608 using net start command
Net Start MSSQL$SQL2014 /T3608
In my case SQL2014 is the name of the instance. If you have default instance then service name would be MSSQLServer. For named instance, it is MSSQL$InstanceNameHere
2. After starting with trace flag 3608, I verified the same from Errorlog as well.
Further, I also found below message in ERRORLOG.
Recovering only master database because traceflag 3608 was specified. This is an informational message only. No user action is required.
3. Connected to SQL Instance using SQLCMD by below command.
SQLCMD -S .\SQL2014 -E
You can read parameter of SQLCMD at Books online here
“1>” means we are connected to SQL Instance and then Executed below command (hit enter at end of each line)
RESTORE DATABASE Model WITH RECOVERY
4. Once the command is executed successfully, we will come back to “1>” again. We can type exit to come out of SQLCMD
5. Now stop SQL Service
Net Stop MSSQL$SQL2014
6. And start again without trace flag.
Net Start MSSQL$SQL2014
Now my SQL instance came up happily and I was unblocked. After sometime I got call from Balmukund asking if SQL is started and I told that my next blog is ready on the same topic. He finally asked, how did that happen? And my answer was – I ran wrong command. My command was
BACKUP DATABASE model TO DISK = 'Full.bak'
BACKUP LOG model TO DISK = 'Log.trn' WITH NORECOVERY
My request to my reader is that please DONOT run the above command in your SQL instance and restart SQL else you need to follow the steps in production server. Learning never stops when working with SQL Server.
Reference: Pinal Dave (http://blog.sqlauthority.com)