设为首页 收藏本站
查看: 550|回复: 0

[经验分享] SQL Server 错误日志(ErrorLog)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2018-10-11 12:14:33 | 显示全部楼层 |阅读模式
  SQL Server 使用ErrorLog记录SQL Server启动和运行过程中的信息,具体信息参考:《SQLSERVER errorlog讲解》。通常来说,ErrorLog是指SQL Server Error Log,其实,SQL Server存在另外一种类型,SQL Server Agent ErrorLog,用于记录Agent的运行信息。
  默认情况下,SQL Server 保存 7 个 ErrorLog 文件,分别命名为: ErrorLog,ErrorLog.n(n=1,2,3,4,5,6)。ErrorLog 文件包含的信息最新,ErrorLog.6 文件包含的信息最老。每次重启 SQL Server 时,这些日志文件都做如下循环: 删除 ErrorLog.6 文件,将ErrorLog.5重命名为ErrorLog.6,依次类推,直到将ErrorLog重命名为ErrorLog.1,最后新建一个ErrorLog文件,用于存储SQL Server 运行过程的信息。
  SQL Server Agent ErrorLog的文件名是:SQLAgent.n(n=1,2,3,4,5,,,,)。
  ErrorLog的存放路径是在:C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log
  1,使用SSMS查看

  2,使用TSQL 脚本查看
  2.1 查看Errorlog的信息
  使用 sys.xp_enumerrorlogs 查看 ErrorLog Files的创建日期和大小,其创建日期就是第一条记录插入的日期。
exec sys.xp_enumerrorlogs
  切换ErrorLog,查看ErrorLog Files的信息
dbcc errorlog
  2.2,读取ErrorLog的信息
  SQL Server提供了存储过程sys.xp_readerrorlog及sys.sp_readerrorlog,用于查看Errorlog记录的错误日志。
  sys.xp_readerrorlog 有7个参数
  1. 存档编号(0~99),其值是 sys.xp_enumerrorlogs返回的Archive#字段的值, 0 代表的是ErrorLog,1代表的是ErrorLog.1。
  2. 日志类型(1为SQL Server日志,2为SQL Server Agent日志)
  3. 查询包含的字符串
  4. 查询包含的字符串,参数4和参数5的逻辑关系是and(与关系)
  5. LogDate开始时间
  6. LogDate结束时间
  7. 结果排序,按LogDate排序(Desc、Asc)
  sys.sp_readerrorlog有四个参数,和sys.xp_readerrorlog的前四个参数相同,sys.sp_readerrorlog内部使用sys.xp_readerrorlog来实现。
  查看登陆失败的错误日志,可以看到参数4和参数5是过滤Text字段。
exec sys.sp_readerrorlog 0,1,'login','failed'
  3,ErrorLog的Rollover

  如果Current ErrorLog 文件很大,那么加载和查看的过程十分缓慢,可以运行 sys.sp_cycle_errorlog 或 DBCC ErrorLog,或exec ('DBCC ErrorLog'),手动强制ErrorLog 文件迭代,避免单个ErrorLog File>  推荐阅读:
  SQL Server 错误日志过滤(ERRORLOG)
  SQL Server 错误日志收缩(ERRORLOG)
  Appendix
  Talking about SQL Server and SQL Server agent error logs. Here are few interesting things that we can do from query analyzer in order to read and analyze the SQL Server and agent error logs.
  1, sys.XP_READERRORLOG
  Syntax: xp_ReadErrorLog a,b,c,d
  a -> default is 0. It accepts only integers. 0 mean the current error log, 1 means 1st archive and so on.
  b -> default is 1. Accepts value equals 1 for SQL Server error log and 2 for SQL Agent error log.
  c -> accepts varchar chanracter upto 255 characters. Default is Null.
  d -> accepts varchar chanracter upto 255 characters. Default is Null.
  1) To read the current SQL Server error log i.e. ERRORLOG file
xp_readerrorlog  
xp_readerrorlog 0 xp_readerrorlog 0,1
  2) TO read SQL Server error log archive 1 i.e. ERRORLOG.1 file
xp_readerrorlog 1 xp_readerrorlog 1,1  3) To read the current SQL Server Agent error log i.e. SQLAGENT.OUT file
xp_readerrorlog 0,2  4) To read SQL Server error log archive 1 i.e. SQLAGENT.1 file
xp_readerrorlog 1,2  5) To search for any specific text in current SQL Server error log
xp_ReadErrorLog 0, 1, 'Failed'  6) To search for entries in current SQL Server error log which contain both ‘Failed’ and ‘Login’ in a row.
xp_ReadErrorLog 0, 1, 'Failed'.'Login'  2,sys.XP_ENUMERRORLOGS
  Syntax: xp_enumerrorlogs a
  a -> default is 1. Accepts value equals 1 for SQL Server error log and 2 for SQL Agent error log.

  1) List all the avilable SQL Server error log archives, there last modified date and>xp_enumerrorlogs  
xp_enumerrorlogs 1

  2) List all the avilable SQL Server Agent error log archives, there last modified date and>xp_enumerrorlogs 2  3,recycle errorlog
  To Recycle Current SQL Server Error Log
exec sp_cycle_errorlog  To Recycle Current SQL Server Agent Error Log
exec sp_cycle_agent_errorlog


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-620367-1-1.html 上篇帖子: SQL Server 死锁的监视 下篇帖子: SQL server 实现自动异地备份
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表