CentOS 上 SQL Server 数据库恢复技巧
一 环境准备与常用命令
sudo systemctl status mssql-serversudo systemctl start|stop mssql-server/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<密码>'sudo mkdir -p /var/opt/mssql/backupssudo chown mssql:mssql /var/opt/mssql/backupssudo chmod 700 /var/opt/mssql/backups二 标准恢复流程与示例
RESTORE DATABASE [YourDB]
FROM DISK = N'/var/opt/mssql/backups/YourDB.bak'
WITH REPLACE, RECOVERY;
RESTORE DATABASE [YourDB]
FROM DISK = N'/var/opt/mssql/backups/YourDB.bak'
WITH NORECOVERY;
RESTORE DATABASE [YourDB]
FROM DISK = N'/var/opt/mssql/backups/YourDB_Diff.bak'
WITH NORECOVERY;
RESTORE LOG [YourDB]
FROM DISK = N'/var/opt/mssql/backups/YourDB_Log.trn'
WITH RECOVERY;
RESTORE LOG [YourDB]
FROM DISK = N'/var/opt/mssql/backups/YourDB_Log.trn'
WITH STOPAT = '2025-11-30 14:00:00', RECOVERY;
三 常见故障与处理
chown/chmod 修正后重试。ALTER DATABASE [YourDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
-- 执行还原
ALTER DATABASE [YourDB] SET MULTI_USER;
SELECT name, recovery_model_desc FROM sys.databases WHERE name='YourDB';ALTER DATABASE [YourDB] SET RECOVERY FULL;四 恢复后验证与优化
DBCC CHECKDB ([YourDB]) WITH NO_INFOMSGS, ALL_ERRORMSGS;UPDATE STATISTICS ALL;ALTER INDEX ALL ON [schema].[table] REBUILD;SELECT name FROM sys.databases WHERE name='YourDB';
SELECT TOP 10 * FROM [YourDB].[dbo].[YourTable];
五 迁移与灾难恢复建议
ALTER DATABASE [YourDB] SET PARTNER FAILOVER;