System database movement is one of the best practices after sql server installations.
2. Set the database offline
(use
Just copy the files to new location
4. Bring the database online
Example:
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 locationJust 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:
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
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
After moving the files Start SQL Service.
To move other system databases except master database:
Make sure to move both ldf and mdf files except for tempdb.
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.
- Log in as sa user in SSMS
- Take a backup of the user created database for safety.
- Kill all sessions connected to the Server from SSMS.
- Execute the following command to check the current file location of system databases:
USE master;
SELECT * FROM sys.master_files;
- 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' )
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.
- Stop the instance of SQL Server.
- Copy the file or files to the new location.