As a SQL Server DBA, we are responsible for moving the customer SQL databases to other servers. Recently, I was assigned the same project. One of the customers wanted to move their SQL database to a separate server. Following were the challenges:

作为SQL Server DBA,我们负责将客户SQL数据库移至其他服务器。 最近,我被分配了相同的项目。 其中一位客户希望将其SQL数据库移至单独的服务器。 以下是挑战:

  1. The database is very large; therefore, we cannot detach the database and copy it to another server and reattach it. It was a bad idea because it takes approx. 5 hours and it might block the network as well 该数据库非常大; 因此,我们无法分离数据库并将其复制到另一台服务器并重新附加它。 这是一个坏主意,因为它大约需要花费时间。 5个小时,它也可能会阻塞网络
  2. The size of the backup was large too. We tried to restore it on the test server, and the entire database restore process took approximately 3 hours 备份的大小也很大。 我们尝试在测试服务器上还原它,整个数据库还原过程大约花费了3个小时

So, we decided to use the following approach:

因此,我们决定使用以下方法:

  1. Script out all the SQL Jobs and recreate them on the new server 编写所有SQL作业的脚本,然后在新服务器上重新创建它们
  2. Recreate all the logins on the new server 在新服务器上重新创建所有登录名
  3. Set up log shipping between the old server and the new server 设置旧服务器和新服务器之间的日志传送
  4. During maintenance hours, perform a switch over from the old server to the new server and disable the log shipping 在维护时间内,请执行从旧服务器到新服务器的切换并禁用日志传送

In this article, I am going to explain how we can move the SQL Database to a different server using SQL Log shipping. To demonstrate the entire process, I have created a demo setup, and the details are as following:

在本文中,我将解释如何使用SQL日志传送将SQL数据库移动到其他服务器。 为了演示整个过程,我创建了一个演示设置,详细信息如下:

Source database server

Destination database server

Host Name and IP Address

SQL01.DC.Local (192.168.0.131)

SQL02.DC.Local (192.168.0.132)

SQL Server version

SQL Server 2016

SQL Server 2016

源数据库服务器

目标数据库服务器

主机名和IP地址

SQL01.DC.Local(192.168.0.131)

SQL02.DC.Local(192.168.0.132)

SQL Server版本

SQL Server 2016

SQL Server 2016

We are going to move the AdventureWorks2017 database, so I have restored it on SQL01 (Old server). I have created two SQL logins named nisargupadhyay and niraliupadhyay on SQL01.

我们将移动AdventureWorks2017数据库,因此我已在SQL01(旧服务器)上将其还原。 我在SQL01上创建了两个名为nisargupadhyayniraliupadhyay的 SQL登录名

I have created SQL Jobs for database maintenance using the Ola Hallengren’s database maintenance solution. The stored procedures and tables used by the maintenance solutions are created on a separate database named DBA, so it must be restored on a new server before we script out the maintenance jobs.

我已经使用Ola Hallengren的数据库维护解决方案创建了SQL Jobs来进行数据库维护。 维护解决方案使用的存储过程和表是在名为DBA的单独数据库上创建的,因此在编写维护任务脚本之前,必须将其还原到新服务器上。

First, we will script out the SQL Jobs.

首先,我们将编写SQL作业脚本。

编写SQL Job脚本并在新服务器上重新创建它们 (Script out SQL Jobs and recreate them on the new server)

To generate the scripts of the database maintenance Job, connect to the SQL01.DC.Local using the remote desktop connection -> Launch SQL Server Management Studio and connect to the SQL Server database engine. Once connected, expand SQL Server Agent -> Expand Jobs -> Right-click on DatabaseBackup – USER_DATABASES – FULL -> Hover on Script Job as -> hover on Create To -> Hover on New Query Editor Window. See the following image:

要生成数据库维护作业的脚本, 使用远程桌面连接->启动SQL Server Management Studio连接到SQL01.DC.Local并连接到SQL Server数据库引擎 。 连接后,展开SQL Server代理 ->展开作业 ->右键单击DatabaseBackup – USER_DATABASES – FULL- > 将鼠标悬停在脚本作业上 ->将鼠标悬停在创建到 ->将鼠标悬停在新查询编辑器窗口上 。 见下图:

Copy the T-SQL script to recreate the SQL job and execute it on the new server. Similarly, recreate other SQL jobs on a new server. Once the SQL Jobs are created, Let us create the required logins on the new server.

复制T-SQL脚本以重新创建SQL作业并在新服务器上执行它。 同样,在新服务器上重新创建其他SQL作业。 创建SQL作业后,让我们在新服务器上创建所需的登录名。

在新服务器上重新创建所有登录名 (Recreate all the logins on the new server)

First, we must script out all the logins. To do that, we can use sp_help_revlogin stored procedure. I have written an article that explains how we can use it to transfer the SQL Logins on the new server. You can check out this article, Transferring SQL Logins to the secondary replica using sp_help_revlogin.

首先,我们必须编写所有登录的脚本。 为此,我们可以使用sp_help_revlogin存储过程。 我写了一篇文章,解释了我们如何使用它在新服务器上传输SQL登录名。 您可以查看本文使用sp_help_revlogin将SQL登录名传输到辅助副本 。

As mentioned, I have created two logins on SQL01 (Old server). The sp_help_revlogin stored procedure generates the CREATE LOGIN script for them. Script is below:

如前所述,我在SQL01 (旧服务器)上创建了两个登录名。 sp_help_revlogin存储过程将为其生成CREATE LOGIN脚本。 脚本如下:

-- Login: NisargUpadhyayCREATE LOGIN [NisargUpadhyay] WITH PASSWORD = 0x020014224E06368CBCDA16C4722BFFF476B1D244CC0ABE02CBAA906F143C331560B0E6A39BAB0CC8BDFEF75EA4A65E052CB7276BAD3827F0B2FC211CBAD8516548AAC38A7D3F HASHED, SID = 0x2F7DBDA6F4C58B4EB469294EFCAEAE72, DEFAULT_DATABASE = [master], CHECK_POLICY = OFF, CHECK_EXPIRATION = OFF-- Login: NiraliUpadhyayCREATE LOGIN [NiraliUpadhyay] WITH PASSWORD = 0x0200412CDCDB555FF142871BEBF50C6CF5069FF0713C55505CD9CB0EF603593745E2250CEECCC9E6B696DB8791C34184C3DA8AFD01AA2C6D42EC50FFE21926E7DB8EEE09FF87 HASHED, SID = 0xAFD93F8F801E0A43AFD34ABA01F89574, DEFAULT_DATABASE = [master], CHECK_POLICY = OFF, CHECK_EXPIRATION = OFF

Copy the entire script and execute it on the SQL02 (New Server).

复制整个脚本并在SQL02 (新服务器)上执行它。

设置旧服务器和新服务器之间的日志传送 (Set up log shipping between the old server and the new server)

The SQL Server log shipping is a widely used technology. Many articles have been written on it. So, I am not going to explain the process to set up the Log shipping. You can read the following articles to understand the deployment and monitoring of the SQL Server log shipping.

SQL Server日志传送是一种广泛使用的技术。 关于它的文章很多。 因此,我将不解释设置日志传送的过程。 您可以阅读以下文章,以了解SQL Server日志传送的部署和监视。

  1. How to configure SQL Server Log Shipping 如何配置SQL Server日志传送
  2. Monitor Transaction Log shipping using T-SQL and SSMS 使用T-SQL和SSMS监视事务日志传送

I have already established a log shipping between the SQL01 (Old server) and the SQL02 (New server). To view the configuration, connect to SQL01 -> Expand Database -> Right-click on AdventureWorks2017 -> Select Properties. See the following image:

我已经在SQL01(旧服务器)和SQL02(新服务器)之间建立了日志传送。 要查看配置,请连接到SQL01- >展开数据库 ->右键单击AdventureWorks2017- >选择属性 。 见下图:

In Properties, click on Log Shipping. In the right pane, you can view the schedule of the transactional log backups, the list of secondary instances, and the secondary database. To view the configuration of the secondary server, click on the ellipsis (…) in the secondary server and instances grid view. See the following image:

在属性中,单击日志传送 。 在右窗格中,您可以查看事务日志备份时间表 ,辅助实例列表辅助数据库 。 要查看辅助服务器的配置,请在辅助服务器和实例网格视图中单击省略号(...)。 见下图:

On the secondary database setting screen, you can view the state of the secondary database, name of the SQL Job that is used to restore the transactional logs on the secondary database, and its schedule. See the following image:

在辅助数据库设置屏幕上,您可以查看辅助数据库的状态,用于在辅助数据库上还原事务日志SQL Job的名称及其计划。 见下图:

禁用日志传送作业并切换数据库服务器 (Disabling log shipping jobs and switch over the database server)

During maintenance hours, we can disable the log shipping jobs on SQL01 (Old Server) and SQL02 (New Server). Details of the SQL Jobs are as follows:

在维护时间内,我们可以在SQL01(旧服务器)和SQL02(新服务器)上禁用日志传送作业。 SQL作业的详细信息如下:

SQL Server instance

Name of the SQL Job

SQL01 (Old Server)

  1. LSBackup_AdventureWorks2017
  2. LSAlert_SQL01

SQL02 (New Server)

  1. LSAlert_SQL02
  2. LSCopy_SQL01_AdventureWorks2017
  3. LSRestore_SQL01_AdventureWorks2017

SQL Server实例

SQL作业的名称

SQL01(旧服务器)

  1. LSBackup_AdventureWorks2017
  2. LSAlert_SQL01

SQL02(新服务器)

  1. LSAlert_SQL02
  2. LSCopy_SQL01_AdventureWorks2017
  3. LSRestore_SQL01_AdventureWorks2017

To disable the job, execute the following queries:

要禁用该作业,请执行以下查询:

On SQL01:

在SQL01上:

EXEC msdb.dbo.sp_update_job @job_name=' LSBackup_AdventureWorks2017',@enabled = 0
Go
EXEC msdb.dbo.sp_update_job @job_name=' LSAlert_SQL01',@enabled = 0
Go

On SQL02:

在SQL02上:

EXEC msdb.dbo.sp_update_job @job_name='LSCopy_SQL01_AdventureWorks2017',@enabled = 0
Go
EXEC msdb.dbo.sp_update_job @job_name='LSRestore_SQL01_AdventureWorks2017',@enabled = 0
GoEXEC msdb.dbo.sp_update_job @job_name=' LSAlert_SQL02',@enabled = 0
Go

Now, on SQL01 (Old Server) generate a tail log backup of the AdventureWorks2017 database WITH NORECOVERY option by executing the following query:

现在,在SQL01(旧服务器)上,通过执行以下查询,生成带有NORECOVERY选项的AdventureWorks2017数据库的尾日志备份:

BACKUP LOG AdventureWorks2017 TO DISK = '\\SQL01\Backup\AdventureWorks2017_NORECOVERY.TRN' WITH NORECOVERY, COMPRESSION, CHECKSUM, STATS = 25

Once the backup is generated, restore the backup of AdventureWorks2017 on SQL02 (New server) with the Recovery option.

生成备份后,使用恢复选项在SQL02(新服务器)上恢复 AdventureWorks2017的备份。

RESTORE LOG [AdventureWorks2017] FROM  DISK = N'\\SQL01\Backup\AdventureWorks2017_NORECOVERY.TRN' WITH  FILE = 1,  NOUNLOAD,  STATS = 10
GO

Once the backup is restored successfully, the AdventureWorks2017 database is online on SQL02 (New server). See the following image:

成功还原备份后,AdventureWorks2017数据库在SQL02(新服务器)上处于联机状态。 见下图:

If required, update the connection strings of the application, and we are good to go.

如果需要,请更新应用程序的连接字符串,我们很好。

摘要 (Summary)

As a database administrator, sometimes we must think out of the box to fix the issues. In this article, I have explained how we can use SQL Server Log shipping technology to move a SQL Database to another server with minimum downtime.

作为数据库管理员,有时我们必须开箱即用以解决问题。 在本文中,我已经解释了如何使用SQL Server日志传送技术将SQL数据库移动到另一台服务器,而停机时间最少。

翻译自: https://www.sqlshack.com/move-sql-databases-to-a-different-server-using-sql-server-log-shipping/

使用SQL Server日志传送将SQL数据库移动到其他服务器相关推荐

  1. 什么是SQL Server日志传送?

    什么是SQL Server日志传送? (What is SQL Server log shipping?) SQL Server日志传送是一种涉及两个或多个SQL Server实例,并将事务日志文件从 ...

  2. 如何创建SQL Server日志传送

    In my last article, I show how to create Database Mirroring for high availability. This time, I will ...

  3. SQL Server 日志传送[转载]

    http://jimshu.blog.51cto.com/3171847/590413 SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件 ...

  4. SQL Server 日志传送

    SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件夹,主机备份放在这个文件夹,而且客户机有权访问这个共享文件夹. 二.基本配置 1.启动配 ...

  5. SQL Server 日志数据库清理办法

    更新一个SQL Server 日志数据库清理脚本,以备日后用到. USE [master] GO ALTER DATABASE [数据库名称] SET RECOVERY SIMPLE WITH NO_ ...

  6. sql server日志占用空间过大的问题

    一.关于日志的基本知识:     在 SQL Server 2000 和 SQL Server 2005 中,每个数据库都至少包含一个数据文件和一个事务日志文件.SQL Server 在该数据文件中以 ...

  7. 清理SQL Server日志释放文件空间的终极方法

    清理SQL Server日志释放文件空间的终极方法 转自:http://www.cnblogs.com/dudu/archive/2013/04/10/3011416.html [问题场景]有一个数据 ...

  8. Sql server 数据转到 Mysql 数据库

    Sql server 数据转到 Mysql 数据库http://www.bieryun.com/3355.html 在网上找了一些方案,目前一个可行的较好的方案,虽然不够完美但也十分好用: 用到的用具 ...

  9. SQL Server 备份与恢复之八:还原数据库

    本文主要来源于Microsoft<SQL Server 2008 R2联机丛书>.转裁请注明出处. 一.还原用户数据库 1."还原数据库"基本操作 (1)目标数据库 在 ...

最新文章

  1. 剑指offer面试题6:重建二叉树
  2. 编译原理扫描程序,判断当前输入的程序中程序调用的函数是那个函数原型
  3. java学习(162):同步对象锁
  4. Gmail的另类浏览法--RSS
  5. DTCC 2020 | 阿里云梁高中:DAS之基于Workload的全局自动优化实践
  6. HashMap的结构及源码分析
  7. caioj1495: [视频]基于连通性状态压缩的动态规划问题:Formula 2
  8. 蓝桥杯 ALGO-28 算法训练 星际交流
  9. laravel中修改默认时区
  10. 华为云虚拟服务器怎么搭建,云服务器怎么搭建虚拟主机
  11. 计算机更换固态硬盘方法,详解电脑更换固态硬盘需要重装系统吗
  12. 如何快速找到微信支付的商户号和商户密钥?望相互转告!
  13. nodejs 读取写入 plist 文件,使用 plist npm 包
  14. 计算机专业论文提纲,计算机硕士毕业论文提纲(范文精选)
  15. 89c51的万年历c语言,用AT89C51与DS1302做的万年历c语言编程
  16. 深圳市海平线科技有限公司
  17. 前端获取QQ音乐(mp3+m4a)
  18. 微信签到 表单 mysql_java做的一个简易的微信签到系统
  19. 什么是自律性?如何提高自律性?
  20. Wipro为悉尼水务局部署SAP S/4HANA®

热门文章

  1. if else if else语句格式_计算机各语言之间if...else区别
  2. 什么是立即执行函数,有什么作用?
  3. [导入]C++ OpenGL底层和C# GUI无缝联合!
  4. Java回文数的判断与生成
  5. Professional C# 6 and .NET Core 1.0 - Chapter 43 WebHooks and SignalR
  6. 设计模式之——单例模式(Singleton)的常见应用场景(转):
  7. TempTable临时表
  8. Something about TFS
  9. rollup配置及使用
  10. 浅谈函数的重入与不可重入