Moving a database in SQL Server is normally as simple as detaching it from the server, moving it to it's new location and then attaching it again. However, that only counts for user databases. Moving system databases requires quite a bit more work. Since I had to do this a couple of times during the last weeks I wrote down a simple checklist that I could follow to make sure it was done right, so I did not screw up the servers and could speed up the process as much as possible. And, since I guess I will be doing it again sometime in the future and inevitably will lose the notes I scribbled down, I thought I could just as well post them here for safe-keeping. If anyone wants to use this checklist go ahead, but remember to do the steps in the exact order of the list, and make sure you have all the necessary backups before starting. To be completely clear, doing this wrong can completely screw up your databases and I can not take any responsibility if anything does go wrong. Also note that the checklist was written for the specific situation I was encountered with. Your system databases might have more data files and/or other file paths and names than those in the list, so you might need to make some changes. Have fun!
------------- use master go exec sp_detach_db 'msdb' go exec sp_detach_db 'model' go -------------
------------- use master go exec sp_attach_db 'model' , 'PATH_TO_MODEL_DATAFILE\model.mdf' , 'PATH_TO_MODEL_LOGFILE\modellog.ldf' go -------------
------------- use model go exec sp_helpfile go -------------
------------- use master go exec sp_attach_db 'msdb' , 'PATH_TO_MSDB_DATAFILE\msdbdata.mdf' , 'PATH_TO_MSDB_LOGFILE\msdblog.ldf' go -------------
------------- use msdb go exec sp_helpfile go -------------
------------- use master go alter database tempdb modify file (name = tempdev , filename = 'PATH_TO_NEW_LOCATION_OF_TEMPDB_DATAFILE\tempdb.mdf') go alter database tempdb modify file (name = templog , filename = 'PATH_TO_NEW_LOCATION_OF_TEMPDB_DATAFILE\templog.ldf') go -------------
------------- use tempdb go exec sp_helpfile go -------------
------------- use tempdb go create table test (a int) insert into test (a) values (1) select * from test drop table test go -------------
OLD_PATH_TO_MASTER_MDFFILE\master.mdf --> NEW_PATH_TO_MASTER_MDFFILE\master.mdf OLD_PATH_TO_MASTER_LOGFILE\Data\mastlog.ldf --> NEW_PATH_TO_MASTER_LOGFILE\mastlog.ldf OLD_PATH_TO_SQL_DATA_LOCATION\BACKUP --> NEW_PATH_TO_SQL_DATA_LOCATION (the entire folder with everything in it) OLD_PATH_TO_SQL_DATA_LOCATION\JOBS --> NEW_PATH_TO_SQL_DATA_LOCATION (the entire folder with everything in it) OLD_PATH_TO_SQL_DATA_LOCATION\LOG --> NEW_PATH_TO_SQL_DATA_LOCATION (the entire folder with everything in it) OLD_PATH_TO_SQL_DATA_LOCATION\REPLDATA --> NEW_PATH_TO_SQL_DATA_LOCATION (the entire folder with everything in it)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer BackupDirectory = NEW_PATH_TO_SQL_DATA_LOCATION\BACKUP HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Replication WorkingDirectory = NEW_PATH_TO_SQL_DATA_LOCATION\REPLDATA HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup SQLDataRoot = NEW_PATH_TO_SQL_DATA_LOCATION\ HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent ErrorLogFile = NEW_PATH_TO_SQL_DATA_LOCATION\LOG\SQLAGENT.OUT WorkingDirectory = NEW_PATH_TO_SQL_DATA_LOCATION\JOBS
So there we are, all system databases moved. Again, please note that this was mainly meant as a checklist for myself, but feel free to use it as a base for your own checklist when you need to move system databases. I urge you to read through it several times so you are sure what it says and what you are doing. Most of the steps here come from the Microsoft article Moving SQL Server databases to a new location with Detach/Attach, which will probably help you more than this list. I simply compiled them into an easy-to-follow, step-by-step list that I could use to cut the down-time as much as possible.
To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.
We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:
We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.
Steve Jones Editor, SQLServerCentral.com