msdb 数据库_系统msdb数据库的介绍和提示

msdb 数据库

介绍 (Introduction)

In a previous article, we talked about the tempdb system database. In this article, we will talk about the msdb database.

在上一篇文章中,我们讨论了tempdb系统数据库 。 在本文中,我们将讨论msdb数据库。

The msdb database stores information like the SQL Server Agent information, backup information, log shipping, maintenance plans.

msdb数据库存储诸如SQL Server代理信息,备份信息,日志传送,维护计划之类的信息。

In this article we will describe internal system tables of the msdb database and create some jobs to see the tables used.

在本文中,我们将描述msdb数据库的内部系统表,并创建一些作业以查看所使用的表。

要求 (Requirements)

  • SQL Server installed (we are using the SQL 2016, but earlier versions are valid).

    已安装SQL Server(我们正在使用SQL 2016,但早期版本有效)。

入门 (Getting started)

备份系统表 (Backup system tables)

  1. We will start creating a backup to show how the data is stored in the msdb.

    我们将开始创建备份,以显示数据如何存储在msdb中。

  2. In SQL Server Management Studio (SSMS), right click on a database and select the option Tasks>Back up

    在SQL Server Management Studio(SSMS)中,右键单击数据库,然后选择选项“ 任务”>“备份”



    Figure 1. Backup option 图1.备份选项
  3. Press OK to backup with the default values:

    按确定以使用默认值备份:



    Figure 2. Backup options 图2.备份选项
  4. In the msdb database, you can check the backup information in the backup system tables. For example, you can check the backup file information in the dbo.backupfile:

    在msdb数据库中,您可以在备份系统表中检查备份信息。 例如,您可以在dbo.backupfile中检查备份文件信息:



    Figure 3. The backupfile system table 图3. backupfile系统表
  5. You can check information like the file size, physical location of the backup, etc:

    您可以检查信息,例如文件大小,备份的物理位置等:



    Figure 4. The backup file information 图4.备份文件信息
  6. In the dbo.backupset system table you can find the name of the backup and the backup configuration options like the backup size, encryption information and more:

    在dbo.backupset系统表中,您可以找到备份的名称和备份配置选项,例如备份大小,加密信息等:



    Figure 5. The backupset information 图5.备份集信息
  7. As you can see, all the information is stored in the MSDB system views. If you create a backup it is a good practice to backup the MSDB database also to track the changes.

    如您所见,所有信息都存储在MSDB系统视图中。 如果创建备份,则最好同时备份MSDB数据库以跟踪更改。

  8. 日志传送系统表 (Log Shipping system tables)

    1. Other tables used in the MSDB database are the Log Shipping system tables. These tables are used to restore the Log Shipping information. Log Shipping is a feature used to have a secondary server ready in case that the primary server fails. This method uses Transaction Logs to replicate the data. For more information about Log Shipping, you can read this related article: How to create SQL Server Log Shipping

      MSDB数据库中使用的其他表是日志传送系统表。 这些表用于还原日志传送信息。 日志传送是一项功能,用于在主服务器发生故障时准备好辅助服务器。 此方法使用事务日志来复制数据。 有关日志传送的更多信息,您可以阅读以下相关文章: 如何创建SQL Server日志传送

      You can see the list of system tables in the following picture:

      您可以在下图中看到系统表的列表:



      Figure 6. Log Shipping system tables 图6.日志传送系统表
    2. Whenever you change the Log Shipping configuration option it is a good practice to backup the msdb database.

      每当您更改“日志传送”配置选项时,备份msdb数据库都是一个好习惯。

    3. SQL Agent系统表 (SQL Agent System tables)

      1. In the demo we will create a Database Backup using PowerShell in the SQL Agent and show the tables used.

        在演示中,我们将使用SQL Agent中的PowerShell创建数据库备份并显示使用的表。

      2. In the SQL Server Agent node right click on Jobs and select new job:

        在“ SQL Server代理”节点中,右键单击“作业”,然后选择新作业:



        Figure 7. Creating a new job 图7.创建一个新工作
      3. Add a name for the job:

        为工作添加名称:



        Figure 8. General job information 图8.常规职位信息
      4. In the step page, press the New button:

        在步骤页面中,按“新建”按钮:



        Figure 9. Job steps 图9.作业步骤
      5. Specify a name. In the type, select Power Shell and in the Command, write the following commands:

        指定一个名称。 在类型中,选择“ Power Shell”,然后在“命令”中编写以下命令:

        Backup-SqlDatabase –ServerInstance “OLAPSQLDAN” –Database “Test”

        Backup-SqlDatabase –服务器实例“ OLAPSQLDAN” –数据库“ Test”

        The backup-sqlDatabase cmdlet, is used to backup databases. In this example, we are backing up the Test database in the OLAPSQLDAN Server:

        backup-sqlDatabase cmdlet用于备份数据库。 在此示例中,我们将在OLAPSQLDAN服务器中备份Test数据库:



        Figure 10. Powershell Backup Cmdlet 图10. Powershell备份Cmdlet
      6. We can define a Schedule for the job. In the Schedule page, press the new button:

        我们可以为工作定义时间表。 在“计划”页面中,按新按钮:



        Figure 11. New Schedule 图11.新时间表
      7. Add a name and specify the schedule to run the job:

        添加名称并指定运行作业的时间表:



        Figure 12. Schedule information 图12.计划信息
      8. In the Alert section, press the Add button:

        在“警报”部分,按“添加”按钮:



        Figure 13. The Alerts page 图13. Alerts页面
      9. We will generate an alert if the Database file of the test database rises above 10,000 Kb:

        如果测试数据库的数据库文件超过10,000 Kb,我们将生成警报:



        Figure 14. Alert options 图14.警报选项
      10. Press OK to save the job. Once the job is create right click the Operators folder and select New Operator:

        按确定保存作业。 创建作业后,右键单击Operators文件夹,然后选择New Operator:



        Figure 15. Creating a new operator 图15.创建一个新的运算符
      11. Add a Name and an email:

        添加名称和电子邮件:



        Figure 16. New operator properties 图16.新的操作员属性
      12. Save the operator information.

        保存操作员信息。

      13. Open the Job created before. Go to the notification page. Check the E-mail and select the operator created on step 11:

        打开之前创建的作业。 进入通知页面。 检查电子邮件并选择在步骤11中创建的操作员:



        Figure 17. Job notification 图17.作业通知
      14. Start the job:

        开始工作:



        Figure 18. Starting a job 图18.开始工作
      15. You will receive a success message after starting the job:

        开始工作后,您将收到一条成功消息:



        Figure 19. Success job message 图19.成功作业消息
      16. You can check that the backup was created successfully:

        您可以检查是否已成功创建备份:



        Figure 20. The backup created 图20.创建的备份
      17. All the job information is stored in the msdb system tables. For example the execution information is stored in the sysjobactivity:

        所有作业信息都存储在msdb系统表中。 例如,执行信息存储在sysjobactivity中:



        Figure 21. The job activity table 图21.作业活动表
      18. The job activity table contains the information about the executed jobs:

        作业活动表包含有关已执行作业的信息:



        Figure 22. The table information 图22.表信息
      19. The sysjobhistory contains historical information like the error messages and the execution date and time:

        sysjobhistory包含历史信息,例如错误消息以及执行日期和时间:



        Figure 23. The sysjobhistory table 图23. sysjobhistory表
      20. You can also see the success messages and operator information:

        您还可以查看成功消息和操作员信息:



        Figure 24. The sysjobhistory data 图24. sysjobhistory数据
      21. The sysjobs table contains the list of jobs available:

        sysjobs表包含可用作业列表:



        Figure 25. The sysjobs table 图25. sysjobs表
      22. The sysjobschedules contains the schedules created in figure 12 :

        sysjobschedules包含在图12中创建的日程表:



        Figure 26. The data in the sysjob table 图26. sysjob表中的数据
      23. The sysjobschedules contain the schedules created in figure 12:

        sysjobschedules包含在图12中创建的日程表:



        Figure 27. The sysjobschedules 图27. sysjobschedules
      24. The sysjobsteps table contains the job steps created in figure 10:

        sysjobsteps表包含图10中创建的作业步骤:



        Figure 28. The PowerShell job step 图28. PowerShell作业步骤
      25. We can also see the job alerts created on the figure 14 using the sysalerts:

        我们还可以使用sysalerts来查看图14中创建的作业警报:



        Figure 29. The sysalerts system table 图29. sysalerts系统表
      26. In the sysoperators table, you can see the operator created in figure 16

        在sysoperators表中,您可以看到在图16中创建的运算符



        Figure 30. The sysoperators table 图30. sysoperators表

      Whenever you make important changes in the SQL Server Agent it is a good practice to backup the msdb database.

      每当您在SQL Server代理中进行重要更改时,备份msdb数据库都是一个好习惯。

      如果没有备份,如何还原msdb数据库? (How can I restore the msdb database if I do not have a backup?)

      You can rebuild your msdb database using the instmsdb script included in the MSSQL\Install folder. For more information about restoring system databases, refer to the following link:

      您可以使用MSSQL \ Install文件夹中包含的instmsdb脚本来重建msdb数据库。 有关还原系统数据库的更多信息,请参考以下链接:

如何在SSMS中隐藏msdb数据库? (How can I hide the msdb database in the SSMS?)

If you want to hide the system database you can do so in SSMS. Go to Tools>Options>Startup and selecting the option Hide system object form Object Explorer:

如果要隐藏系统数据库,可以在SSMS中进行隐藏。 转到“工具”>“选项”>“启动”,然后选择选项“从对象资源管理器隐藏系统对象”:

You will need to close and reopen the SSMS. Once it is done, the system tables including the MSDB will not be visible in the solution explorer:

您将需要关闭并重新打开SSMS。 一旦完成,包括MSDB在内的系统表将在解决方案资源管理器中不可见:


However, you can query the system tables using T-SQL.

但是,您可以使用T-SQL查询系统表。

结论 (Conclusions)

The msdb database is an important database used to store backup, Log Shipping and Agent information. Backup your msdb datatabase whenever you create backups, when you change the SQL Agent information or when you change the log shipping information or when you modify information that is stored in the msdb database.

msdb数据库是用于存储备份,日志传送和代理信息的重要数据库。 每当您创建备份,更改SQL代理信息,更改日志传送信息或修改存储在msdb数据库中的信息时,都备份msdb数据库。

其他建议 (Other recommendations)

  • Make sure to store the MSDB transaction log in a fault tolerant storage device.

    确保将MSDB事务日志存储在容错存储设备中。

  • Set the recovery model to full (by default it is simple).

    将恢复模型设置为完整(默认情况下很简单)。

参考资料 (References)

For more information, refer to these links:

有关更多信息,请参考以下链接:

翻译自: https://www.sqlshack.com/system-msdb-database-introduction-tips/

msdb 数据库

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值