How to move C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\master.mdf to G:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\master.mdf

Avyayah 1,291 Reputation points
2020-10-24T20:08:30.87+00:00

I have all system databases in C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\master.mdf. How do I move the system databases to G:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\master.mdf? Will it cause any issues. I have one database that is existing in the current sqlserver where the datafile is in G:\Data\x.mdf

SQL Server | Other
{count} votes

Answer accepted by question author
  1. Avyayah 1,291 Reputation points
    2020-10-26T13:44:41.6+00:00

    @Shashank Singh , used the below command to move the files. Deleted all tempdb datafles,log files and ndf files.

    USE master;
    GO
    ALTER DATABASE tempdb
    MODIFY FILE (NAME = tempdev, FILENAME = 'F:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\ tempdb.mdf');
    GO

    ALTER DATABASE tempdb
    MODIFY FILE (NAME = templog, FILENAME = 'E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\ templog.ldf');
    GO

    All temp files were created:

    SELECT name, physical_name AS CurrentLocation, state_desc
    FROM sys.master_files
    WHERE database_id = DB_ID('tempdb');
    GO

    name CurrentLocation state_desc
    tempdev F:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb.mdf ONLINE
    templog E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\templog.ldf ONLINE
    temp2 E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_2.ndf ONLINE
    temp3 E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_3.ndf ONLINE
    temp4 E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_4.ndf ONLINE

    .ndf files did not exist prior to moving the system databases so my question was any reason why it was generated? Will the .ndf files grow?


9 additional answers

Sort by: Most helpful
  1. Avyayah 1,291 Reputation points
    2020-10-25T14:09:16.917+00:00

    USE master;
    GO
    ALTER DATABASE tempdb
    MODIFY FILE (NAME = tempdev, FILENAME = 'F:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\ tempdb.mdf');
    GO

    ALTER DATABASE tempdb
    MODIFY FILE (NAME = templog, FILENAME = 'E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\ templog.ldf');
    GO

    Executed the statement, stopped the service, removed all files but found ndf files in the same location. Any other suggestions?

    tempdev F:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb.mdf ONLINE
    templog E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\templog.ldf ONLINE
    temp2 C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_2.ndf ONLINE
    temp3 C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_3.ndf ONLINE
    temp4 C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_4.ndf ONLINE


  2. Avyayah 1,291 Reputation points
    2020-10-25T15:36:56.897+00:00

    SELECT name, physical_name AS CurrentLocation, state_desc
    FROM sys.master_files
    WHERE database_id = DB_ID('tempdb');
    GO

    name CurrentLocation state_desc
    tempdev F:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb.mdf ONLINE
    templog E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\templog.ldf ONLINE
    temp2 E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_2.ndf ONLINE
    temp3 E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_3.ndf ONLINE
    temp4 E:\Log\MSSQL13.MSSQLSERVER\MSSQL\DATA\tempdb_mssql_4.ndf ONLINE

    ndf files now moved to new location. These ndf files were generated after I moved the system databases. Any reason why it was generated after moving the files?


  3. Cris Zhan-MSFT 6,671 Reputation points
    2020-10-26T08:53:14.213+00:00

    Hi @Avyayah ,

    >Followed the documentation and was able to move the system databases but why do I see the ndf files for tempdb?

    The tempdb in In your SQL Server instance contains multiple secondary data files.
    Each database has only one primary data file(.mdf). All other data files except the primary data file are scondary data files( .ndf). Where there are multiple database files, the .mdf file will always store the database’s internal configuration information and other internal system data.

    >ndf files now moved to new location. These ndf files were generated after I moved the system databases. Any reason why it was generated after moving the files?

    The tempdb is re-created every time SQL Server is started. Now that you have executed the "ALTER DATABASE tempdb MODIFY FILE(...)" command modified the physical_name of the tempdb .ndf files. After restarting the sql server service, the data file will be created in the new location.

    In addition, it seems that you have already got suggestions for moving the system database from the responses of others. So if the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  4. tibor_karaszi@hotmail.com 4,316 Reputation points
    2020-10-26T09:24:56.857+00:00

    Regarding the tempdb files: I have written a blog post regarding managing tempdb, including relocating database files. The key is that tempdb is created at startup based on sys.master_files regarding the database files. You find my blog post here: http://sqlblog.karaszi.com/managing-tempdb/

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.