System database relocation

System database movement is one of the best practices after sql server installations.

It is important to keep SQL Server files away from C drive location where windows operating system is installed, in order to avoid corruption or crashes.

Steps need to follow for changing location of mdf and ldf:

1. Change the file locations with an ALTER DATABASE command:
USE master; --do this all from the master
ALTER DATABASE foo
MODIFY FILE (name='logical name
,filename='Location'); --Filename is new location
Changing this path does not take effect immediately, but will be used the next time the database starts up.

2. Set the database offline
(use WITH ROLLBACK IMMEDIATE to close sessions that are active and rollback all currently open transactions)
ALTER DATABASE DBNAME SET OFFLINE WITH ROLLBACK IMMEDIATE;
3. Move/Copy the files to the new location
Just copy the files to new location
4. Bring the database online
ALTER DATABASE DBNAME SET ONLINE;
You can see this described in more detail here.

Example:

To Move system database files follow these steps:

To change master database location:
open SQL Server Configuration Manager.
Click on the SQL Server Service on the left side of the window and then locate SQL Server (MSSQLSERVER) on the right side and then right-click and select Properties.
By default three existing parameters listed as shown below in picture.  The master.mdf (-d)master.ldf (-l) and ERRORLOG (-e).
For moving the master database we will be modifying the -d and -l parameters.
Click on the parameter beginning with -d & -l. In the Specify a startup parameter text box, modify the path as needed.
Click Update.
Once done stop the SQL Service & physically move the master.mdf and master.ldf files to their new location.
After moving the files Start SQL Service. 



To move other system databases except master database:
  1. Log in as sa user in SSMS
  2. Take a backup of the user created database for safety.
  3. Kill all sessions connected to the Server from SSMS.
  4. Execute the following command to check the current file location of system databases:
    USE master;
    SELECT * FROM sys.master_files;
Identify the path and note the current path of the files.
  1. Use TSQL to change the file path for all database except master:
    ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name , FILENAME = 'new_path\os_file_name' )
Eg:
ALTER DATABASE tempdb
MODIFY FILE ( NAME = tempdev
, FILENAME = "DestinationPath\tempdb.mdf");

ALTER DATABASE tempdb
MODIFY FILE ( NAME = templog
, FILENAME = "DestinationPath\templog.ldf");

ALTER DATABASE model
MODIFY FILE ( NAME = modeldev
, FILENAME = "DestinationPath\model.mdf");

ALTER DATABASE model
MODIFY FILE ( NAME = modellog
, FILENAME = "DestinationPath\modellog.ldf");

ALTER DATABASE msdb
    MODIFY FILE ( NAME = MSDBData
, FILENAME = "DestinationPath\msdbdata.mdf");

ALTER DATABASE msdb
    MODIFY FILE ( NAME = MSDBLog
, FILENAME = "DestinationPath\msdblog.ldf");
Now the file location has been changed.
Make sure to move both ldf and mdf files except for tempdb.
  1. Stop the instance of SQL Server.
  2. Copy the file or files to the new location. 

Post a Comment