Migrating SQL Server Databases to Ubuntu: A Step-by-Step Guide
Migrating a SQL Server database to Ubuntu involves transferring data from a source server (Windows or another Ubuntu instance) to a target Ubuntu server running SQL Server. Below are the most effective methods, detailed steps, and best practices to ensure a smooth migration.
Before starting, complete these prerequisites to avoid errors:
Install SQL Server on Ubuntu:
sudo apt-get update.curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.curl https://packages.microsoft.com/config/ubuntu/22.04/mssql-server-2022.list | sudo tee /etc/apt/sources.list.d/mssql-server.list
sudo apt-get install -y mssql-server.sudo /opt/mssql/bin/mssql-conf setup, set the SA password (follow SQL Server’s complexity rules), and complete the setup.sudo systemctl start mssql-server and enable auto-start: sudo systemctl enable mssql-server.Install Command-Line Tools:
SQL Server tools (sqlcmd, bcp) are required for command-line operations. Install them using:
sudo apt-get install -y mssql-tools unixodbc-dev
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
Verify installation: sqlcmd -S localhost -U SA -P '<YourPassword>' -Q "SELECT @@VERSION".
Assess the Source Database:
sp_spaceused in SSMS).This is the most common method for full database migration, suitable for most scenarios.
D:\backups\MyDB.bak), and click OK.sqlcmd to create a backup:sqlcmd -S localhost -U SA -P '<YourPassword>' -Q "BACKUP DATABASE [MyDB] TO DISK = N'/var/opt/mssql/backup/MyDB.bak' WITH INIT, STATS = 10"
Replace [MyDB] with your database name and adjust the path.Use scp (secure copy) to transfer the .bak file from the source to the Ubuntu server:
scp username@source_server:/path/to/MyDB.bak /home/ubuntu/backup/
Replace username, source_server, and paths with actual values.
Create a new database (optional but recommended):
sqlcmd -S localhost -U SA -P '<YourPassword>' -Q "CREATE DATABASE [MyDB_New]"
Restore the backup using RESTORE DATABASE with MOVE to relocate data/log files (adjust paths as needed):
sqlcmd -S localhost -U SA -P '<YourPassword>' -Q "RESTORE DATABASE [MyDB_New] FROM DISK = N'/home/ubuntu/backup/MyDB.bak' WITH MOVE 'MyDB_Data' TO N'/var/opt/mssql/data/MyDB_New.mdf', MOVE 'MyDB_Log' TO N'/var/opt/mssql/data/MyDB_New_log.ldf', STATS = 10, REPLACE, RECOVERY"
REPLACE: Overwrites an existing database with the same name.RECOVERY: Brings the database online after restore.Verify the restore:
sqlcmd -S localhost -U SA -P '<YourPassword>' -d MyDB_New -Q "SELECT TOP 10 * FROM YourTableName"
Replace YourTableName with a table from your database.
For large databases or incremental migrations, bcp (Bulk Copy Program) is efficient for exporting/importing data in CSV or native formats.
bcp to export a table to a CSV file:bcp MyDB.dbo.MyTable out /home/ubuntu/export/MyTable.csv -c -t, -S localhost -U SA -P '<YourPassword>'
-c: Uses character data type.-t,: Sets the field terminator to a comma.Use scp to copy CSV files to the target Ubuntu server (if exported from Windows).
Use bcp to import the CSV file into the target database:
bcp MyDB_New.dbo.MyTable in /home/ubuntu/export/MyTable.csv -c -t, -S localhost -U SA -P '<YourPassword>' -e /home/ubuntu/export/error.log
-e: Logs errors to a file (useful for troubleshooting).-F 2 to skip the header row if your CSV has one.Run queries on the target database to compare record counts or sample data:
sqlcmd -S localhost -U SA -P '<YourPassword>' -d MyDB_New -Q "SELECT COUNT(*) FROM MyTable"
Ensure the count matches the source table.
For complex migrations (schema transformations, data cleansing, automation), use SSIS (SQL Server Data Tools).
cron (Ubuntu) to automate the package execution.SSIS is ideal for large-scale migrations with complex requirements but requires familiarity with the tool.
After migration, perform these checks to ensure success:
CHECKSUM TABLE in MySQL—SQL Server uses HASHBYTES) between source and target databases.INFORMATION_SCHEMA or third-party tools (e.g., Redgate SQL Compare) to verify tables, columns, indexes, and constraints.Server=ubuntu_ip;Database=MyDB_New;User Id=sa;Password=<YourPassword>;).sys.dm_exec_query_stats or tools like SQL Server Profiler. Optimize indexes if needed.ALTER LOGIN SA ENABLE if the account is disabled.RESTORE DATABASE (data/log files) exist and are writable by the mssql user.ping the Ubuntu server), and SQL Server configuration (sudo /opt/mssql/bin/mssql-conf set network.tcpport 1433).By following these methods and best practices, you can successfully migrate SQL Server databases to Ubuntu while minimizing downtime and ensuring data integrity.