Migrating a SQL Server database to Ubuntu involves several critical steps to ensure data integrity, compatibility, and minimal downtime. Below is a structured guide covering preparation, core migration methods, post-migration validation, and optimization.
Before starting the migration, address these essential prerequisites:
mssql-server
package. Post-installation, configure the SA password and enable the service.Choose the method that aligns with your migration complexity and tool preference:
This method is ideal for most scenarios, offering a balance of simplicity and reliability.
sqlcmd
to create a full backup. For SSMS: right-click the database → Tasks → Backup; select “Full” type and specify a local path. For sqlcmd
:BACKUP DATABASE [YourDatabase] TO DISK = N'/path/to/backup/YourDatabase.bak' WITH INIT, STATS = 10;
scp
(secure copy) to move the .bak
file to the Ubuntu server. Example:scp /path/to/backup/YourDatabase.bak user@ubuntu_server_ip:/var/opt/mssql/backup/
sqlcmd
to restore the database, adjusting file paths to Ubuntu’s SQL Server data directory (/var/opt/mssql/data/
). Example command:RESTORE DATABASE [YourDatabase]
FROM DISK = N'/var/opt/mssql/backup/YourDatabase.bak'
WITH MOVE 'YourDatabase_Data' TO '/var/opt/mssql/data/YourDatabase.mdf',
MOVE 'YourDatabase_Log' TO '/var/opt/mssql/data/YourDatabase_Log.ldf',
REPLACE, RECOVERY;
Replace YourDatabase_Data
and YourDatabase_Log
with the actual logical names from the source database (viewable via RESTORE FILELISTONLY
).For complex migrations requiring data transformation (e.g., schema changes, data cleansing), use SSIS:
For high-performance migration of large tables, use the bcp
(Bulk Copy Program) utility:
bcp
to export table data to a CSV file:bcp YourDatabase.dbo.YourTable out /path/to/export/YourTable.csv -c -t, -S source_server -U sa -P YourPassword
scp
to copy CSV files to the Ubuntu server.bcp
to import data into the restored database:bcp YourDatabase.dbo.YourTable in /var/opt/mssql/backup/YourTable.csv -c -t, -S localhost -U sa -P YourPassword
This method is faster than sqlcmd
for large datasets but requires manual schema creation beforehand.After restoring the database, perform these checks to ensure success:
CHECKSUM TABLE
in MySQL or custom scripts) between source and target databases.Server=ubuntu_server_ip;Database=YourDatabase;User Id=sa;Password=YourPassword;
). Run functional tests to confirm the app interacts with the database correctly./var/opt/mssql/log/errorlog
) for any migration-related errors or warnings.UPDATE STATISTICS
on all tables to ensure the query optimizer has accurate data distribution information.ALTER INDEX ALL ON YourTable REBUILD;
mssql
user has read/write access to the backup directory and data files./etc/mssql/mssql.conf
), and network connectivity between source and target servers.By following this guide, you can successfully migrate your SQL Server database to Ubuntu while minimizing risks and ensuring data integrity. Always test migrations in a non-production environment first to identify and resolve potential issues.