数据库镜像怎么还原数据库_镜像数据库上的日志传送

本文详细介绍了如何在SQL Server中配置数据库镜像和日志传送,确保高可用性和灾难恢复。内容涉及镜像和日志传送的概念、配置步骤,以及在主体数据库故障时如何保持日志传送的连续性。
摘要由CSDN通过智能技术生成

数据库镜像怎么还原数据库

Both log shipping and mirroring are high availability and disaster recovery options available in SQL Server. This article will review on how to configure log shipping on a mirrored database.

日志传送和镜像都是SQL Server中可用的高可用性和灾难恢复选项。 本文将回顾如何在镜像数据库上配置日志传送。

什么是镜像 (What is mirroring)

  • Database mirroring is high availability and disaster recovery option available in SQL server

    数据库镜像是SQL Server中可用的高可用性和灾难恢复选项
  • The transactions are sent from principal database to mirror database

    事务从主体数据库发送到镜像数据库
  • Mirror server comes online when principal goes down

    当主体发生故障时,镜像服务器会联机
  • Mirroring is configured at database level and has only one mirror database

    镜像是在数据库级别配置的,并且只有一个镜像数据库
  • Supports automatic failover if witness server is used

    如果使用见证服务器,则支持自动故障转移

什么是日志传送 (What is log shipping)

  • Log shipping takes the log backup on primary server, ships the log backups to secondary and restore them on secondary server

    日志传送将日志备份放在主服务器上,将日志备份传送到辅助服务器上,然后将其还原到辅助服务器上
  • A backup job is created on primary server and takes backup as per schedule

    备份作业在主服务器上创建,并按计划进行备份
  • Copy and restore jobs are configured on secondary server and runs as per schedule

    复制和还原作业在辅助服务器上配置,并按计划运行
  • Supports manual failover only

    仅支持手动故障转移

Here is a series of step by step instructions to configure log shipping along with mirroring on a database so that in case of failover from principal to mirror the log shipping continue to work.

这是一系列循序渐进的说明,用于配置日志传送以及在数据库上的镜像,以便在从主体到镜像进行故障转移的情况下,日志传送继续工作。

配置镜像 (Configuring Mirroring)

先决条件 (Pre-requisites )

  • Database should be in full recovery mode. Simple and bulk logged recovery models are not supported in mirroring

    数据库应处于完全恢复模式。 镜像不支持简单和批量记录的恢复模型
  • Principal server, mirror server and witness server (If used) should be on same SQL server version

    主服务器,镜像服务器和见证服务器(如果使用)应该在相同SQL Server版本上
  • Partners should be running same edition of SQL Server. Witness can be any edition that supports database mirroring

    合作伙伴应运行相同版本SQL Server。 见证可以是任何支持数据库镜像的版本

Login to your primary SQL Server and take one full backup and one transactional log backup of the database you are going to mirror.

登录到您的主SQL Server,并对要镜像的数据库进行一次完整备份和一个事务日志备份。

In this case I am configuring mirroring on database called “Sample”.

在这种情况下,我将在名为“ Sample”的数据库上配置镜像。

Below is the T-SQL script to take full back of the database. Replace the location of the backup as per your need.

下面是完整记录数据库的T-SQL脚本。 根据需要更换备份位置。

BACKUP DATABASE [Sample] TO  DISK = N'D:\SQL_Backup\Sample.bak' WITH NOFORMAT, NOINIT,  NAME = N'Sample-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

Below is the T-SQL script to take transaction log backup.

下面是执行事务日志备份的T-SQL脚本。

BACKUP LOG [Sample] TO  DISK = N'D:\SQL_Backup\Sample_T1.trn' WITH NOFORMAT, NOINIT,  NAME = N'Sample-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

Copy the backup files from primary server to secondary server in your desired location.

将备份文件从主服务器复制到所需位置的辅助服务器。

Now login to your secondary server and restore the database from the backups with no recovery option.

现在登录到辅助服务器,并从没有恢复选项的备份中还原数据库。

Below is the T-SQL Script to restore full backup. Replace the location of your backup file in the script.

以下是还原完整备份的T-SQL脚本。 在脚本中替换备份文件的位置。

Use master
RESTORE DATABASE [Sample] FROM  DISK = N'X:\Sample.bak' WITH  FILE = 1,  MOVE N'Sample' TO N'I:\MSSQL\Data\Sample.mdf',  MOVE N'Sample_log' TO N'I:\MSSQL\Data\Sample_log.ldf',  NORECOVERY,  NOUNLOAD,  STATS = 5

Below is the T-SQL Script to restore transaction log backup.

以下是用于还原事务日志备份的T-SQL脚本。

Use master
RESTORE LOG [Sample] FROM  DISK = N'X:\Sample_T1.trn' WITH  FILE = 1,  NORECOVERY,  NOUNLOAD,  STATS = 5
 
GO

Now on the primary server, navigate to the database and right click on the database, Select Properties and click on Mirroring

现在在主服务器上,导航到数据库,然后右键单击数据库,选择“ 属性” ,然后单击“ 镜像”

Mirroring setup - Configure security

Click on Configure Security, click Next and Select yes to include a witness server.

单击“ 配置安全性” ,单击“ 下一步” ,然后选择“是”以包括见证服务器。

Witness server is optional in mirroring and supports automatic failover when configured using high safety with automatic failover operating mode.

见证服务器在镜像中是可选的,并且在使用高安全性和自动故障转移操作模式进行配置时,支持自动故障转移。

Enable witness server and click Next.

启用见证服务器,然后单击“ 下一步”

Mirroring Setup - Add witness server

Input the listener port numbers for principal server, mirror server and witness server which will be used to communicate over the network.

输入将用于通过网络进行通信的主体服务器,镜像服务器和见证服务器的侦听器端口号。

Mirroring Setup - Listener ports configuration

If all the SQL Server instances are running on same service account leave the service accounts section blank else input the accounts as per your need.

如果所有SQL Server实例都在同一服务帐户上运行,则将“服务帐户”部分留空,否则根据需要输入帐户。

Click Next and Finish. Then click on Start Mirroring.

单击下一步,然后单击完成 。 然后单击“ 开始镜像”

Mirroring Setup - Start mirroring session

In the above step you may encounter issues and mirroring does not start if there is no proper access on end points or if the ports are blocked by firewall.

在上述步骤中,如果端点上没有适当的访问权限,或者端口被防火墙阻止,则可能会遇到镜像无法启动的问题。

Once the mirror is started the database on primary server looks like below and act as Principal database. the mirror database will be in restoring mode.

启动镜像后,主服务器上的数据库如下所示,并充当Principal数据库。 镜像数据库将处于还原模式。

Mirroring synchronized principal database

在主体数据库上配置日志传送 (Configuring Log Shipping on a Principal Database)

先决条件: (Pre-requisites: )

  • The database should be in full or bulk logged recovery model. But in this case, it is combination of mirroring and log shipping and database should be in full recovery model

    数据库应为完整或批量记录的恢复模型。 但是在这种情况下,它是镜像和日志传送的结合,数据库应该处于完全恢复模式

Login to the primary server and navigate to the principal database. Right click on the database and select Properties. Click on Transaction Log Shipping and check Enable this is as a primary database in log shipping configuration.

登录到主服务器并导航到主体数据库。 右键单击数据库,然后选择“ 属性” 。 单击“ 事务日志传送”,然后选中“ 启用它作为日志传送配置中的主数据库”

Enable log shipping for a database

Click on backup settings to configure and schedule the transactional log backups. Input the shared path where transactional log backups will be taken. The shared path you set should be accessable from the secondary server where mirror database exist and aslo it should be available accessable from seconadry server of your logshipping database.

单击备份设置以配置和计划事务日志备份。 输入将进行事务日志备份的共享路径。 您设置的共享路径应该可以从存在镜像数据库的辅助服务器上访问,也应该可以从日志传送数据库的seconadry服务器上访问。

Click on schedule to set the frequncey of transaction log backups. In this case I scheduled it the backup job to run for every 15 minutes. We can set the retention period for backup files, so that the backup files greater than retention period will be deleted. Backup compression is also available which compress the transaction log backups. Set the compresssion option as per your need.

单击计划以设置事务日志备份的频率。 在这种情况下,我计划将备份作业每15分钟运行一次。 我们可以设置备份文件的保留期限,以便删除大于保留期限的备份文件。 还可以使用备份压缩来压缩事务日志备份。 根据需要设置压缩选项。

Backup setting in log shipping

Once the backup settings are configured. Click on Add to add secondary sever instance. Connect to the secondary instance using windows or SQL server authentication and input the secondary database name of your choice. There are different ways to initialize the secondary database in log shipping. As I do not have secondary database already initialized on the secondary server. I selected the first option which will take the full backup of the database and restore it on the secondary server.

一旦配置了备份设置。 单击添加以添加辅助服务器实例。 使用Windows或SQL Server身份验证连接到辅助实例,然后输入您选择的辅助数据库名称。 在日志传送中,可以使用不同的方法来初始化辅助数据库。 由于我还没有在辅助服务器上初始化辅助数据库。 我选择了第一个选项,它将对数据库进行完整备份并将其还原到辅助服务器上。

Initialize secondary database on secondary server

Now navigate to Copy Files tab. create a folder in local drive and Input the folder path to copy the backup files from shared path provided in the backup settings.

现在,导航到“ 复制文件”选项卡。 在本地驱动器中创建一个文件夹,然后输入文件夹路径以从备份设置中提供的共享路径复制备份文件。

Click on schedule to schedule the copy job. You can also configure when to delete to the copied files as per your needs.

单击计划以计划复制作业。 您还可以根据需要配置何时删除复制的文件。

Log shipping setup - Copy files configuration

Navigate to Restore Transaction Log tab and schedule the restore job as per your need. In this case I scheduled restore job to run for every 15 minutes.

导航到“还原事务日志”选项卡,然后根据需要安排还原作业。 在这种情况下,我计划将还原作业每15分钟运行一次。

We have two restore options available one is no recovery mode and the other is standby mode.

我们有两个可用的还原选项,一个是无恢复模式,另一个是待机模式。

In no recovery mode the secondary database is not available for users and will be in restoring state. In standby mode the secondary database will be in read only mode and users can read the data. When stand by mode is used make sure you enable disconnect users in database when restoring backups.

在无恢复模式下,辅助数据库不可用于用户,并且将处于还原状态。 在待机模式下,辅助数据库将处于只读模式,并且用户可以读取数据。 使用待机模式时,请确保在还原备份时启用数据库中的断开连接用户

Once the log shipping setup is completed. You will find Backup job on primary server which takes the transaction log backup as per schedule you configured.

日志传送设置完成后。 您将在主服务器上找到备份作业,该备份作业将按照您配置的时间表进行事务日志备份。

The copy job will be created on secondary server and copy the transaction log backup files from shared path to local path.

复制作业将在辅助服务器上创建,并将事务日志备份文件从共享路径复制到本地路径。

The restore job is created on secondary server which restores the copied transaction log backups in sequence.

还原作业在辅助服务器上创建,该服务器按顺序还原复制的事务日志备份。

在镜像服务器上配置备份作业 (Configuring a backup job on a mirror server)

When mirroring fails over, the mirror database becomes online, act as a principal database and the principal database goes into restoring state. The backup job on the initial primary server does not take backups as the database is in the restoring mode.

当镜像故障转移时,镜像数据库变为联机状态,充当主体数据库,并且主体数据库进入还原状态。 初始主服务器上的备份作业不进行备份,因为数据库处于还原模式。

To continue log shipping, we need to configure the backup job on the current principal server.

要继续日志传送,我们需要在当前主体服务器上配置备份作业。

To simulate the failover, Login to the primary server and navigate to the principal database right click on it select Properties. Click on Mirroring and click on failover to manually failover the mirroring. Now the mirror database becomes principal database.

为了模拟故障转移,登录到主服务器并导航到主体数据库,右键单击它,选择Properties 。 单击镜像 ,然后单击故障转移以手动对镜像进行故障转移。 现在,镜像数据库成为主体数据库。

Login to the current primary database server and navigate to principal database. Right click on the database and select Properties. Click on Transaction Log Shipping and check Enable this is as a primary database in log shipping configuration.

登录到当前的主数据库服务器,然后导航到主体数据库。 右键单击数据库,然后选择“ 属性” 。 单击“ 事务日志传送”,然后选中“ 启用它作为日志传送配置中的主数据库”

Click on backup settings to configure and schedule backups. Input the same shared path provided while configuring the log shipping initially.

单击备份设置以配置和安排备份。 输入最初配置日志传送时提供的相同共享路径。

Click on the schedule button to set the frequncey of transaction log backups. Once the backup job is created on current primary server it starts taking the transaction log backups of the database to to shared path provided in the backup settings.

单击计划按钮以设置事务日志备份的频率。 在当前主服务器上创建备份作业后,它将开始将数据库的事务日志备份转移到备份设置中提供的共享路径。

The copy and restore jobs on log shipping secondary server run as usual with out any change in copy and restore settings.

日志传送辅助服务器上的复制和还原作业照常运行,而复制和还原设置没有任何更改。

Monitor the backup, copy and restore jobs to make sure every thing is as expected.

监视备份,复制和还原作业,以确保一切都如预期。

步骤总结 (Steps summary)

  1. Configure mirroring on the database (synchronous with full saftey)

    在数据库上配置镜像(与完整安全同步)
  2. Configure log shipping on the principal database

    在主体数据库上配置日志传送
  3. Manually failover the mirroring

    手动故障转移镜像
  4. On current principal database enable transaction log shipping and configure backup using same shared path

    在当前的主体数据库上,启用事务日志传送并使用相同的共享路径配置备份
  5. Monitor the log shipping jobs

    监视日志传送作业

目录 (Table of contents)

SQL Server Replication with a table with more than 246 columns
Foreign key issues while applying a snapshot in SQL Server merge replication
SQL Server Replication (Merge) – What gets replicated and what doesn’t
SQL Server Replication (Merge) – Performance Issues in replicating schema changes
Merge SQL Server replication parameterized row filter issues
SQL Server Replication on a mirrored database
Log shipping on a mirrored database
具有超过246列的表SQL Server复制
在SQL Server合并复制中应用快照时出现外键问题
SQL Server复制(合并)–复制什么,什么不复制
SQL Server复制(合并)–复制架构更改中的性能问题
合并SQL Server复制参数化的行筛选器问题
镜像数据库上SQL Server复制
镜像数据库上的日志传送

翻译自: https://www.sqlshack.com/log-shipping-on-a-mirrored-database/

数据库镜像怎么还原数据库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值