| description | Move System Databases | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| title | Move System Databases | ||||||||||||||||
| ms.custom | |||||||||||||||||
| ms.date | 02/24/2022 | ||||||||||||||||
| ms.prod | sql | ||||||||||||||||
| ms.prod_service | database-engine | ||||||||||||||||
| ms.reviewer | |||||||||||||||||
| ms.technology | |||||||||||||||||
| ms.topic | conceptual | ||||||||||||||||
| helpviewer_keywords |
|
||||||||||||||||
| author | WilliamDAssafMSFT | ||||||||||||||||
| ms.author | wiassaf |
[!INCLUDE SQL Server]
This article describes how to move system databases in [!INCLUDEssNoVersion]. 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 [!INCLUDEssNoVersion]. To move a database to another instance of [!INCLUDEssNoVersion] or to another server, use the backup and restore operation.
The procedures in this article 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.
To move a system database data or log file as part of a planned relocation or scheduled maintenance operation, follow these steps. This includes the model, msdb, and tempdb system databases.
Important
This procedure applies to all system databases except the master and Resource databases. See later in this article for steps to move the master database. The Resource database cannot be moved.
-
Record the existing location of the database files you intend to move, by reviewing the sys.master_files catalog view.
-
Verify that the service account for the [!INCLUDEssNoVersion] [!INCLUDEssDE] has full permissions to the new location of the files. For more information, see Configure Windows Service Accounts and Permissions. If the [!INCLUDEssDE] service account cannot control the files in their new location, the [!INCLUDEssNoVersion] instance will not start.
-
For each database file to be moved, run the following statement.
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name , FILENAME = 'new_path\os_file_name' );
Until the service is restarted, the database continues to use the data and log files in the existing location.
-
Stop the instance of [!INCLUDEssNoVersion] to perform maintenance. For more information, see Start, Stop, Pause, Resume, Restart the Database Engine, SQL Server Agent, or SQL Server Browser Service.
-
Copy the database file or files to the new location. Note that this is not a necessary step for the
tempdbsystem database, those files will be created in the new location automatically. -
Restart the instance of [!INCLUDEssNoVersion] or the server. For more information, see Start, Stop, Pause, Resume, Restart the Database Engine, SQL Server Agent, or SQL Server Browser Service.
-
Verify the file change by running the following query. The system databases should report the new physical file locations.
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID(N'<database_name>');
-
Since in Step 5 you copied the database files instead of moving them, now you can safely delete the unused database files from their previous location.
If the msdb database is moved and Database Mail is configured, complete the following additional steps.
-
Verify that [!INCLUDEssSB] is enabled for the
msdbdatabase by running the following query.SELECT is_broker_enabled FROM sys.databases WHERE name = N'msdb';
If the [!INCLUDEssSB] is not enabled for
msdb, it must be re-enabled for Database Mail to function. For more information, see ALTER DATABASE ... SET ENABLE_BROKER.ALTER DATABASE msdb SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;
Confirm that the value of
is_broker_enabledis now 1. -
Verify that Database Mail is working by sending a test 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. The following examples use the Windows command-line prompt and sqlcmd Utility.
Important
If the database cannot be started, if it is in suspect mode or in an unrecovered state, only members of the sysadmin fixed role can move the file.
-
Verify that the service account for the [!INCLUDEssNoVersion] [!INCLUDEssDE] has full permissions to the new location of the files. For more information, see Configure Windows Service Accounts and Permissions. If the [!INCLUDEssDE] service account cannot control the files in their new location, the [!INCLUDEssNoVersion] instance will not start.
-
Stop the instance of [!INCLUDEssNoVersion] if it is started.
-
Start the instance of [!INCLUDEssNoVersion] in master-only recovery mode by entering one of the following commands at the command prompt. Using startup parameter 3608 prevents SQL Server from automatically starting and recovering any database except the
masterdatabase. For more information, see Startup Parameters and TF3608.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 /T3608For a named instance, run the following command:
NET START MSSQL$instancename /f /T3608For more information, see Start, Stop, Pause, Resume, Restart the Database Engine, SQL Server Agent, or SQL Server Browser Service.
-
Promptly after service startup with trace flag 3608 and
/f, start a sqlcmd connection to the server, to claim the single connection that is available. For example, when executing sqlcmd locally on the same server as the default (MSSQLSERVER) instance, and to connect with Active Directory integration authentication, run the following command:sqlcmd
To connect to a named instance on the local server, with Active Directory integration authentication:
sqlcmd -S localhost\instancename
For more information on sqlcmd syntax, see sqlcmd utility.
For each file to be moved, use sqlcmd commands or [!INCLUDEssManStudioFull] to run the following statement. For more information about using the sqlcmd utility, see Use the sqlcmd Utility. Once the sqlcmd session is open, run the following statement once for each file to be moved:
ALTER DATABASE database_name MODIFY FILE( NAME = logical_name , FILENAME = 'new_path\os_file_name' ) GO
-
Exit the sqlcmd utility or [!INCLUDEssManStudioFull].
-
Stop the instance of [!INCLUDEssNoVersion]. For example, run
NET STOP MSSQLSERVERin the command-line prompt. -
Copy the file or files to the new location.
-
Restart the instance of [!INCLUDEssNoVersion]. For example, run
NET START MSSQLSERVERin the command-line prompt. -
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>');
-
Since in Step 7 you copied the database files instead of moving them, now you can safely delete the unused database files from their previous location.
To move the master database, follow these steps.
-
Verify that the service account for the [!INCLUDEssNoVersion] [!INCLUDEssDE] has full permissions to the new location of the files. For more information, see Configure Windows Service Accounts and Permissions. If the [!INCLUDEssDE] service account cannot control the files in their new location, the [!INCLUDEssNoVersion] instance will not start.
-
From the Start menu, locate and launch SQL Server Configuration Manager. For more information on the expected location, see SQL Server Configuration Manager.
-
In the SQL Server Services node, right-click the instance of [!INCLUDEssNoVersion] (for example, SQL Server (MSSQLSERVER)) and choose Properties.
-
In the SQL Server (instance_name) Properties dialog box, select the Startup Parameters tab.
-
In the Existing parameters box, select the
-dparameter. In the Specify a startup parameter box, change the parameter to the new path of themasterdata file. Select Update to save the change. -
In the Existing parameters box, select the
-lparameter. In the Specify a startup parameter box, change the parameter to the new path of themasterlog file. Select Update to save the change.The parameter value for the data file must follow the
-dparameter and the value for the log file must follow the-lparameter. The following example shows the parameter values for the default location of themasterdata file.-dC:\Program Files\Microsoft SQL Server\MSSQL<version>.MSSQLSERVER\MSSQL\DATA\master.mdf-lC:\Program Files\Microsoft SQL Server\MSSQL<version>.MSSQLSERVER\MSSQL\DATA\mastlog.ldfIf the planned relocation for the
masterdata file isE:\SQLData, the parameter values would be changed as follows:-dE:\SQLData\master.mdf-lE:\SQLData\mastlog.ldf -
Select OK to save the changes permanently and close the SQL Server (instance_name) Properties dialog box.
-
Stop the instance of [!INCLUDEssNoVersion] by right-clicking the instance name and choosing Stop.
-
Copy the
master.mdfandmastlog.ldffiles to the new location. -
Restart the instance of [!INCLUDEssNoVersion].
-
Verify the file change for the
masterdatabase by running the following query.SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID('master');
-
At this point [!INCLUDEssNoVersion] should run normally. However Microsoft recommends also adjusting the registry entry at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\instance_ID\Setup, where instance_ID is likeMSSQL13.MSSQLSERVER. In that hive, change theSQLDataRootvalue to the new path of the new location of themasterdatabase files. Failure to update the registry can cause patching and upgrading to fail. -
Since in Step 9 you copied the database files instead of moving them, now you can safely delete the unused database files from their previous location.
The location of the Resource database is \<*drive*>:\Program Files\Microsoft SQL Server\MSSQL\<version>.\<*instance_name*>\MSSQL\Binn\\. The database cannot be moved.
If you have moved all of the system databases to a new drive or volume or to another server with a different drive letter, make the following updates.
-
Change the SQL Server Agent log path. If you do not update this path, SQL Server Agent will fail to start.
-
Change the database default location. Creating a new database may fail if the drive letter and path specified as the default location do not exist.
If you have moved all of the system databases to a new volume or have migrated to another server with a different drive letter, and the path of the SQL Agent error log file SQLAGENT.OUT no longer exists, make the following updates.
-
From SQL Server Management Studio, in Object Explorer, expand SQL Server Agent.
-
Right-click Error Logs and select Configure.
-
In the Configure SQL Server Agent Error Logs dialog box, specify the new location of the SQLAGENT.OUT file. The default location is
C:\Program Files\Microsoft SQL Server\MSSQL\<version>.<instance_name>\MSSQL\Log\\.
-
From SQL Server Management Studio, in Object Explorer, connect to the desired [!INCLUDEssNoVersion] instance. Right-click the instance and select Properties.
-
In the Server Properties dialog box, select Database Settings.
-
Under Database Default Locations, browse to the new location for both the data and log files.
-
Stop and start the [!INCLUDEssNoVersion] service to complete the change.
The following example moves the tempdb data and log files to a new location as part of a planned relocation.
Tip
Take this opportunity to review your tempdb files for optimal size and placement. For more information, see Optimizing tempdb performance in SQL Server.
Because tempdb is re-created each time the instance of [!INCLUDEssNoVersion] 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 4. Until the service is restarted, tempdb continues to use the data and log files in the existing location.
-
Determine the logical file names of the
tempdbdatabase 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
-
Verify that the service account for the [!INCLUDEssNoVersion] [!INCLUDEssDE] has full permissions to the new location of the files. For more information, see Configure Windows Service Accounts and Permissions. If the [!INCLUDEssDE] service account cannot control the files in their new location, the [!INCLUDEssNoVersion] instance will not start.
-
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
Until the service is restarted,
tempdbcontinues to use the data and log files in the existing location. -
Stop and restart the instance of [!INCLUDEssNoVersion].
-
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 unused
tempdbfiles from their original location.