Transferring Data from Windows SQL Server 2016 to Linux SQL Server 2017
This guide covers migrating a database from a Windows-based SQL Server 2016 instance to a Linux-based SQL Server 2017 instance. The target environment is a Linux server lacking resources for a dedicated Windows machine.
Installing SQL Server 2017 on Linux
Follow these steps to install SQL Server 2017 on a RHEL 7-based Linux distribution:
- Add the Microsoft repository: ```
curl https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo > /etc/yum.repos.d/mssql-server.repo
- Install the package: ```
yum install -y mssql-server
- Run the setup wizard: ```
sudo /opt/mssql/bin/mssql-conf setup
During setup, select option `2` for the Developer (free) edition. Confirm the end-user license agreement by selecting `Yes`, then set the `sa` password. The service will start automatically after installation. - Verify the installation: ```
rpm -qa | grep mssql
- Enable automatic startup: ```
systemctl enable mssql-server
Note: The repository at https://packages.microsoft.com/config/rhel/7/ currently contains only SQL Server 2017 and 2019 repositories. Other versions must be sourced separately.
Enabling SQL Server Agent
SQL Server Agent is disabled by default. Enable it with:
sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true
sudo systemctl restart mssql-server
Exporting Data from Windows SQL Server 2016
Two recommended methods exist for exporting data:
Method 1: SQL Script Export
SQL Server Management Studio (SSMS) can generate a script containing both schema and data. Use the following steps:
- Open SSMS and connect to the source server.
- Right-click the database, select Tasks > Generate Scripts.
- In the wizard, choose Select specific database objects and pick the required tables.
- In the Advanced options, set Script for Server Version to SQL Server 2017 and Script Data Types to Schema and Data.
- Save the script as a
.sqlfile.
This method may produce issues with certain data types or dependencies, so consider Method 2.
Method 2: Binary Backup (.bak) File
Creating a full database backup is often more reliable:
- In SSMS, right-click the database, select Tasks > Back Up.
- In the Back Up Database dialog, choose Backup type as Full.
- Under Destination, specify a file path and name with a
.bakextension. - Optionally, enable compression: go to Backup Options and check Compress backup. Compression typically reduces file size by 3:1 to 4:1, beneficial for long-term storage.
- Click OK to create the backup.
Importing Data to Linux SQL Server 2017
Setting File Permissions
Ensure the backup file has correct ownership for the mssql user:
id mssql
chown -R mssql:mssql /path/to/db-20220222/
Restoring the Database
Use SSMS or sqlcmd to restore. If using SSMS:
- Connect to the Linux SQL Server instance.
- Right-click Databases, select Restore Database.
- Choose Device and browse to the
.bakfile. - In Options, uncheck Take tail-log backup before restore to avoid errors like "BACKUP LOG cannot be performed because there is no current database backup."
- Check Overwrite the existing database (WITH REPLACE) if necessary.
- Click OK to restore.
Default Data Locations
- Data files:
/var/opt/mssql/data - Executables:
/opt/mssql - Configuration:
/var/opt/mssql/mssql.conf
To migrate storage to a custom location, e.g., /data/mssql_data:
- Set permissions: ```
chown -R mssql:mssql /data/mssql_data
- Update configuration via
mssql-conf: ``` /opt/mssql/bin/mssql-conf set filelocation.defaultdatadir /data/mssql_data /opt/mssql/bin/mssql-conf set filelocation.defaultlogdir /data/mssql_data /opt/mssql/bin/mssql-conf set filelocation.defaultbackupdir /data/mssql_data - Restart the service: ```
systemctl restart mssql-server
The `mssql.conf` file will contain: ``` [sqlagent] enabled = true [EULA] accepteula = Y [filelocation] defaultdatadir = /data/mssql_data defaultlogdir = /data/mssql_data defaultbackupdir = /data/mssql_data
New databases will be created in the new location. Old databases can be removed after migration.
Bulk Exporting SQL Agent Jobs
To export all SQL Agent jobs to a script:
- In SSMS, navigate to SQL Server Agent > Jobs.
- Press F7 to open Object Explorer Details.
- Select multiple jobs (using Ctrl or Shift), right-click, and choose Script Job as > CREATE To > File.
For scheduled backups, consider using Maintenance Plans under Management > Maintenance Plan Wizard.