How to move SQL Server System Database files to another drive
Problem: It occasionally becomes necessary to move SQL Server system databasesโ file to a new location. In this blog article understand the process of moving tempdb, master, msdb, and model system databases to a different location, it is crucial to keep in mind that moving the resource database files is not possible.
How to create Mount drive in Local system
Steps to assign a mount-point folder path to a drive.
- Go to Computer Management, Action -> All tasks->, and then clickโฏChange Drive Letter and Paths.
- To assign a mount-point folder path, clickโฏAdd. ClickโฏMount in the following empty NTFS folder, type the path to an empty folder on an NTFS volume, or clickโฏBrowseโฏto locate it. Here I created in E$ (E:\New folder\New Folder)
Scenario
Named Instance Name: XXXXXXX
Current location of system database mentioned in below table.
name | Physical name |
Master | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\master.mdf |
mastlog | E:\SystemFile\MSSQL10_50. XXXXXXX\MSSQL\DATA\mastlog.ldf |
modeldev | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\model.mdf |
modellog | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\modellog.ldf |
MSDBData | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\MSDBData.mdf |
MSDBLog | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\MSDBLog.ldf |
tempdev | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\tempdb.mdf |
templog | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\templog.ldf |
Created Mount Drive in my Local system E$
New Physical path in mount drive will be in E:\New folder\New Folder\MSSQL\DATA
name | Physical name |
Master | E:\New folder\New Folder\MSSQL\Data1\master.mdf |
mastlog | E:\New folder\New Folder\MSSQL\Data1\mastlog.ldf |
modeldev | E:\New folder\New Folder\MSSQL\Data1\model.mdf |
modellog | E:\New folder\New Folder\MSSQL\Data1\modellog.ldf |
MSDBData | E:\New folder\New Folder\MSSQL\Data1\MSDBData.mdf |
MSDBLog | E:\New folder\New Folder\MSSQL\Data1\MSDBLog.ldf |
tempdev | E:\New folder\New Folder\MSSQL\Data1\tempdb.mdf |
templog | E:\New folder\New Folder\MSSQL\Data1\templog.ldf |
Moving TempDB database
- Execute below script
–Moving TempDB
Use master
Go
Alter database tempdb modify file (Name =tempdev, filename=’E:\New folder\New Folder\MSSQL\Data1\tempdb.mdf’)
Go
Alter database tempdb modify file (Name =templog, filename=’E:\New folder\New Folder\MSSQL\Data1\templog.ldf’)
Go
- Restart Services from Configuration manager.
- Confirm Path of Database files using the query.
Moving model database
- Execute below script
Before Moving:
use model
go
select name,physical_name ,* from sys.database_files
modeldev | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\model.mdf |
modellog | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\modellog.ldf |
Use master
go
alter database model modify file (Name =modeldev,filename=’E:\New folder\New Folder\MSSQL\Data1\model.mdf’)
go
alter database model modify file (Name =modellog,filename=’E:\New folder\New Folder\MSSQL\Data1\modellog.ldf’)
go
- Stop Services from Configuration manager.
- Copy the files to new location (E:\New folder\New Folder\MSSQL\Data1).
- Restart services.
- Confirm Path of Database files using the query.
Considering a career in cloud?
Moving msdb database
- Execute below script
Before Moving:
use msdb
go
select name,physical_name ,* from sys.database_files
MSDBData | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\MSDBData.mdf |
MSDBLog | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\MSDBLog.ldf |
Use master
go
alter database msdb modify file (Name =MSDBData,filename=’E:\New folder\New Folder\MSSQL\Data1\MSDBData.mdf’)
go
Use master
go
alter database msdb modify file (Name =MSDBData,filename=’E:\New folder\New Folder\MSSQL\Data1\MSDBData.mdf’)
go
alter database msdb modify file (Name =MSDBLog,filename=’E:\New folder\New Folder\MSSQL\Data1\MSDBLog.ldf’)
go
- Stop Services from Configuration manager.
- Copy the files to new location (E:\New folder\New Folder\MSSQL\Data1).
- Restart services.
- Confirm Path of Database files using the query.
Moving the master database
At present master database is present under following path E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\master.mdf and our requirement is to move its mdf and ldf file to the path name E:\New folder\New Folder\MSSQL\Data1.
name | Physical name |
Master | E:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\master.mdf |
mastlog | E:\SystemFile\MSSQL10_50. XXXXXXX\MSSQL\DATA\mastlog.ldf |
- 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 (XXXXXXX) Propertiesโฏdialog box, click theโฏStartup Parametersโฏtab.
- In theโฏExisting parametersโฏbox, select the โd parameter to move the master data file.
- In theโฏSpecify a startup parameterโฏbox, change the parameter to the new path of the master database.
- In theโฏExisting parametersโฏbox, select the โl parameter to move the master log file.
- 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.
-dE:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\master.mdf;-eE:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\Log\ERRORLOG;-lE:\ SystemFile \MSSQL10_50. XXXXXXX \MSSQL\DATA\mastlog.ldf
New Location for the master data file isโฏE:\New folder\New Folder\MSSQL\Data1, the parameter values would be changed as follows:
-dE:\New folder\New Folder\MSSQL\Data1\master.mdf;-eE:\Documents + Ebooks\MSSQL10_50.TIFFANY\MSSQL\Log\ERRORLOG;-lE:\New folder\New Folder\MSSQL\Data1\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.
Follow-up: After Moving All System Databases
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.
Change the SQL Server Agent Log Path
- From SQL Server Management Studio, in Object Explorer, expandโฏSQL Server Agent.
- Right-clickโฏError Logsโฏand clickโฏConfigure.
- In theโฏConfigure SQL Server Agent Error Logsโฏdialog box, specify the new location of the SQLAGENT.OUT file.
- The default location is E:\SystemFile\MSSQL10_50. XXXXXXX \MSSQL\Log\SQLAGENT.OUT\
- Change it to E:\New folder\New Folder\MSSQL\LOG\ SQLAGENT.OUT