zoukankan      html  css  js  c++  java
  • 查看SQL Server日志 Part 1

    曾经有朋友问我数据被删除了,不借助第三方工具能不能查是什么时候发生的。 SQL Server提供了一个undocumented的函数fn_dblog可以让我们查看活动的transaction log。

    语法如下:

    ::fn_dblog(@StartingLSN,@EndingLSN)

    如果参数都为NULL默认是抓取所有的交易信息。

    使用这个函数我们可以查询DML,DDL信息,比如数据删除,修改更新等等。下面我们来看一个数据更新的例子:

    create table test(namevarchar(10))

    --插入条数据

    insert into testvalues('allen test')

    go 5


    ---查询对表Test的修改

    select [Transaction Name],Operation,AllocUnitId,AllocUnitName,[Begin Time]fromfn_dblog(null,null)

    where AllocUnitName ='dbo.test'andOperation='LOP_INSERT_ROWS'

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

    NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

    NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

    NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

    NULLLOP_INSERT_ROWS72057594040090624 dbo.test

    --删除表

    drop table test

    ----Log中查询对表的删除

    select [Transaction Name],Operation,AllocUnitId,AllocUnitName,[Begin Time]fromfn_dblog(null,null)

    where [Transaction Name] = 'DROPOBJ'

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    DROPOBJLOP_BEGIN_XACT NULL NULL

    --查询Page Split

    select Operation, AllocUnitName,COUNT(*)asNumberofIncidents

    from ::fn_dblog(null,null)

    where Operation =N'LOP_DELETE_SPLIT'

    group byOperation, AllocUnitName

    注意:从fn_dblog 查出来的LSN是不能直接作为参数的,需要将16进制转化为numeric:

    The LSN we havefrom the log dump above is 0000009d:0000021e:0001.To convert it:

    • Take the rightmost 4 characters (2-byte log record     number) and convert to a 5-character decimal number, including leading     zeroes, to get stringA
    • Take the middle number (4-byte log block number) and     convert to a 10-character decimal number, including leading zeroes, to get     stringB
    • Take the leftmost number (4-byte VLF sequence number)     and convert to a decimal number, with no leading zeroes, to get stringC
    • The LSN string we need is stringC + stringB + stringA

    So0000009d:0000021e:0001 becomes '157' + '0000000542'+ '00001' ='157000000054200001'.(来自SQLSkills)

    可以参考下面的脚本:

     

    DECLARE @pageID$ NVARCHAR(23), @pageIDNVARCHAR(50), @sqlCmdNVARCHAR(4000);

    SET @pageID$ ='0001:0000004d'--- PageID

     

    SELECT @pageID =

    CONVERT(VARCHAR(4),CONVERT(INT,CONVERT(VARBINARY,

    SUBSTRING(@pageID$, 0, 5), 2)))

    + ',' +

    CONVERT(VARCHAR(8),CONVERT(INT,CONVERT(VARBINARY,

    SUBSTRING(@pageID$, 6, 8), 2)))

     

    ---查看Page内容

    SET @sqlCmd = 'DBCC PAGE (''Crack_Me'','+ @pageID + ',3) WITH TABLERESULTS'

    EXECUTE(@sqlCmd)

    另外这个函数只能对当前活动的Log生效,如果Log备份之后就无法读取了,我们需要用另外一个函数(fn_dump_dblog http://blog.csdn.net/smithliu328/article/details/7817540)从备份中读取。上面也只是能够看到数据被修改,但是无法做到恢复数据。

    目前网上有人做了一些脚本可以实现恢复,我现在正在做测试,如果能够成功的话后面会把脚本发出来。

    转载于:http://blog.csdn.net/kevinsqlserver/article/details/7800628

     《解释一下SQLSERVER事务日志记录》

    -- select * FROM fn_dblog(null,null)

  • 相关阅读:
    maven打包成jar文件与打包成tar.gz文件
    maven命令错误:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME
    Nginx监听多个端口配置实例 Linux
    怎么修改redis-cli访问的地址
    eclipse的.properties文件中文显示问题
    Linux关闭防火墙命令red hat/CentOs7
    CentOS 7防火墙快速开放端口配置方法
    Window下Beego环境搭建和bee工具使用
    .netcore2.1 使用middleware对api请求头进行验证
    .netcore2.1 统一接口返回属性名称
  • 原文地址:https://www.cnblogs.com/elves/p/4025336.html
Copyright © 2011-2022 走看看