Moving SQL Server System Tables
Moving SQL Server System Tables sounds like it's going to be a complicated and fairly long-winded task, but with a few SQL queries, you're good to go.
Before you start, make sure that the user you've used to connect to your instance of MSSQL Server has the correct permissions to perform administrative tasks. You'll need to have a user that's part of the sysadmin group. I also assume that you're using SQL Server Management Studio, which can be obtained at the following address:
Queries for moving the system databases:
To find out the logical names of all the database files for a database, you'll need to run the following query:
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID(N'tempdb');
The above query will display a row for each file that the database is attached to and a logical name for each one.
Note: You can change the query to use another database by replacing 'tempdb' with the database name.
The below example displays what was displayed to me when I ran the query for the master database:
|master||C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\master.mdf||ONLINE|
|masterlog||C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\mastlog.ldf||ONLINE|
You can run the following command to move a database file location:
USE master; GO ALTER DATABASE databasename MODIFY FILE (NAME = logicalname, FILENAME = 'F:\myfolder\databasefilename.mdf'); GO
Here's an example to move the tempdb 'database' files:
USE master; GO ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'F:\TempDB\tempdb.mdf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'F:\TempDB\templog.ldf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = temp2, FILENAME = 'F:\TempDB\tempdb_mssql_2.ndf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = temp3, FILENAME = 'F:\TempDB\tempdb_mssql_3.ndf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = temp4, FILENAME = 'F:\TempDB\tempdb_mssql_4.ndf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = temp5, FILENAME = 'F:\TempDB\tempdb_mssql_5.ndf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = temp6, FILENAME = 'F:\TempDB\tempdb_mssql_6.ndf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = temp7, FILENAME = 'F:\TempDB\tempdb_mssql_7.ndf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = temp8, FILENAME = 'F:\TempDB\tempdb_mssql_8.ndf'); GO
Once you've run the command, restart the SQL Server instance to make it use the newly stored configuration
Note: The tempdb database does not require you to move any files, but other databases do, so make sure you do that before restarting the SQL Server instance.
Issues that might occur:
If you've moved the 'msdb' database and restarted the SQL Server instance, you might run into an issue where the database displays '(recovery mode)' after the database name, in this case, you need to make sure that you've manually moved the database files from 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA' or wherever your SQL Server instance has been installed, to the new folder that you specified above. You also need to make sure that Network Service has full access to the aforementioned folder.
Reasons for moving database files:
You may have your system set up to use solid SATA drives and wish to extend drive lifetime by reducing writes, in this case, moving the 'tempdb' files to a hard drive is a good idea.
You may want to store database files on another drive to save on disk space on drives that have little remaining space.
Microsoft documentation for moving system databases:
Microsoft documentation for the msdb database:
Published at 22 Feb 2018, 11:25 AM