How to...
Move Database Files
If data files are located on the C: drive, at a minimum, move the user databases and tempdb to a different drive if possible.
Contents
- Moving user databases
- Special considerations for MSDB
- Moving the TempDB
- Moving the master database
Details
Moving user databases
1. Prepare the new location
2. Ensure a permanent drive letter is assigned.
3. Ensure enough space is available.
4. Set permissions on the new folder for the Service account running SQL.
5. For each file to be moved, run the following statement (The master database steps are blow).
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name , FILENAME = 'new_path\os_file_name' )
6. Stop the instance of SQL Server or shut down the system to perform maintenance.
(or for non-system databases, take the database offline)
7. Move the file or files to the new location.
8. Restart the instance of SQL Server or the server.
(or bring the database back online)
9. 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>');
Special considerations for MSDB
If the msdb database is moved and the instance of SQL Server is configured for Database Mail, complete these additional steps.
1. 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';
2. Verify that Database Mail is working by sending a test mail.
Moving the TempDB
1. 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');
2. 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');
3. Stop and restart the instance of SQL Server.
4. Verify the file change.
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'tempdb');
5. Delete the tempdb.mdf and templog.ldf files from the original location.
Moving the master database
1. From the Start menu, point to All
Programs, point to Microsoft SQL Server, point to Configuration
Tools, and then click SQL Server Configuration Manager.
2. In the
SQL Server Services node, right-click the instance of SQL Server
(for example, SQL Server (MSSQLSERVER)) and choose Properties.
3.
In the SQL Server (instance_name) Properties dialog box, click the
Startup Parameters tab.
4. In the Existing parameters box, select
the -d parameter to move the master data file. Click Update to save
the change.
In the Specify a startup parameter box, change the
parameter to the new path of the master database.
5. In the
Existing parameters box, select the -l parameter to move the master
log file. Click Update to save the change.
In the Specify a
startup parameter box, change the parameter to the new path of the
master database.
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 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.ldf
6. Stop the instance of SQL Server by right-clicking the instance name and choosing Stop.
7. Move the master.mdf and mastlog.ldf files to the new location.
8. Restart the instance of SQL Server.
9. 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
10. At this point SQL Server 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 like MSSQL13.MSSQLSERVER. In that hive, change
the SQLDataRoot value to the new path. Failure to update the
registry can cause patching and upgrading to fail.
References:
https://docs.microsoft.com/en-us/sql/relational-databases/databases/move-system-databases?view=sql-server-2017
