首页 > 技术文章 > slow-log 和bin-log相关参数介绍

start-from-zero 2020-03-27 17:35 原文

1. slow-log

  • show global status

Slow_queries   --------慢查询的次数,即查询的时间超过long_query_time设置的时间(不能修改)

  • 配置文件

log-short-format   --------是否记录更少的东西去slow log(不能动态修改)

  • show variables

slow_query_log    --------是否开启慢查询日志(动态修改)

slow_query_log_file  ------记录慢查询SQL的日志文件路径(动态修改)

long_query_time   -------超过该参数设置的值为慢查询(可动态修改)

log_slow_admin_statements   ---是否把ALTER TABLE, ANALYZE TABLE, CHECK TABLE, CREATE INDEX, DROP INDEX, OPTIMIZE TABLE,和 REPAIR TABLE等管理语句执行时超过long_query_time也记入慢查询日志(可动态修改)

log_slow_slave_statements   — 开启此参数后,从库会把SQL thread执行超过long_query_time的语句写到slow log,但是当binlog-format=row时无效(可以动态修改)

log_queries_not_using_indexes  ---是否把没有使用索引的语句也记入慢查询(可动态修改)

log_throttle_queries_not_using_indexes   ---当把没有使用索引的语句记入慢查询时,日志内容会增加的很快,可以通过这个参数来设置每分钟的限制(可动态修改)

min_examined_row_limit    ----- 如果慢查询检查的行数不超过这个参数设置的值将不会被记录(可动态修改)

log_timestamps   ---- 控制 error log、slow_log、genera log,等等记录日志的显示时间参数(可动态修改)

2.如何查看发生了哪些慢查询?

show variables like '%slow%';

##slow_query_log =on 表示慢查询记录开启

##slow_query_log_file ---表示slowlog日志文件的位置

 

3. 使用pt 工具分析慢查询

pt-query-digest /usr/logs/slow3307.log

pt-query-digest --user=xxx --password=xxx --review S=/tmp/mysql.sock,D=test,t=slow_query --create-review-table /usr/logs/slow3307.log  ## 把分析结果写入表中

最近1小时 ---pt-query-digest --since=1h  /usr/logs/slow3307.log  ##按时间

        时间区间pt-query-digest --since '2020-03-10 11:54:20' --until '2020-03-10 12:00:00'/usr/logs/slow3307.log  ## 按时间

# 170ms user time, 10ms system time, 25.05M rss, 203.14M vsz #####用户时间,系统时间,物理内存,虚拟内存 
# Current date: Tue Mar 10 12:42:34 2020 ###工具的执行时间 
# Hostname: william01guo-gz-web-upwup.vclound.com ###运行工具的主机名 
# Files: /usr/logs/slow3307.log ###被分析的slow log文件 
# Overall: 3 total, 2 unique, 0.01 QPS, 0.00x concurrency ________________ ###语句的总数量,不重复的语句总数,QPS和并发数
# Time range: 2020-03-10T11:52:43 to 2020-03-10T12:02:19 ###日志中记录的时间范围 # Attribute total min max avg 95% stddev median # ============ ======= ======= ======= ======= ======= ======= ======= # Exec time 28ms 1ms 26ms 9ms 26ms 11ms 1ms ###执行时间 # Lock time 1ms 228us 749us 405us 725us 233us 236us ###锁占用的时间 # Rows sent 1.19k 356 509 407 487.09 66.43 346.17 ###发送到客户端的行数 # Rows examine 2.38k 712 1018 814 1012.63 153 685.39 ###select语句扫描行数
# Query size 36 11 14 12 13.83 1.41 10.84 ###查询的字节数 # Profile # Rank Query ID Response time Calls R/Call V/M Item ###排名 语句ID,响应时间,该查询在分析中的占比,执行次数,平均执行的相应时间, ###响应时间Variance-to-mean的比率,查询对象
# ==== ================== ============= ===== ====== ===== ============== # 1 0x911658842DA64DC4 0.0258 90.9% 1 0.0258 0.00 SHOW VARIABLES # 2 0x31DA25F95494CA95 0.0026 9.1% 2 0.0013 0.00 SHOW STATUS # Query 1: 0 QPS, 0x concurrency, ID 0x911658842DA64DC4 at byte 0 ________ # This item is included in the report because it matches --limit. # Scores: V/M = 0.00 # Time range: all events occurred at 2020-03-10T11:52:43 # Attribute pct total min max avg 95% stddev median # ============ === ======= ======= ======= ======= ======= ======= ======= # Count 33 1 # Exec time 90 26ms 26ms 26ms 26ms 26ms 0 26ms # Lock time 61 749us 749us 749us 749us 749us 0 749us # Rows sent 41 509 509 509 509 509 0 509 # Rows examine 41 1018 1018 1018 1018 1018 0 1018 # Query size 38 14 14 14 14 14 0 14 # String: # Hosts localhost # Users dba # Query_time distribution # 1us # 10us # 100us # 1ms # 10ms ################################################################ # 100ms # 1s # 10s+ show variables\G

4.使用mysqldumpslow

        

-v verbose ###详细模式 
-d debug ##调试模式 
-s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default ##排序 
	al: average lock time ar: average rows sent at: average query time ###平均
ar:平均
at:平均 c: count l: lock time r: rows sent t: query time ##按数量,获取锁的时间,发送给客户端的字节大小,执行时间 -r reverse the sort order (largest last instead of first) ##颠倒顺序 -t NUM just show the top n queries ##只显示开口的NUM条 -a don't abstract all numbers to N and strings to 'S' ##不把数字显示成N -n NUM abstract numbers with at least n digits within names -g PATTERN grep: only consider stmts that include this string ##匹配模式查询 -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard), default is '*', i.e. match all -i NAME name of server instance (if using mysql.server startup script) ##服务器实例名称 -l don't subtract lock time from total time ##不从总时间减去锁时间

 5.bin-log

  • show global status;

binlog_cache_use    ----使用缓冲区存储binlog event(或者文件)的事务数量

binlog_cache_disk_use   ----不得不使用临时文件存储binlog event的事务数量

binlog_stmt_cache_use   ----使用缓冲区存储事务中语句的数量

binlog_stmt_cache_disk_use  --------不得不使用临时文件存储事务中语句的数量

  • show variables

binlog_checksum     ---binlog日志记录事件的长度和事件本身,开启该参数可以用它们来验证事件是否正确写入(可动态修改)

master_verify_checksum    ----校验binary log 的读取事件,禁用时主服务器使用binary log中的事件长度来验证事件(可动态修改)

binlog_cache-size    ----分配一段binlog_cache_size大小的缓冲区存储事务中的语句,如果事务中的语句大于这个缓冲区会打开一个临时文件存放多出来的内容,事务结束会删除该文件(可动态修改)

max_binlog_cache_size   ----限制多语句事务的大小(可动态修改)

max_binlog_stmt_cache_size    ----分配给事务每条语句的缓冲区大小的总和(可动态修改)

binlog_stmt_cache_size    -----分配给事务每条语句的缓冲区大小(可动态修改)

binlog_error_action    --------当服务器无法写入binary log时做的操作 binlog_error_action=ABORT_SERVER 让服务器停止binary log日志的记录并关闭(可动态修改)

============sync_binlog(可动态修改)==============

        sync_binlog=0    -----禁止mysql出做fsync()把binary log同步到磁盘的指令,表示服务器像处理其他文件一样依靠操作系统不时地把binary log刷新到磁盘

        sync_binlog=1     ----在每个事务提交之前把binary_log刷新到磁盘

        sync_binlog=N    ----事务组收到多少个事务后时把binary log刷新到磁盘

binlog_direct_non_transactional_updates   ----------对非事务的表直接写binlog(可动态修改)

binlog_group_commit_sync_delay    ----控制二进制日志提交后刷新到磁盘之前等待多少微妙(可动态修改)

binlog_group_commit_sync_no_delay_count      ----在binlog_group_commit_sync_delay之间等待的最大事务数(binlog_group_commit_sync_delay=0时无效)(可动态修改)

binlog_order_commits         ----------发给存储引擎的事务提交指令会在一个线程上序列化。因此事务始终以写入binary log日志相同的顺序提交,当禁用时会导致多线程提交,会导致事务顺序不一致(可动态修改)

binlog_row_image       -----确定如何将行写入binary log(full所有列,minimal仅记录更新的列以及标识的列,noblob记录所有列,但不需要的BLOB和TEXT列除外)(可动态修改)

  • 配置文件

 

log-bin       ---值为binary log的路径,例如log-bin=mysql-bin(扩展名会被忽略掉例如mysql-bin.1其中.1会被忽略),每次服务器创建日志文件时,该扩展名会自增1,从而创建有序的binary log文件

 

log-bin.index    ---值为binary log 索引文件的路径

binlog-do-db     ---binary log记录哪些db的更改,在bin-log-format=row时 

binlog-ignore-db      ---binary log 不记录哪些db的更改,在bin-log-format=row时 

log-slave-update      ---复制的从服务器不会在binary log记录那些从主库接收到的更改信息,除非使用log-slave-update才会记录 

binlog-row-event-max-size   ---基于行的binary log事件的最大大小 

sporadic-binlog-dump-fail    ---Mysql复制测试和调试时使用的参数 

max-binlog-dump-fail        ---Mysql复制测试和调试时使用的参数 

log_statement_unsafe_for_binlog     ---如果遇到1592错误(Unsafe statement written to the binary log using statement format),控制是否将警告加到error log中

 5. relay log文件的大小问题

配置文件参数 和variables中的 relay_log_purge: 控制是否清理relay log,如果开启mha建议关闭该参数,mha基于realy log进行切换

relay log 单个文件的大小限制由参数中的 和variables中的 max_relay_log_size 决定,如果该参数没有设置,由max_binlog_size 决定,操作系统有限制的话,必须低于操作系统限制

relay log 和binlog 文件的总大小: 因为binlog 和relay log文件的后缀只有6位数 最大 999999 约等于 100W个文件,在磁盘空间不受限制的时候总大小即是100W*max_relay_log_size 

推荐阅读