開始使用 Linux 上的記錄傳送
適用於:SQL Server - Linux
記錄傳送是一種 SQL Server 高可用性 (HA) 設定,其中主伺服器的資料庫會複寫到一或多部次要伺服器上。 記錄傳送可讓來源資料庫的備份檔案還原到次要伺服器。 主要伺服器會定期建立交易記錄備份,而次要伺服器會將它們還原,同時更新資料庫的次要複本。
如前面圖表所示,記錄傳送工作階段包含下列步驟:
- 在主要 SQL Server 執行個體上備份交易記錄檔
- 透過網路,將交易記錄備份檔案複製到一或多個次要 SQL Server 執行個體
- 在次要 SQL Server 執行個體上還原交易記錄備份檔案
Prerequisites
使用 CIFS 設定記錄傳送的網路共用
注意
本教學課程使用 CIFS + Samba 來設定網路共用。
設定主要伺服器
使用下列命令來安裝 Samba:
針對 Red Hat Enterprise Linux (RHEL):
sudo yum -y install samba
針對 Ubuntu:
sudo apt-get install samba
建立目錄以儲存記錄傳送的記錄檔,並為
mssql
使用者提供必要的權限:mkdir /var/opt/mssql/tlogs chown mssql:mssql /var/opt/mssql/tlogs chmod 0700 /var/opt/mssql/tlogs
編輯
/etc/samba/smb.conf
檔案 (您需要根權限) 並新增下列區段:[tlogs] path=/var/opt/mssql/tlogs available=yes read only=yes browsable=yes public=yes writable=no
建立適用於 Samba 的
mssql
使用者:sudo smbpasswd -a mssql
重新啟動 Samba 服務:
sudo systemctl restart smbd.service nmbd.service
設定次要伺服器
使用下列命令來安裝 CIFS 用戶端:
針對 RHEL:
sudo yum -y install cifs-utils
針對 Ubuntu:
sudo apt-get install cifs-utils
建立檔案以儲存您的認證。 在此範例中,我們使用
/var/opt/mssql/.tlogcreds
。 使用您最近針對mssql
Samba 帳戶所設定的密碼,並取代<domain>
:username=mssql domain=<domain> password=<password>
執行下列命令以建立用於掛接的空目錄,並正確設定權限和所有權
mkdir /var/opt/mssql/tlogs sudo chown root:root /var/opt/mssql/tlogs sudo chmod 0550 /var/opt/mssql/tlogs sudo chown root:root /var/opt/mssql/.tlogcreds sudo chmod 0660 /var/opt/mssql/.tlogcreds
將行新增至
etc/fstab
以保存共用。 請使用適當的值取代<ip_address_of_primary_server>
://<ip_address_of_primary_server>/tlogs /var/opt/mssql/tlogs cifs credentials=/var/opt/mssql/.tlogcreds,ro,uid=mssql,gid=mssql 0 0
掛接共用:
sudo mount -a
使用 Transact-SQL 設定記錄傳送
備份主要伺服器上的資料庫:
BACKUP DATABASE SampleDB TO DISK = '/var/opt/mssql/tlogs/SampleDB.bak'; GO
在主要伺服器上設定記錄傳送:
DECLARE @LS_BackupJobId AS UNIQUEIDENTIFIER; DECLARE @LS_PrimaryId AS UNIQUEIDENTIFIER; DECLARE @SP_Add_RetCode AS INT; EXECUTE @SP_Add_RetCode = master.dbo.sp_add_log_shipping_primary_database @database = N'SampleDB', @backup_directory = N'/var/opt/mssql/tlogs', @backup_share = N'/var/opt/mssql/tlogs', @backup_job_name = N'LSBackup_SampleDB', @backup_retention_period = 4320, @backup_compression = 2, @backup_threshold = 60, @threshold_alert_enabled = 1, @history_retention_period = 5760, @backup_job_id = @LS_BackupJobId OUTPUT, @primary_id = @LS_PrimaryId OUTPUT, @overwrite = 1; IF (@@ERROR = 0 AND @SP_Add_RetCode = 0) BEGIN DECLARE @LS_BackUpScheduleUID AS UNIQUEIDENTIFIER; DECLARE @LS_BackUpScheduleID AS INT; EXECUTE msdb.dbo.sp_add_schedule @schedule_name = N'LSBackupSchedule', @enabled = 1, @freq_type = 4, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = 15, @freq_recurrence_factor = 0, @active_start_date = 20170418, @active_end_date = 99991231, @active_start_time = 0, @active_end_time = 235900, @schedule_uid = @LS_BackUpScheduleUID OUTPUT, @schedule_id = @LS_BackUpScheduleID OUTPUT; EXECUTE msdb.dbo.sp_attach_schedule @job_id = @LS_BackupJobId, @schedule_id = @LS_BackUpScheduleID; EXECUTE msdb.dbo.sp_update_job @job_id = @LS_BackupJobId, @enabled = 1; END EXECUTE master.dbo.sp_add_log_shipping_alert_job; EXECUTE master.dbo.sp_add_log_shipping_primary_secondary @primary_database = N'SampleDB', @secondary_server = N'<ip_address_of_secondary_server>', @secondary_database = N'SampleDB', @overwrite = 1;
在次要伺服器上還原資料庫:
RESTORE DATABASE SampleDB FROM DISK = '/var/opt/mssql/tlogs/SampleDB.bak' WITH NORECOVERY;
在次要伺服器上設定記錄傳送:
DECLARE @LS_Secondary__CopyJobId AS UNIQUEIDENTIFIER; DECLARE @LS_Secondary__RestoreJobId AS UNIQUEIDENTIFIER; DECLARE @LS_Secondary__SecondaryId AS UNIQUEIDENTIFIER; DECLARE @LS_Add_RetCode AS INT; EXECUTE @LS_Add_RetCode = master.dbo.sp_add_log_shipping_secondary_primary @primary_server = N'<ip_address_of_primary_server>', @primary_database = N'SampleDB', @backup_source_directory = N'/var/opt/mssql/tlogs/', @backup_destination_directory = N'/var/opt/mssql/tlogs/', @copy_job_name = N'LSCopy_SampleDB', @restore_job_name = N'LSRestore_SampleDB', @file_retention_period = 4320, @overwrite = 1, @copy_job_id = @LS_Secondary__CopyJobId OUTPUT, @restore_job_id = @LS_Secondary__RestoreJobId OUTPUT, @secondary_id = @LS_Secondary__SecondaryId OUTPUT; IF (@@ERROR = 0 AND @LS_Add_RetCode = 0) BEGIN DECLARE @LS_SecondaryCopyJobScheduleUID AS UNIQUEIDENTIFIER; DECLARE @LS_SecondaryCopyJobScheduleID AS INT; EXECUTE msdb.dbo.sp_add_schedule @schedule_name = N'DefaultCopyJobSchedule', @enabled = 1, @freq_type = 4, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = 15, @freq_recurrence_factor = 0, @active_start_date = 20170418, @active_end_date = 99991231, @active_start_time = 0, @active_end_time = 235900, @schedule_uid = @LS_SecondaryCopyJobScheduleUID OUTPUT, @schedule_id = @LS_SecondaryCopyJobScheduleID OUTPUT; EXECUTE msdb.dbo.sp_attach_schedule @job_id = @LS_Secondary__CopyJobId, @schedule_id = @LS_SecondaryCopyJobScheduleID; DECLARE @LS_SecondaryRestoreJobScheduleUID AS UNIQUEIDENTIFIER; DECLARE @LS_SecondaryRestoreJobScheduleID AS INT; EXECUTE msdb.dbo.sp_add_schedule @schedule_name = N'DefaultRestoreJobSchedule', @enabled = 1, @freq_type = 4, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = 15, @freq_recurrence_factor = 0, @active_start_date = 20170418, @active_end_date = 99991231, @active_start_time = 0, @active_end_time = 235900, @schedule_uid = @LS_SecondaryRestoreJobScheduleUID OUTPUT, @schedule_id = @LS_SecondaryRestoreJobScheduleID OUTPUT; EXECUTE msdb.dbo.sp_attach_schedule @job_id = @LS_Secondary__RestoreJobId, @schedule_id = @LS_SecondaryRestoreJobScheduleID; END DECLARE @LS_Add_RetCode2 AS INT; IF (@@ERROR = 0 AND @LS_Add_RetCode = 0) BEGIN EXECUTE @LS_Add_RetCode2 = master.dbo.sp_add_log_shipping_secondary_database @secondary_database = N'SampleDB', @primary_server = N'<ip_address_of_primary_server>', @primary_database = N'SampleDB', @restore_delay = 0, @restore_mode = 0, @disconnect_users = 0, @restore_threshold = 45, @threshold_alert_enabled = 1, @history_retention_period = 5760, @overwrite = 1; END IF (@@ERROR = 0 AND @LS_Add_RetCode = 0) BEGIN EXECUTE msdb.dbo.sp_update_job @job_id = @LS_Secondary__CopyJobId, @enabled = 1; EXECUTE msdb.dbo.sp_update_job @job_id = @LS_Secondary__RestoreJobId, @enabled = 1; END
驗證記錄傳送是否正常運作
在主要伺服器上啟動下列工作,以驗證記錄傳送是否正常運作:
USE msdb; GO EXECUTE dbo.sp_start_job N'LSBackup_SampleDB'; GO
在次要伺服器上啟動下列工作,以驗證記錄傳送是否正常運作:
USE msdb; GO EXECUTE dbo.sp_start_job N'LSCopy_SampleDB'; GO EXECUTE dbo.sp_start_job N'LSRestore_SampleDB'; GO
執行下列命令,以驗證「記錄傳送」容錯移轉是否正常運作:
警告
此命令會讓次要資料庫上線並中斷「記錄傳送」設定。 執行此命令之後,您必須重新設定「記錄傳送」。
RESTORE DATABASE SampleDB WITH RECOVERY;