SQL Server在CentOS中的数据迁移技巧
mssql-tools、unixODBC-devel),并配置环境变量(如将/opt/mssql-tools/bin添加至PATH),以便使用sqlcmd等命令行工具。ping或telnet测试1433端口连通性)。.bak文件)。例如,T-SQL命令:BACKUP DATABASE [YourDatabaseName]
TO DISK = N'/path/to/source/backup/YourDatabaseName.bak'
WITH NOFORMAT, NOINIT, NAME = N'Full Backup', STATS = 10;
scp(安全复制)将.bak文件从源服务器传输至目标CentOS服务器。例如:scp /path/to/source/backup/YourDatabaseName.bak user@target_centos:/path/to/destination/
.mdf)和日志文件(.ldf)的新路径(如/var/opt/mssql/data/目录)。例如:RESTORE DATABASE [YourDatabaseName]
FROM DISK = N'/path/to/destination/YourDatabaseName.bak'
WITH FILE = 1,
MOVE N'YourDatabaseName_Data' TO N'/var/opt/mssql/data/YourDatabaseName.mdf',
MOVE N'YourDatabaseName_Log' TO N'/var/opt/mssql/data/YourDatabaseName.ldf',
NOUNLOAD, STATS = 10;
sqlcmd连接源服务器,将表数据导出为CSV文件(支持字段分隔符定制)。例如:sqlcmd -S source_server -U username -P password -d YourDatabase -Q "SELECT * FROM YourTable" -s "," -W -o /path/to/export.csv
sqlcmd连接目标服务器,通过BULK INSERT命令将CSV数据导入目标表。例如:BULK INSERT YourDatabase.dbo.YourTable
FROM '/path/to/export.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');
.bacpac文件(包含架构、数据及扩展属性)。例如:sqlpackage export -ServerName source_server -DatabaseName YourDatabase -Username username -Password password -OutputFile /path/to/database.bacpac
.bacpac文件导入目标SQL Server实例,快速重建数据库。例如:sqlpackage import -ServerName target_centos -DatabaseName TargetDatabase -Username username -Password password -File /path/to/database.bacpac
COUNT(*)查询,对比源数据库与目标数据库的记录数是否一致。例如:SELECT COUNT(*) FROM YourTable;
sqlcmd执行跨服务器查询(需配置链接服务器)。db_owner)执行还原或导入操作。/var/opt/mssql/data/),避免因路径不存在导致还原失败。scp、rsync over SSH),避免数据泄露;迁移完成后删除临时备份文件。