如何在多个SQL Server上执行作业

介绍 (Introduction)

In earlier chapter, we explained how to run queries in multiple SQL servers using the SQL Central Management Server. In this new chapter, we will show how to propagate a job from a SQL Server Master Agent Job to a target server.

在前面的章节中,我们解释了如何使用SQL Central Management Server在多个SQL服务器中运行查询。 在这一新的章节中,我们将展示如何将作业从SQL Server主代理作业传播到目标服务器。

This feature is called Multiserver Administration. In a multiserver administration, you need a Master Server and one or more target servers. In the master server, you create a copy of the job and then it is copied and executed in the target servers.

此功能称为多服务器管理。 在多服务器管理中,您需要一个主服务器和一个或多个目标服务器。 在主服务器中,创建作业的副本,然后将其复制并在目标服务器中执行。

The jobs are scheduled to run and are executed in each Target Server.

作业计划运行,并在每个目标服务器中执行。

要求 (Requirements)

  • SQL Server Installed (2 SQL Servers or 2 SQL Server Instances).

    已安装SQL Server(2个SQL Server或2个SQL Server实例)。
  • 2 SQL Server Agents running.

    2个SQL Server代理正在运行。

入门 (Getting started)

  1. Open the regedit, to edit the Windows registry and change the registry \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<instance_name>\SQL Server Agent\AllowDownloadedJobsToMatchProxyName to 1 in the master and target Servers.

    打开注册表编辑器,以编辑Windows注册表,并将主服务器和目标服务器中的注册表\ HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Microsoft SQL Server \ <instance_name> \ SQL Server Agent \ AllowDownloadedJobsToMatchProxyName更改为1。



    Figure 1. Registry 图1.注册表
  2. In the SQL Server Management Studio go to the SQL Server Agent, right click and select the Multi Server Administration and select the Make this a Master option.

    在SQL Server Management Studio中,转到SQL Server代理,右键单击并选择“多服务器管理”,然后选择“ 使它成为主服务器”选项



    Figure 2. The Multi Server option 图2.多服务器选项
  3. The wizard to create the master server will be displayed. Press Next.

    将显示创建主服务器的向导。 按下一步。



    Figure 3. The Master Server Wizard 图3.主服务器向导
  4. The next window is the Master Server Operator. The operator can receive notification using email, pager address or with a net send address. Specify the address of your preference and press next.

    下一个窗口是主服务器操作员。 操作员可以使用电子邮件,寻呼机地址或网络发送地址接收通知。 指定您的首选项地址,然后按下一步。



    Figure 4. The address of the operator 图4.操作员的地址
  5. In the Target Server window, we will select all the target servers. In this example, we only have one target server, but in the real-life, we may have several. Press the Add connection button to add a new target server.

    在“目标服务器”窗口中,我们将选择所有目标服务器。 在此示例中,我们只有一台目标服务器,但实际上,我们可能有几台。 按添加连接按钮以添加新的目标服务器。



    Figure 5. Target Servers 图5.目标服务器
  6. Connect to the SQL Server that will be used as a Target Server.

    连接到将用作目标服务器SQL Server。



    Figure 6. Connection information 图6.连接信息
  7. If everything is OK, you will receive a success message related to the compatibility between the 2 servers.

    如果一切正常,您将收到与两台服务器之间的兼容性有关的成功消息。



    Figure 7. Server Compatibility Check 图7.服务器兼容性检查
  8. You can view the report, save the report in a file, in the clipboard or send the report by email. In this example, we will select the first option.

    您可以查看报告,将报告保存在文件中,剪贴板中或通过电子邮件发送报告。 在此示例中,我们将选择第一个选项。



    Figure 8. Report options 图8.报告选项
  9. In this option, the report will show the result of the version compatibility between the master and target servers.

    在此选项中,报告将显示主服务器与目标服务器之间版本兼容性的结果。



    Figure 9. Compatibility Report 图9.兼容性报告


  10. Figure 10. Actions to enlist the target server and create the MSX Operator. 图10.注册目标服务器并创建MSX Operator的操作。
  11. You will notice that in the Master Server it says (MSX). It means that it is a Master Server. Right-click and select the New Job option.

    您会注意到,在主服务器中显示为(MSX)。 这意味着它是一个主服务器。 右键单击并选择“ 新作业”选项。



    Figure 11. Creating a new job. 图11.创建一个新作业。
  12. We will create a backup in the target server. If you have several target servers, the backup will be created in all of them.

    我们将在目标服务器中创建一个备份。 如果您有多个目标服务器,则将在所有目标服务器中创建备份。
  13. The creation of a job will be similar to local jobs. You just need a name and optionally a description.

    职位的创建将类似于本地职位。 您只需要一个名称和一个描述即可。



    Figure 12. Creating a backup 图12.创建一个备份
  14. Go to the Steps page and click the New button to create a new Job.

    转到“步骤”页面,然后单击“新建”按钮以创建新的作业。



    Figure 13. Creating new steps 图13.创建新步骤
  15. Specify the name and the command. In this example, we are backing up a database named db2 in the db2.bak file. You can modify the T-SQL sentences to your own needs:

    指定名称和命令。 在此示例中,我们将在db2.bak文件中备份名为db2的数据库。 您可以根据自己的需要修改T-SQL语句:

    BACKUP DATABASE [db2] TO DISK = N’C:\Backup\db2.bak’ WITH NO FORMAT
    GO

    备份数据库[db2]到磁盘= N'C:\ Backup \ db2.bak',不带格式



    Figure 14. The step to create backups 图14.创建备份的步骤
  16. This is the key section, go to the Targets page and select the target multiple servers where you want to run the job. In this example, we have one target server. Select the option target to multiple servers and check the target server(s) where you want to run the job.

    这是关键部分,请转到“目标”页面,然后选择要在其中运行作业的目标多台服务器。 在此示例中,我们有一台目标服务器。 选择多个服务器的目标服务器,然后检查要在其中运行作业的目标服务器。



    Figure 15. Selecting Target servers 图15.选择目标服务器


  17. Figure 16. Start Job at Step 图16.在步骤开始作业
  18. If everything is OK, you will receive a Success message that the job was posted remotely for execution.

    如果一切正常,您将收到一条成功消息,说明该作业已远程发布以执行。



    Figure 17. Success execution message. 图17.成功执行消息。
  19. A new backup will be created in the target server(s). As you can see, working with master and target servers is a straightforward process.

    将在目标服务器中创建一个新的备份。 如您所见,使用主服务器和目标服务器是一个简单的过程。



    Figure 18. The SQL Server backup created. 图18.创建SQL Server备份。

一些常见问题 (Some common problems)

A very common problem when you are creating the target server (step 10) is the following message:

创建目标服务器(步骤10)时,一个非常常见的问题是以下消息:


This error is related to the encryption between the master and target server.

此错误与主服务器和目标服务器之间的加密有关。

If you have this error, verify that the registry
\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<instance_name>\SQL Server Agent\MsxEncryptChannelOptions matches in the master and the target servers. If not, modify the registries. This registry controls the encryption between the master and target server. The value 0 means no encryption. 1 means to encrypt without a certificate and 2 with a certificate.

如果出现此错误,请验证注册表
\ HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Microsoft SQL Server \ <实例名称> \ SQL Server代理\ MsxEncryptChannelOptions在主服务器和目标服务器中匹配。 如果不是,请修改注册表。 该注册表控制主服务器和目标服务器之间的加密。 值0表示不加密。 1表示不使用证书进行加密,2表示使用证书。



一些管理选项 (Some administrative options)

In the master server (MSX), it is posible to administer the Multi server configuration. Right click on the SQL Server Agent and in the Multi Server Administration, select Manage Target Servers.

在主服务器(MSX)中,可以管理多服务器配置。 右键单击SQL Server代理,然后在“多服务器管理”中选择“ 管理目标服务器”。


The target server status tab shows the list of target servers, the local time

目标服务器状态选项卡显示目标服务器列表,本地时间

(the data and time of the target server in the local time) and the last time that the target server polled the master.

(目标服务器在本地时间中的数据和时间)以及目标服务器上次轮询主服务器的时间。

The force poll button forces the poll of the selected target server to the master server. The Force Defection button allows defecting the target server and the post-instruction allows posting the instructions.

强制轮询按钮将选定目标服务器的轮询强制到主服务器。 强制删除按钮可以使目标服务器损坏,而后指令则可以发布指令。


The Download Instruction shows the operations sent to the target servers, specifying the Object Name, Date posted and the Date that it was downloaded.

下载指令显示发送到目标服务器的操作,并指定对象名称,发布日期和下载日期。


需要代理时 (When the proxy is required)

If a proxy is necessary, you will need to create a credential and then associate it with the proxy. Make sure that the proxy name is the same in the Master and Target servers.

如果需要代理,则需要创建一个证书,然后将其与代理关联。 确保主服务器和目标服务器中的代理名称相同。

如何删除目标服务器 (How to remove the target server)

The option to remove the target server from the Multi-Server. You can do that on the target server.

从多服务器中删除目标服务器的选项。 您可以在目标服务器上执行此操作。

Go to the SQL Server Agent, right click and select Multi Server Administration>Defect

转到SQL Server代理,右键单击并选择“多服务器管理”>“缺陷”


结论 (Conclusion)

As you can see, creating a master server and target servers is a straightforward process. Once configured, you can send jobs to multiple servers at the same time.

如您所见,创建主服务器和目标服务器是一个简单的过程。 配置完成后,您可以同时将作业发送到多个服务器。

Some useful stored procedures:

一些有用的存储过程:

Some useful system views:

一些有用的系统视图:

翻译自: https://www.sqlshack.com/how-to-execute-jobs-on-multiple-sql-servers/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值