如何查询MySql日志

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介:   如何查询MySql日志 分类: mysql2012-02-23 19:14 26756人阅读 评论(2) 收藏 举报 mysqlcommandprintingserversocketoutput 今天美国老板发来一封email,一句What happen to .
 

如何查询MySql日志

分类: mysql

今天美国老板发来一封email,一句What happen to ...? 其他任何线索没有,我的给出答案,一一排查,看能否找到问题缘由或是恢复数据,更新问题?程序问题?结果都是没有头绪,最后想了下mysql的日志看能否看看,以前没看过mysql的日志,查查资料,最终皇天不负有心人,搞定了,现在总结下:

 

1.首先确认你日志是否启用了

  1. MySQL>show variables like 'log_bin'; 

2.如果启用了,即ON那日志文件就在MySQL的安装目录的data目录下

3.怎样知道当前的日志 

  1. MySQL> show master status;  

4.看二进制日志文件用MySQLbinlog

  1. shell>MySQLbinlog mail-bin.000001 

或者

  1. shell>MySQLbinlog mail-bin.000001 | tail  
因为mail-bin.000001是二进制的日志,所以想看日志就需要用mysqlbinlog命令,将二进制文件转换为日志文件,下面详细说说如何使用:
上面已经说过如果启用了日志文件,那么默认的日志文件就在data目录下(如果你没有更改的话)
进入存放日志文件目录,使用
mysqlbinlog localhost-bin.000202 > new_file_name.log
命令,将目标文件保存为日志文件,可指定保存路径下
 
有个小技巧跟大家介绍下,我在准备转换的时候发现日志文件有2G多,寻思着为什么mysql为什么不把日志按日志定期的分多个文件放呢。结果发现mysql有个更好的方法,可以通过时间参数获取某个时间段的数据,例子如下:
mysqlbinlog   --start-datetime="2010-11-20 00:00:00"  --stop-datetime="2010-11-21 00:00:00" 

[hx@localhost data]$ mysqlbinlog mysqlbinlog Ver 3.0 for pc-linux-gnu at i686 By Monty and Sasha, for your professional use This software comes with NO WARRANTY: This is free software, and you are welcome to modify and redistribute it under the GPL license

Dumps a MySQL binary log in a format usable for viewing or for piping to the mysql command line client

Usage: mysqlbinlog [options] log-files -d, --database=name List entries for just this database (local log only). -D, --disable-log-bin  Disable binary log. This is useful, if you enabled --to-last-log and are sending the output to the same MySQL server. This way you could avoid an endless loop. You would also like to use it when restoring after a crash to avoid duplication of the statements you already have. NOTE: you will need a SUPER privilege to use this option. -f, --force-read    Force reading unknown binlog events. -?, --help          Display this help and exit. -h, --host=name     Get the binlog from server. -o, --offset=#      Skip the first N entries. -p, --password[=name]  Password to connect to remote server. -P, --port=#        Use port to connect to the remote server. -j, --position=#    Deprecated. Use --start-position instead. --protocol=name     The protocol of connection (tcp,socket,pipe,memory). -r, --result-file=name  Direct output to a given file. -R, --read-from-remote-server  Read binary logs from a MySQL server --open_files_limit=#  Used to reserve file descriptors for usage by this program -s, --short-form    Just show the queries, no extra info. -S, --socket=name   Socket file to use for connection. --start-datetime=name  Start reading the binlog at first event having a datetime equal or posterior to the argument; the argument must be a date and time in the local time zone, in any format accepted by the MySQL server for DATETIME and TIMESTAMP types, for example: 2004-12-25 11:25:56 (you should probably use quotes for your shell to set it properly). --stop-datetime=name  Stop reading the binlog at first event having a datetime equal or posterior to the argument; the argument must be a date and time in the local time zone, in any format accepted by the MySQL server for DATETIME and TIMESTAMP types, for example: 2004-12-25 11:25:56 (you should probably use quotes for your shell to set it properly). --start-position=# Start reading the binlog at position N. Applies to the first binlog passed on the command line. --stop-position=#   Stop reading the binlog at position N. Applies to the last binlog passed on the command line. -t, --to-last-log   Requires -R. Will not stop at the end of the requested binlog but rather continue printing until the end of the last binlog of the MySQL server. If you send the output to the same MySQL server, that may lead to an endless loop. -u, --user=name     Connect to the remote server as username. -l, --local-load=name  Prepare local temporary files for LOAD DATA INFILE in the specified directory. -V, --version       Print version and exit.

Variables (--variable-name=value) and boolean options {FALSE|TRUE} Value (after reading options) --------------------------------- ----------------------------- database                          (No default value) disable-log-bin                   FALSE force-read                        FALSE host                              (No default value) offset                            0 port                              3306 position                          4 read-from-remote-server           FALSE open_files_limit                  64 short-form                        FALSE socket                            (No default value) start-datetime                    (No default value) stop-datetime                     (No default value) start-position                    4 stop-position                     18446744073709551615 to-last-log                       FALSE user                              (No default value) local-load                        (No default value)

2009.09.30 检查一个应用的问题的时候,发现通过 oracle 的 dblink 连接 mysql 进行更新等操作的时候,mysql 不会把操作的 sql 语句记录到日志文件里,有点点郁闷了

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3天前
|
存储 关系型数据库 MySQL
mysql数据库查询时用到的分页方法有哪些
【8月更文挑战第16天】在MySQL中,实现分页的主要方法包括:1)使用`LIMIT`子句,简单直接但随页数增加性能下降;2)通过子查询优化`LIMIT`分页,提高大页码时的查询效率;3)利用存储过程封装分页逻辑,便于复用但需额外维护;4)借助MySQL变量实现,可能提供更好的性能但实现较复杂。这些方法各有优缺点,可根据实际需求选择适用方案。
|
5天前
|
存储 监控 Serverless
阿里泛日志设计与实践问题之Grafana Loki在日志查询方案中存在哪些设计限制,如何解决
阿里泛日志设计与实践问题之Grafana Loki在日志查询方案中存在哪些设计限制,如何解决
|
7天前
|
SQL 存储 JSON
更快更强,SLS 推出高性能 SPL 日志查询模式
从海量的日志数据中,按照各种灵活的条件进行即时查询搜索,是可观测场景下的基本需求。本文介绍了 SLS 新推出的高性能 SPL 日志查询模式,支持 Unix 风格级联管道式语法,以及各种丰富的 SQL 处理函数。同时通过计算下推、向量化计算等优化,使得 SPL 查询可以在数秒内处理亿级数据,并支持 SPL 过滤结果分布图、随机翻页等特性。
268 65
|
1天前
|
存储 关系型数据库 MySQL
MySQL bit类型增加索引后查询结果不正确案例浅析
【8月更文挑战第17天】在MySQL中,`BIT`类型字段在添加索引后可能出现查询结果异常。表现为查询结果与预期不符,如返回错误记录或遗漏部分数据。原因包括索引使用不当、数据存储及比较问题,以及索引创建时未充分考虑`BIT`特性。解决方法涉及正确运用索引、理解`BIT`的存储和比较机制,以及合理创建索引以覆盖各种查询条件。通过`EXPLAIN`分析执行计划可帮助诊断和优化查询。
|
5天前
|
关系型数据库 MySQL 索引
mysql8.0中fulltext不能查询到中文的解决方法
确保MySQL服务器字符集为`utf8mb4`,并设置`ngram_token_size=1`以支持单字搜索。如已更改此参数且存在全文索引,需删除原有索引并重建,使用`WITH PARSER ngram`指定解析器。例如: ``` ALTER TABLE your_table DROP INDEX idx_fulltext, ADD FULLTEXT INDEX idx_fulltext (your_column) WITH PARSER ngram; ```
|
7天前
|
存储 算法 关系型数据库
探索MySQL递归查询,优雅的给树结构分页!
总结起来,对于MySQL中的树结构数据,递归查询结合预排序遍历树算法可以实现优雅的分页,但需要注意性能优化和数据更新的问题。这项技术提供了一种高效处理层级数据的工具,使得开发者可以在复杂的数据结构下实现直观和可靠的数据查询。
18 1
|
12天前
|
SQL 关系型数据库 MySQL
MySQL 常见日志清理策略
MySQL 数据库服务器使用多种类型的日志来记录操作和事件,这对于故障诊断、审计和性能分析非常重要。然而,这些日志文件会随着时间的推移而不断增长,可能会占用大量的磁盘空间。因此,定期清理这些日志是必要的,本篇文章我们一起来学习下如何清理 MySQL 中的日志文件。
32 3
|
4天前
|
存储 SQL 关系型数据库
探索MySQL的执行奥秘:从查询执行到数据存储与优化的深入解析
探索MySQL的执行奥秘:从查询执行到数据存储与优化的深入解析
|
8天前
|
SQL 关系型数据库 MySQL
MySQL——如何查看MySQL登录日志
MySQL——如何查看MySQL登录日志
21 0
|
1天前
|
SQL 存储 关系型数据库
数据库-MySQL-01(一)
数据库-MySQL-01(一)
13 4

热门文章

最新文章