search
HomeDatabaseMysql TutorialMS SQL 日志记录管理

MS SQL的日志信息/日志记录,可能对你来说,既熟悉又陌生,熟悉是因为你可能一直都在使用,查看、关注一些日志信息/记录,例如,作业历史记录;陌生是因为你可能从不关注日志信息/记录的管理,这里我一直用日志信息/记录这个词,而没有用日志文件这个词来阐

    MS SQL的日志信息/日志记录,可能对你来说,既熟悉又陌生,熟悉是因为你可能一直都在使用,查看、关注一些日志信息/记录,例如,作业历史记录;陌生是因为你可能从不关注日志信息/记录的管理,这里我一直用日志信息/记录这个词,而没有用日志文件这个词来阐述,是想让大家把它和事务日志文件(ldf)区分开来,香港服务器租用,网上你用日志文件做搜索关键词,可能搜出来的都是事务日志相关的信息。其实它真的也叫日志文件,这篇文章我大概从日志记录分类、如何查看日志记录、日志记录的位置、日志记录的设置、为什么错误日志会暴增、如何清除日志记录等方面来讲述。

日志记录分类

按日志文件查看器,习惯将错误日志归为SQL SERVER、 SQL SERVER 代理, Windows应用程序日志,数据库邮件等四类错误日志记录。如果还考虑维护计划、远程维护计划、作业历史记录日志信息,总共是7类日志信息文件。

clip_image002

其中Windows应用程序日志类型又分为系统日志(System)、安全日志(Security)、应用程序日志(Application), PatchLink日志等几种,我在服务器(Windows Server  2008 R2 Standard)上打开SSMS,居然发现又多了HardwareEvents, Internet Explorer、Windows PowerShell等日志文件。这些都是系统的日志文件。你不必太纠结有多少种。

clip_image004

日志记录位置

 

SQL SERVER日志记录、 SQL SERVER代理记录的位置如下所示, SQL SERVER日志记录一般存储在ERRORLOG.n(n为数字)文件里, SQL SERVER代理日志记录位于SQLAGENT.n这类的文件里。当然这跟数据库的版本也有关系:

版本

路径

SQL SERVER 2005

Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG

SQL SERVER 2008

Program Files\Microsoft SQL Server\MSSQL10.实例名\MSSQL\LOG

SQL SERVER 2008 R2

Program Files\Microsoft SQL Server\MSSQL10_50.实例名\MSSQL\LOG

    SQL SERVER 2005,默认情况下,错误日志位于 Program Files\Microsoft SQL Server\MSSQL.n\MSSQL\LOG\ERRORLOG 和 ERRORLOG.n 文件中。其中MSSQL.n的区分为:

        MSSQL.1:SQLSERVER

        MSSQL.2:SSAS

        MSSQL.3:SQLExpress

        MSSQL.4:SSRS

所以,一般情况下,你只需要关注MSSSQL.1目录下的日志文件

clip_image006

那么,数据库邮件日志记录位于哪里呢?作业历史记录日志信息、Windows应用程序日志又位于哪里呢?是不是从没考虑过这些?

数据库邮件日志记录信息可以从视图msdb.dbo.sysmail_event_log查询得到,实质保存在[dbo].[sysmail_log]表里面。

sysmail_event_log

作业历史记录日志信息都保存在msdb.dbo.sysjobhistory的表里面,其中run_status字段代表作业执行状态

0 = 失败

1 = 成功

2 = 重试

3 = 已取消

4= 正在进行

所有Windows应用程序日志其实都位于同一位置%SystemRoot%\System32\Winevt\Log。像Application日志文件位于%SystemRoot%\System32\Winevt\Logs\Application.evtx,如下所示,

clip_image008

查看日志记录

查看日志记录可以确保进程(例如,备份和还原操作、批处理命令或其他脚本和进程)成功完成。此功能可用于帮助检测任何当前或潜在的问题领域,包括自动恢复消息(尤其是在 SQL Server 实例已停止并重新启动时)、内核消息或其他服务器级错误消息。

方式1: 查看错误日志文件

对SQL SERVER、SQL SERVER AGENT日志记录信息,你可以直接去log目录下找到ERRORLOG、SQLAGENT日志文件,直接打开查看;而像Windows应用程序日志记录,去到%SystemRoot%\System32\Winevt\Log目录,找到对应的日志文件,直接打开查看。

 

方式2:通过SSMS来查看日志记录

查看与常规 SQL Server 活动相关的日志

  • 在对象资源管理器中,依次展开“管理”和“SQL Server 日志”,再双击“当前”,将显示 SQL Server、“SQL 代理”和“Windows 事件”日志。
  • 查看与作业相关的日志

  • 在对象资源管理器中,展开“SQL Server 代理”,右键单击“作业”,再单击“查看历史记录”,此时将显示“作业历史记录”和“SQL 代理”日志。
  • 查看与维护计划相关的日志

  • 在对象资源管理器中,展开“管理”,右键单击“维护计划”,再单击“查看历史记录”,此时将显示“维护计划”、“作业历史记录”和“SQL 代理”日志。
  •  

    方式3:用脚本查看

    3.1 对于SQL SERVER日志文件,可以通过下面脚本查看:

       --查看日志文件的存档号

        EXEC master.dbo.sp_enumerrorlogs

       用这个命令可以查看日志文件的大小,这个非常有用,你可以把大小异常的文件给排查出来。

     

       --根据存档号查看该档日志内容

       EXEC master.dbo.xp_readerrorlog 1

       --根据job_id查看SQL SERVER日志记录

      SELECT * FROM  msdb.dbo.sysjobhistory WHERE job_id='36E9232B-CD5B-4646-9BED-B8242090FFF9'

     

    3.2 对于作业历史记录日志信息,你既可以通过下面存储过程查看,也可以直接查询对应的表。

    例如,服务器空间,我要查看作业“ServerDiskCapacityCheck”的历史记录

    Code Snippet

     

    3.3数据库邮件记录查看

    SELECT * FROM  msdb.dbo.sysmail_event_log;

     

    日志记录管理

     

    设置最大错误日志文件数

        1:在对象资源管理器中,连接到 SQL Server 数据库引擎实例,再展开该实例。

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

    InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

    MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

    Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

    Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

    MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

    MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

    MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

    MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

    MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

    MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

    MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

    The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

    SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

    MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

    The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools