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.
  1. Go to Computer Management, Action -> All tasks->, and then clickโ€ฏChange Drive Letter and Paths
SQL Server System Database files
  1. 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

  1. 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 
  1. Restart Services from Configuration manager.
  2. Confirm Path of Database files using the query.

Moving model database 

  1. 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

  1. Stop Services from Configuration manager.
  2. Copy the files to new location (E:\New folder\New Folder\MSSQL\Data1).
  3. Restart services.
  4. Confirm Path of Database files using the query.

Considering a career in cloud?

Moving msdb database 

  1. 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

  1. Stop Services from Configuration manager.
  2. Copy the files to new location (E:\New folder\New Folder\MSSQL\Data1).
  3. Restart services.
  4. 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 
  1. To move the master database, follow these steps.
  2. From theโ€ฏStartโ€ฏmenu, point toโ€ฏAll Programs, point toโ€ฏMicrosoft SQL Server, point toโ€ฏConfiguration Tools, and then clickโ€ฏSQL Server Configuration Manager.
  3. In theโ€ฏSQL Server Servicesโ€ฏnode, right-click the instance of SQL Server (for example,โ€ฏSQL Server (MSSQLSERVER)) and chooseโ€ฏProperties.
  4. In theโ€ฏSQL Server (XXXXXXX) Propertiesโ€ฏdialog box, click theโ€ฏStartup Parametersโ€ฏtab.
  5. In theโ€ฏExisting parametersโ€ฏbox, select the โ€“d parameter to move the master data file.
  6. In theโ€ฏSpecify a startup parameterโ€ฏbox, change the parameter to the new path of the master database.
  7. In theโ€ฏExisting parametersโ€ฏbox, select the โ€“l parameter to move the master log file.
  8. In theโ€ฏSpecify a startup parameterโ€ฏbox, change the parameter to the new path of the master database.
  9. 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 

 

  1. Stop the instance of SQL Server by right-clicking the instance name and choosingโ€ฏStop.
  2. Move the master.mdf and mastlog.ldf files to the new location.
  3. Restart the instance of SQL Server. 
  4. 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 

  1. From SQL Server Management Studio, in Object Explorer, expandโ€ฏSQL Server Agent.
  2. Right-clickโ€ฏError Logsโ€ฏand clickโ€ฏConfigure.
  3. In theโ€ฏConfigure SQL Server Agent Error Logsโ€ฏdialog box, specify the new location of the SQLAGENT.OUT file.
  1. The default location is E:\SystemFile\MSSQL10_50. XXXXXXX \MSSQL\Log\SQLAGENT.OUT\
  2. Change it to E:\New folder\New Folder\MSSQL\LOG\ SQLAGENT.OUT 

Author: Mayank Minal, Principal Consultant, Fusion Practices

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *