Moving System Databases
This topic describes how to move system databases in SQL Server. Moving system databases may be useful in the following situations:
Failure recovery. For example, the database is in suspect mode or has shut down because of a hardware failure.
Planned relocation.
Relocation for scheduled disk maintenance.
The following procedures apply to moving database files within the same instance of SQL Server. To move a database to another instance of SQL Server or to another server, use the backup and restore or detach and attach operations.
The procedures in this topic require the logical name of the database files. To obtain the name, query the name column in the sys.master_files catalog view.
Important |
|---|
If you move a system database and later rebuild the master database, you must move the system database again because the rebuild operation installs all system databases to their default location. For more information about rebuilding the master database, see "Rebuilding System Databases, Rebuilding the Registry" in How to: Install SQL Server 2008 R2 from the Command Prompt. |
To move a system database data or log file as part of a planned relocation or scheduled maintenance operation, follow these steps. This procedure applies to all system databases except the master and Resource databases.
-
For each file to be moved, run the following statement.
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name , FILENAME = 'new_path\os_file_name' )
-
Stop the instance of SQL Server or shut down the system to perform maintenance. For more information, see Stopping Services.
-
Move the file or files to the new location.
-
Restart the instance of SQL Server or the server. For more information, see Starting and Restarting Services.
-
Verify the file change by running the following query.
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID(N'<database_name>');
If the msdb database is moved and the instance of SQL Server is configured for Database Mail, complete these additional steps.
-
Verify that Service Broker is enabled for the msdb database by running the following query.
SELECT is_broker_enabled FROM sys.databases WHERE name = N'msdb';
For more information about enabling Service Broker, see ALTER DATABASE (Transact-SQL).
-
Verify that Database Mail is working by sending a test mail. For more information, see Troubleshooting Database Mail.
If a file must be moved because of a hardware failure, follow these steps to relocate the file to a new location. This procedure applies to all system databases except the master and Resource databases.
Important
|
|---|
|
If the database cannot be started, that is it is in suspect mode or in an unrecovered state, only members of the sysadmin fixed role can move the file. |
-
Stop the instance of SQL Server if it is started.
-
Start the instance of SQL Server in master-only recovery mode by entering one of the following commands at the command prompt. The parameters specified in these commands are case sensitive. The commands fail when the parameters are not specified as shown.
-
For the default (MSSQLSERVER) instance, run the following command:
NET START MSSQLSERVER /f /T3608
-
For a named instance, run the following command:
NET START MSSQL$instancename /f /T3608
For more information, see How to: Start an Instance of SQL Server (net Commands).
-
-
For each file to be moved, use sqlcmd commands or SQL Server Management Studio to run the following statement.
ALTER DATABASE database_name MODIFY FILE( NAME = logical_name , FILENAME = 'new_path\os_file_name' )
For more information about using the sqlcmd utility, see Using the sqlcmd Utility.
-
Exit the sqlcmd utility or SQL Server Management Studio.
-
Stop the instance of SQL Server. For example, run NET STOP MSSQLSERVER.
-
Move the file or files to the new location.
-
Restart the instance of SQL Server. For example, run NET START MSSQLSERVER.
-
Verify the file change by running the following query.
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID(N'<database_name>');
To move the master database, follow these steps.
-
From the Start menu, point to All Programs, point to Microsoft SQL Server, point to Configuration Tools, and then click SQL Server Configuration Manager.
-
In the SQL Server Services node, right-click the instance of SQL Server (for example, SQL Server (MSSQLSERVER)) and choose Properties.
-
In the SQL Server (instance_name) Properties dialog box, click the Advanced tab.
-
Edit the Startup Parameters values to point to the planned location for the master database data and log files, and click OK. Moving the error log file is optional.
The parameter value for the data file must follow the -d parameter and the value for the log file must follow the -l parameter. The following example shows the parameter values for the default location of the master data and log files.
-dC:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\ master.mdf;-eC:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\ LOG\ERRORLOG;-lC:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\ DATA\mastlog.ldf
If the planned relocation for the master data and log files is E:\SQLData, the parameter values would be changed as follows:
-dE:\SQLData\master.mdf;-eC:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\LOG\ERRORLOG;-lE:\SQLData\mastlog.ldf
-
Stop the instance of SQL Server by right-clicking the instance name and choosing Stop.
-
Move the master.mdf and mastlog.ldf files to the new location.
-
Restart the instance of SQL Server.
-
Verify the file change for the master database by running the following query.
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID('master'); GO
A. Moving the tempdb database
The following example moves the tempdb data and log files to a new location as part of a planned relocation.
Note
|
|---|
|
Because tempdb is re-created each time the instance of SQL Server is started, you do not have to physically move the data and log files. The files are created in the new location when the service is restarted in step 3. Until the service is restarted, tempdb continues to use the data and log files in existing location. |
-
Determine the logical file names of the tempdb database and their current location on the disk.
SELECT name, physical_name AS CurrentLocation FROM sys.master_files WHERE database_id = DB_ID(N'tempdb'); GO
-
Change the location of each file by using ALTER DATABASE.
USE master; GO ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'E:\SQLData\tempdb.mdf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'F:\SQLLog\templog.ldf'); GO
-
Stop and restart the instance of SQL Server.
-
Verify the file change.
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID(N'tempdb');
-
Delete the tempdb.mdf and templog.ldf files from the original location.
If you are moving databases to a new drive/volume or another server (different drive letter), you will find it useful to also change the SQL Server Agent log path as well as the Database Default Locations path. If you do not change the SQL Agent log path, SQL Agent will fail to start. If you do not modify the Database Default Locations path, creating a database may fail, if the original drive letter\path does not exist any more.
To change the SQL Agent log path:
1. In SQL Server Management Studio expand SQL Server Agent
2. Right-click on Errorlogs
3. Click on Configure
4. Change the Error Log File path to the new desired location
To change the Database Default Locations path
1. In SQL Server Management Studio right-click on the SQL Server name
2. Choose Prooperties
3. Choose Database Settings
4. Under "Database Default Locations" modify the Data and Log paths to the new, desired location
- 10/14/2011
- JPizho
- The location of the Resource database is <drive>:\Program Files\Microsoft SQL Server\MSSQL10_50.<instance_name>\MSSQL\Binn\. The database cannot be moved.
- The location of the Resource database is either
- <directory specified by /INSTANCEDIR install parameter>\MSSQL10_50.<instance_name>\MSSQL\Binn\
- Or the Default: <drive>:\Program Files\Microsoft SQL Server\MSSQL10_50.<instance_name>\MSSQL\Binn\
- 5/11/2011
- BluegrassWorkJam
If the system database "tempdb" is defined on several data files as recommended in general, each data file is moved by a ALTER DATABASE statement.
USE master;
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'T:\MSSQL\SANDY\tempdb.mdf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev_01, FILENAME = 'T:\MSSQL\SANDY\tempdb_01.ndf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev_02, FILENAME = 'T:\MSSQL\SANDY\tempdb_02.ndf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev_03, FILENAME = 'T:\MSSQL\SANDY\tempdb_03.ndf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'T:\MSSQL\SANDY\templog.ldf');
GO
The output of each statement is a message that the system catalog is modified and that the new path will be used after a restart of the database instance.
The file "tempdev" has been modified in the system catalog. The new path will be used the next time the database is started.
Please notice that you have to remove the old tmepdb files manually after the restart.
If the size of the files is configured before the move, a side benefit is de-fragmented files.
- 7/8/2010
- Niels Grove-Rasmussen
- 8/1/2010
- Thomas Lee
