While in the process of upgrading our database servers, I had to recall how to move system databases, which forced me to update my notes, from sql 2000. Below are the steps required to move sql 2005 system databases. If you’re wondering why this is needed, well there’s a few reasons. First, if you don’t select the target location during SQL install, these files can be put under the default installation path, typically under the C:\ drive. In our world, optimization is everything, therefore we attempt distribute data, logs, tempDB on their own spindles with varying RAID levels to maximize performance, which is a whole other topic.
NOTE: These steps are slightly different if you’re working with clustered databases, which I’ll speak to in a future post.
MODEL:
To move the MODEL database, you must start SQL Server together with the -c option, the -m option, and trace flag 3608. Trace flag 3608 prevents SQL Server from recovering any database except the master database.
1. In SQL Server Configuration Manager, click SQL Server 2005 Services.
2. In the right pane, right-click SQL Server (<instance_name>), and then click Properties.
3. On the Advanced tab, in the Startup Parameters box, type the parameters separated by semicolons (;). i.e. -c; -m; -T3608
For example, to start in single-user mode, insert -m; in front of the existing startup options, and then restart the database.
Important: After you are finished using single-user mode, you must remove the -m; from the Startup Parameters box before you can restart the server instance in the normal multi-user mode.
4. Stop and then restart SQL Server.
5. Detach the model database by using the following commands:
use master
go
sp_detach_db ‘model’
go
6. Move the Model.mdf and Modellog.ldf files from the D:\Mssql7\Data folder to the desired location.
7. Reattach the model database by using the following commands:
use master
go
sp_attach_db ‘model’,’R:\sqlData\model.mdf’,’T:\sqlLogs\modellog.ldf’
go
8. Remove -c -m -T3608 from the startup parameters in SQL Server Configuration Manager.
9. Stop and then restart SQL Server. You can verify the change in file locations by using the sp_helpfile stored procedure. For example, use the following command:
use model
go
sp_helpfile
go
MSDB:
To move the MSDB database, you must start SQL Server together with the -c option, the -m option, and trace flag 3608. Trace flag 3608 prevents SQL Server from recovering any database except the master database. To add the -c option, the -m option, and trace flag 3608, follow the steps in the “Moving the model database” section. After you add the -c option, the -m option and trace flag 3608, follow these steps:
1. Stop, and then restart SQL Server.
2. Make sure that the SQL Server Agent service is not currently running.
3. Detach the msdb database as follows:
use master
go
sp_detach_db ‘msdb’
go
4. Move the Msdbdata.mdf and Msdblog.ldf files from the current location to the new target location.
5. Remove -c -m -T3608 from the startup parameters in SQL Server Configuration Manager.
6. Stop and then restart SQL Server.
Note If you try to reattach the msdb database by starting SQL Server together with the -c option, the -m option, and trace flag 3608, you may receive the following error message:
Server: Msg 615, Level 21, State 1, Line 1 Could not find database table ID 3, name ‘model’.
7. Reattach the msdb database as follows, changing “X” for your target location:
use master
go
sp_attach_db ‘msdb’,’X:\sqlData\msdbdata.mdf’,’X:\sqlLogs\msdblog.ldf’
go
MASTER: Moving the master database
1. Change the path for the master data files and the master log files in SQL Server Configuration Manager. Note You may also change the location of the error log here.
2. Right-click the SQL Server in Enterprise Manager and then click Properties.
3. Click Startup Parameters to see the following entries:
-dX:\data\master.mdf
-eY:\errlog\ErrorLog
-lZ:\logs\mastlog.ldf
-d is the fully qualified path for the master database data file.
-e is the fully qualified path for the error log file.
-l is the fully qualified path for the master database log file.
4. Change these values as follows:
a. Remove the current entries for the Master.mdf and Mastlog.ldf files.
b. Add new entries specifying the new location:
-dX:\SQLDATA\master.mdf
-lZ:\SQLDATA\mastlog.ldf
5. Stop SQL Server.
6. Copy the Master.mdf and Mastlog.ldf files to the new location (E:\Sqldata).
7. Restart SQL Server.
tempDB: Moving the tempdb database
You can move tempdb files by using the ALTER DATABASE statement.
1. Determine the logical file names for the tempdb database by using sp_helpfile as follows:
use tempdb
go
sp_helpfile
go
The logical name for each file is contained in the name column. This example uses the default file names of tempdev and templog.
2. Use the ALTER DATABASE statement, specifying the logical file name as follows:
use master
go
Alter database tempdb modify file (name = tempdev, filename = ‘S:\tempDB\tempdb.mdf’)
go
Alter database tempdb modify file (name = templog, filename = ‘T:\sqlLogs\templog.ldf’)
go
You should receive the following messages that confirm the change:
Message 1
The file “tempdev” has been modified in the system catalog. The new path will be used the next time the database is started.
Message 2
The file “templog” has been modified in the system catalog. The new path will be used the next time the database is started.
3. Using sp_helpfile in tempdb will not confirm these changes until you restart SQL Server.
4. Stop and then restart SQL Server.
– Hope this helps save you some time next time you need to move system databases around.