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

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 = 5GO

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

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

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.

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

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

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

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.

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

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数据库。 镜像数据库将处于还原模式。

在主体数据库上配置日志传送 (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.

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

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分钟运行一次。 我们可以设置备份文件的保留期限,以便删除大于保留期限的备份文件。 还可以使用备份压缩来压缩事务日志备份。 根据需要设置压缩选项。

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身份验证连接到辅助实例,然后输入您选择的辅助数据库名称。 在日志传送中,可以使用不同的方法来初始化辅助数据库。 由于我还没有在辅助服务器上初始化辅助数据库。 我选择了第一个选项,它将对数据库进行完整备份并将其还原到辅助服务器上。

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.

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

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/

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

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

  1. mysql数据库备份和还原的命令_Mysql数据库备份和还原常用的命令

    备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql 备份MySQL数据库 ...

  2. mysql数据库取奇数行数据_查询数据库中的奇数行和偶数行

    功能奇数次执行和偶数次执行时的结果不同的故障复盘 场景:将数据库查询到的数据,写入一个xls文件.完成后,多点几次,发现一个问题,偶数次生成的文件比较小,打开一看,里面只有一行,只有标题,没有内容.分 ...

  3. MySQL创建数据库得语句是什么_创建数据库的语句是什么

    创建数据库的语句是:[CREATE DATABASE [IF NOT EXISTS] [[DEFAULT] CHARACTER SET ] [[DEFAULT] COLLATE ];]. 本文操作环境 ...

  4. mysql数据库中学分的简写_一些数据库相关词汇缩写

    ORM "对象-关系映射"(Object/Relational Mapping) 的缩写 面向对象编程和关系型数据库,都是目前最流行的技术,但是它们的模型是不一样的. 面向对象编程 ...

  5. 数据库新增幂等操作_使用数据库唯一键实现事务幂等性

    幂等性 概念 在分布式系统中,幂等性是一致性方面的一个重要概念. 幂等(idempotent.idempotence)是一个数学与计算机学概念,常见于抽象代数中. 在编程中一个幂等操作的特点是其任意多 ...

  6. mysql数据库修改列排序规则_更改数据库排序规则

    在SQLServer2008R2版本及以上,我们选择的都是默认安装,所以在创建数据库的时候,服务器默认选择的排序规则是一个SQL_Latin1_General_CP1_CI_AS排序规则,这样在向数据 ...

  7. SQL数据库备份及还原

    在对数据库的实际操作中,数据库的备份及还原十分重要.他不仅能很好的做到服务器容灾,又能有效的解决因为误操作带来的数据问题,还能为开发团队测试团队提供最新的用户动态,及时响应和调整用户最新软件情况.是I ...

  8. 利用日志传送来实现数据库的可用性

    这不是一个新技术,SQL SERVER2000时就已经有了,2005中没有什么太大的改进,,还是由三项操作组成: 1.  在主服务器中备份事务日志 2.  然后将备份的事务日志文件复制到辅助服务器中 ...

  9. 使用SQL Server日志传送将SQL数据库移动到其他服务器

    As a SQL Server DBA, we are responsible for moving the customer SQL databases to other servers. Rece ...

最新文章

  1. 拷贝构造函数的调用以及浅拷贝与深拷贝的理解
  2. 区块链与边缘计算(2)功能介绍
  3. 四边形可以分为几类_四边形有几种类型
  4. 图论 —— 稳定婚姻问题与延迟认可算法
  5. 一像素约束(选中所需约束,切换到类处,选择此类,运行时就是0.5的约束)
  6. Linux gedit
  7. Spring核心技术原理-(2)-通过Web开发演进过程了解一下为什么要有Spring AOP?
  8. java2的7次方怎么表示_一元二次方程常见题型之方程根的解法
  9. 目标检测——YOLOV5的学习笔记(legcay)
  10. 06_day初始化PIC
  11. 从零开始学习CANoe(四)—— 设计panel
  12. 计算机考研江苏,2020江苏高考分数线公布
  13. Java邮件发送(实名发送和匿名发送)
  14. [导入]中国车牌号的识别
  15. 王者荣耀服务器未响应 小米,10月23日小米手机更新王者荣耀失败 小米更新不了新赛季怎么办...
  16. CodeForces - 1526C2
  17. mt9638和t972哪个好
  18. 自制适合城市家庭的鱼菜共生系统
  19. ​计算机视觉传感器系统
  20. 联邦学习 Federated Learning 相关资料整理

热门文章

  1. linux xargs命令
  2. 自定义标签处理器类的生命周期
  3. shell脚本修改文本中匹配行之前的行的方法
  4. c# Linq实现 获得某一个路径下所有文件的名(不含扩展名)
  5. 句句真研—每日长难句打卡Day6
  6. 自己的父母,能把钱交给他们存吗?
  7. 有房贷的房子怎么卖?
  8. 小户型吊顶的注意事项有哪些
  9. 现在企业为何越来越难呢?
  10. 谈判中,要学会“留一手”