Change the Default Data or Log Directory Location on SQL Server on Linux

When you install SQL Server on Linux, the default instance file location is setup to ‘C:\var\opt\mssql\data\’ for data and log file.  Sometimes, it is necessary to relocate the physical files to different drives to get better throughput.

In this post, we shall walkthrough a step by step process of changing the default location of data or log directory on SQL Server on Linux system so that if there is a need of changing the files drive, you can easily achieve it by referring this blog.

Let’s connect to SQL Server Linux Instance using SSMS and go to instance property, select ‘Database Settings’ tab, and get the database default location.

01defaultfileslocationssms

You can also get this information using SERVERPROPERTY () function. From SQL Server 2016, there is an enhancement added to the SERVERPROPERTY () function. Using that function, you can check the default location of data and log file directories.

Syntax:

SELECT
SERVERPROPERTY(‘InstanceDefaultDataPath’) AS InstanceDefaultDataFilePath,
SERVERPROPERTY(‘InstanceDefaultLogPath’) AS InstanceDefaultLogFilePath

01defaultfileslocation

 

Change the default data or log directory

  • Connect your Linux machine as a root user

Syntax: sudo -i

01connectasrootuser

  • Create the directories for data and log files where the new database’s files will reside. For example, I am going to create data file directory at /var/sqldata and log file directory at /var/sqllog locations.

   The below command will create both the folders in one attempt

Syntax: mkdir /var/sqldata  /var/sqllog

03createnewdirectoryfordatalog

  • Verify the created data  and log file folders

Syntaxls -l

04verifycreatedfolderlocation

  • The above step confirms that the directories got created. But the user and group of the folders are roots, let’s replace them to ‘mssql’ so that SQL Server service can access those folders.

   Change the user from root to mssql for the directories.

Syntax: chown mssql /var/sqldata  /var/sqllog

05changetheuserforboth-directories

  Change the group from root to mssql for the directories.

Syntax: chgrp mssql /var/sqldata  /var/sqllog

06changethegroupforbotdirectories

  • Use mssql-conf to change the default data and log directories with the “set” command:

   Replace the default location of data file with the new location

Syntax: /opt/mssql/bin/mssql-conf set defaultdatadir /var/sqldata

07changethedatafilelocation

    Replace the default location of log file with the new location

Syntax: /opt/mssql/bin/mssql-conf set defaultlogdir /var/sqllog

08changethelogfilelocation

  • When you change the default location directory of the data file, log file or both the files,  you need to restart the SQL Server service to get the effect of new location(s)

Syntax: systemctl restart mssql-server

04restartthesqlserverservice

  • Let’s verify the newly updated default location

   Via SSMS

 09verifythefileslocationviassms

   Via Function

10verifythefileslocationviafunction

Important Notes

  • In CTP1 (public preview), when you change the default location of data directory and restart the SQL Server service, the tempdb database data and log files get created into the new location of the data directory.
  • If you want to put tempdb data file and log files into two separate folders, you won’t be able to do that because changing the locations of TempDB data and log files is not supported in CTP1.
  • System databases can not be moved with the mssql-conf utility

Hope, you enjoyed learning how to change the default data or log file directory location on SQL Server on Linux.

Leave a comment

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