常用日志参数

  1. 经常用到的有错误、快慢查询、二进制等日志

错误日志

  1. 1 作用
  2. 记录启动\关\日常运行过程中,状态信息,警告,错误,排查MySQL运行过程的故障
  3. 2 错误日志配置
  4. 默认就是开启的: /数据路径下/hostname.err
  5. 自定义日志路径和名字:
  6. mysql> select @@log_error;
  7. vim /etc/my.cnf
  8. log_error=/var/log/mysql.log
  9. log_timestamps=system
  10. 重启生效
  11. show variables like 'log_error';
  12. 3 日志内容查看
  13. 主要关注[ERROR],看上下文

二进制日志

作用

  1. (1)备份恢复必须依赖二进制日志
  2. (2)主从环境必须依赖二进制日志

binlog配置(5.7必须加上server_id)

  1. 注意:MySQL默认是没有开启二进制日志的
  2. 基础参数查看:
  3. mysql> select @@log_bin;
  4. 日志路径及名字
  5. mysql> select @@log_bin_basename;
  6. 服务ID号:
  7. mysql> select @@server_id;
  8. 二进制日志格式:
  9. mysql> select @@binlog_format;
  10. 双一标准之二:
  11. mysql> select @@sync_binlog;
  12. 1 创建日志目录
  13. mkdir /data/binlog
  14. chown -R mysql.mysql /data/binlog
  15. 2 修改配置文件
  16. vim /etc/my.cnf
  17. server_id=6 # 5.6中,单机可以不需要此参数
  18. log_bin=/data/binlog/mysql-bin # 该目录需要有mysql.mysql权限
  19. binlog_format=row # 5.7的默认配置就是row,可以省略不写
  20. sync_binlog=1 # 每次事务提交都立即刷写binlog到磁盘
  21. 3 重启数据库生效
  22. [root@db01 mysql]# /etc/init.d/mysqld restart
  23. 4 参数说明
  24. server_id=6
  25. 主要是在主从复制过程中必须要加的,但是在5.7版本中,要用以下参数(log_bin),开启binlog日志,即使是单机也是必加的
  26. log_bin=/data/binlog/mysql-bin
  27. (1)开启二进制日志功能
  28. (2)设置二进制日志目录及名称前缀
  29. binlog_format=row

binlog记录了什么

  1. 引入
  2. binlogSQL层的功能。记录的是变更SQL语句,不记录查询语句。
  3. 1 记录SQL语句种类
  4. DDL :原封不动的记录当前DDL(statement语句方式)。
  5. DCL :原封不动的记录当前DCL(statement语句方式)。
  6. DML :只记录已经提交的事务DML
  7. 2 DML三种记录方式
  8. binlog_formatbinlog的记录格式)参数影响
  9. 1statement5.6默认)SBR(statement based replication):语句模式原封不动的记录当前DML
  10. 2row 5.7默认)RBR(ROW based replication):记录数据行的变化(用户看不懂,需要工具分析)
  11. 3mixed (混合)MBR(mixed based replication)模式:以上两种模式的混合
  12. 3
  13. SBRRBR模式的对比
  14. statement:可读性较高,日志量少,但是不够严谨,有可能出现记录不准确的情况
  15. row :可读性很低,日志量大,足够严谨
  16. update t1 set xxx=xxx where id>1000 -->一共500w行,row模式怎么记录的日志,为什么row模式严谨?
  17. id name intime
  18. insert into t1 values(1,'zs',now())
  19. 我们建议使用:row记录模式

event(事件)是什么

  1. 1 事件的简介
  2. event是二进制日志的最小记录单元
  3. 对于DDL,DCL,一个语句就是一个event
  4. 对于DML语句来讲:只记录已提交的事务
  5. 例如以下列子,就被分为了4event
  6. begin; 120 - 340
  7. DML1 340 - 460
  8. DML2 460 - 550
  9. commit; 550 - 760
  10. 2 event的组成
  11. 三部分构成:
  12. (1) 事件的开始标识
  13. (2) 事件内容
  14. (3) 事件的结束标识
  15. Position:
  16. 开始标识: at 194
  17. 结束标识: end_log_pos 254
  18. 194? 254?
  19. 某个事件在binlog中的相对位置号
  20. 位置号的作用是什么?
  21. 为了方便我们截取事件

日志文件查看

  1. 1 查看日志的开启情况
  2. log_bin参数设置的路径,可以找到二进制日志
  3. Master [(none)]>show variables like '%log_bin%';
  4. +---------------------------------+------------------------------+
  5. | Variable_name | Value |
  6. +---------------------------------+------------------------------+
  7. | log_bin | ON |
  8. | log_bin_basename | /data/binlog/mysql-bin |
  9. | log_bin_index | /data/binlog/mysql-bin.index |
  10. | log_bin_trust_function_creators | OFF |
  11. | log_bin_use_v1_row_events | OFF |
  12. | sql_log_bin | ON |
  13. +---------------------------------+------------------------------+
  14. 6 rows in set (0.01 sec)
  15. 2 查看一共多少个binlog
  16. Master [(none)]>show binary logs;
  17. +------------------+-----------+
  18. | Log_name | File_size |
  19. +------------------+-----------+
  20. | mysql-bin.000001 | 154 |
  21. +------------------+-----------+
  22. 1 row in set (0.01 sec)
  23. Master [(none)]>flush logs;
  24. Query OK, 0 rows affected (0.03 sec)
  25. Master [(none)]>flush logs;
  26. Query OK, 0 rows affected (0.01 sec)
  27. Master [(none)]>show binary logs;
  28. +------------------+-----------+
  29. | Log_name | File_size |
  30. +------------------+-----------+
  31. | mysql-bin.000001 | 201 |
  32. | mysql-bin.000002 | 201 |
  33. | mysql-bin.000003 | 154 |
  34. +------------------+-----------+
  35. 3 rows in set (0.00 sec)
  36. 3 查看mysql正在使用的日志文件
  37. Master [(none)]>show master status;
  38. +------------------+----------+--------------+------------------+-------------------+
  39. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  40. +------------------+----------+--------------+------------------+-------------------+
  41. | mysql-bin.000003 | 154 | | | |
  42. +------------------+----------+--------------+------------------+-------------------+
  43. Master [(none)]>
  44. file:当前MySQL正在使用的文件名
  45. Position:最后一个事件的结束位置号

日志内容查看

  1. 1 event查看
  2. Master [binlog]>show binlog events in 'mysql-bin.000003';
  3. +------------------+-----+----------------+-----------+-------------+----------------------------------------+
  4. | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
  5. +------------------+-----+----------------+-----------+-------------+----------------------------------------+
  6. | mysql-bin.000003 | 4 | Format_desc | 6 | 123 | Server ver: 5.7.20-log, Binlog ver: 4 |
  7. | mysql-bin.000003 | 123 | Previous_gtids | 6 | 154 | |
  8. | mysql-bin.000003 | 154 | Anonymous_Gtid | 6 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
  9. | mysql-bin.000003 | 219 | Query | 6 | 319 | create database binlog |
  10. | mysql-bin.000003 | 319 | Anonymous_Gtid | 6 | 384 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
  11. | mysql-bin.000003 | 384 | Query | 6 | 486 | use `binlog`; create table t1 (id int) |
  12. +------------------+-----+----------------+-----------+-------------+----------------------------------------+
  13. Log_namebinlog文件名
  14. Pos:开始的position *****
  15. Event_type:事件类型
  16. Format_desc:格式描述,每一个日志文件的第一个事件,多用户没有意义,MySQL识别binlog必要信息
  17. Server_idmysql服务号标识
  18. End_log_pos:事件的结束位置号 *****
  19. Info:事件内容*****
  20. 补充:
  21. SHOW BINLOG EVENTS
  22. [IN 'log_name']
  23. [FROM pos]
  24. [LIMIT [offset,] row_count]
  25. [root@db01 binlog]# mysql -e "show binlog events in 'mysql-bin.000004'" |grep drop
  26. 2 binlog文件内容详细查看
  27. mysqlbinlog /data/mysql/mysql-bin.000006
  28. mysqlbinlog --base64-output=decode-rows -vvv /data/binlog/mysql-bin.000003
  29. mysqlbinlog -d binlog /data/binlog/mysql-bin.000003
  30. [root@db01 binlog]# mysqlbinlog --start-datetime='2019-05-06 17:00:00' --stop-datetime='2019-05-06 17:01:00' /data/binlog/mysql-bin.000004

基于Position号进行日志截取

  1. 核心就是找截取的起点和终点
  2. --start-position=321
  3. --stop-position=513
  4. mysqlbinlog --start-position=219 --stop-position=1347 /data/binlog/mysql-bin.000003 >/tmp/bin.sql
  5. 案例: 使用binlog日志进行数据恢复
  6. 模拟:
  7. 1.
  8. [(none)]>create database binlog charset utf8;
  9. 2.
  10. [(none)]>use binlog;
  11. [binlog]>create table t1(id int);
  12. 3.
  13. [binlog]>insert into t1 values(1);
  14. [binlog]>commit;
  15. [binlog]>insert into t1 values(2);
  16. [binlog]>commit;
  17. [binlog]>insert into t1 values(3);
  18. [binlog]>commit;
  19. 4.
  20. [binlog]>drop database binlog;
  21. 恢复:
  22. [(none)]>show master status ;
  23. [(none)]>show binlog events in 'mysql-bin.000004';
  24. [root@db01 binlog]# mysqlbinlog --start-position=1227 --stop-position=2342 /data/binlog/mysql-bin.000004 >/tmp/bin.sql
  25. [(none)]>set sql_Log_bin=0; # 在此会话中,临时将二进制日志关闭,恢复时不会产生新的日志
  26. [(none)]>source /tmp/bin.sql
  27. [(none)]>set sql_Log_bin=1; # 恢复完成把参数改回去
  28. 案例:
  29. 1. 备份策略每天全备,有全量的二进制日志
  30. 2. 业务中一共10个库,其中一个被误drop
  31. 3. 需要在其他9个库正常工作过程中进行数据恢复
  32. 假如被删除的库为A
  33. 1.恢复昨天A的全备
  34. 2.找到A库到今天drop执行之前的所有二进制日志
  35. 3.mysqlbinlog工具导出二进制日志为sql
  36. 4.执行第3部导入的sql,恢复今天的数据
  37. 思考
  38. 实验:两个库中的表交叉执行插入语句,然后删除其中一个库,要怎么恢复?
  39. mysqlbinlog -d 指定被删除的库 --start-position=1227 --stop-position=2342 /data/binlog/mysql-bin.000004 >/tmp/bin.sql
  40. 另外,如果有多个二进制日志怎么恢复?
  41. 一个一个的binlog日志文件导入为sql,然后恢复到数据库

binlog日志的GTID新特性

  1. 1 GTID 介绍
  2. 5.6版本新加的特性,5.7中做了加强
  3. 5.6中不开启,没有这个功能.
  4. 5.7中的GTID,即使不开也会有自动生成
  5. SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
  6. 2. GTID(Global Transaction ID)
  7. 是对于一个已提交事务的编号,并且是一个全局唯一的编号。
  8. 它的官方定义如下:
  9. GTID = source_idtransaction_id
  10. [root@db01 data]# pwd
  11. /data/mysql/data
  12. [root@db01 data]# ls
  13. auto.cnf db01.pid ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 ibtmp1 mysql performance_schema school sys test world zabbix
  14. [root@db01 data]# cat auto.cnf
  15. [auto]
  16. server-uuid=a4b648cb-11b8-11ea-8636-000c2990cef0
  17. 7E11FA47-31CA-19E1-9E56-C43AA21293967:29
  18. 重要参数介绍:
  19. vim /etc/my.cnf
  20. gtid-mode=on # 开启gtid模式
  21. enforce-gtid-consistency=true # 强调gtid一致性
  22. # systemctl restart mysqld # 重启服务
  23. Master [(none)]>create database gtid charset utf8;
  24. Query OK, 1 row affected (0.01 sec)
  25. Master [(none)]>show master status ;
  26. +------------------+----------+--------------+------------------+----------------------------------------+
  27. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  28. +------------------+----------+--------------+------------------+----------------------------------------+
  29. | mysql-bin.000004 | 326 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1 |
  30. +------------------+----------+--------------+------------------+----------------------------------------+
  31. 1 row in set (0.00 sec)
  32. Master [(none)]>use gtid
  33. Database changed
  34. Master [gtid]>create table t1 (id int);
  35. Query OK, 0 rows affected (0.01 sec)
  36. Master [gtid]>show master status ;
  37. +------------------+----------+--------------+------------------+------------------------------------------+
  38. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  39. +------------------+----------+--------------+------------------+------------------------------------------+
  40. | mysql-bin.000004 | 489 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-2 |
  41. +------------------+----------+--------------+------------------+------------------------------------------+
  42. 1 row in set (0.00 sec)
  43. Master [gtid]>create table t2 (id int);
  44. Query OK, 0 rows affected (0.01 sec)
  45. Master [gtid]>create table t3 (id int);
  46. Query OK, 0 rows affected (0.02 sec)
  47. Master [gtid]>show master status ;
  48. +------------------+----------+--------------+------------------+------------------------------------------+
  49. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  50. +------------------+----------+--------------+------------------+------------------------------------------+
  51. | mysql-bin.000004 | 815 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-4 |
  52. +------------------+----------+--------------+------------------+------------------------------------------+
  53. 1 row in set (0.00 sec)
  54. Master [gtid]>begin;
  55. Query OK, 0 rows affected (0.00 sec)
  56. Master [gtid]>insert into t1 values(1);
  57. Query OK, 1 row affected (0.00 sec)
  58. Master [gtid]>commit;
  59. Query OK, 0 rows affected (0.00 sec)
  60. Master [gtid]>show master status ;
  61. +------------------+----------+--------------+------------------+------------------------------------------+
  62. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  63. +------------------+----------+--------------+------------------+------------------------------------------+
  64. | mysql-bin.000004 | 1068 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-5 |
  65. +------------------+----------+--------------+------------------+------------------------------------------+
  66. 1 row in set (0.00 sec)
  67. Master [gtid]>begin;
  68. Query OK, 0 rows affected (0.00 sec)
  69. Master [gtid]>insert into t2 values(1);
  70. Query OK, 1 row affected (0.00 sec)
  71. Master [gtid]>commit;
  72. Query OK, 0 rows affected (0.01 sec)
  73. Master [gtid]>show master status ;
  74. +------------------+----------+--------------+------------------+------------------------------------------+
  75. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  76. +------------------+----------+--------------+------------------+------------------------------------------+
  77. | mysql-bin.000004 | 1321 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-6 |
  78. +------------------+----------+--------------+------------------+------------------------------------------+
  79. 1 row in set (0.00 sec)
  80. 3. 基于GTID进行查看binlog
  81. 具备GTID后,截取查看某些事务日志:
  82. --include-gtids
  83. --exclude-gtids
  84. mysqlbinlog --include-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:1-6' --exclude-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:4' /data/binlog/mysql-bin.000004
  85. 4 GTID的幂等性
  86. 开启GTID后,MySQL恢复Binlog时,重复GTID的事务不会再执行了
  87. 就想恢复?怎么办? --skip-gtids
  88. # mysqlbinlog --include-gtids='3ca79ab5-3e4d-11e9-a709-000c293b577e:4' /data/binlog/mysql-bin.000004 >/tmp/binlog.sql
  89. set sql_log_bin=0;
  90. source /tmp/binlog.sql
  91. set sql_log_bin=1;

使用二进制日志恢复数据案例

  1. 1 故障环境介绍
  2. 创建了一个库db,导入了表t1,t1表中录入了很多数据
  3. 一个开发人员,drop database db;
  4. 没有备份,日志都在,怎么恢复?
  5. 思路:找到建库语句到删库之前所有的日志,进行恢复(开启了GTID模式)
  6. 故障案例模拟:
  7. drop database if exists db;
  8. create database db charset utf8;
  9. use db;
  10. create table t1 (id int);
  11. insert into t1 values(1),(2),(3);
  12. insert into t1 values(4),(5),(6);
  13. commit;
  14. update t1 set id=30 where id=3;
  15. commit;
  16. delete from t1 where id=4;
  17. commit;
  18. insert into t1 values(7),(8),(9);
  19. commit;
  20. drop database db;
  21. 运行以上语句,模拟故障场景
  22. 需求:将数据库恢复到以下状态(提示第13drop是误操作,其他都是正常操作)
  23. 2 GTID的恢复
  24. 2.1查看当前使用的binlog文件
  25. mysql> show master status;
  26. +------------------+----------+--------------+------------------+-------------------------------------------+
  27. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  28. +------------------+----------+--------------+------------------+-------------------------------------------+
  29. | mysql-bin.000012 | 1941 | | | a4b648cb-11b8-11ea-8636-000c2990cef0:1-14 |
  30. +------------------+----------+--------------+------------------+-------------------------------------------+
  31. 1 row in set (0.00 sec)
  32. 2.2查看事件:
  33. mysql> show binlog events in 'mysql-bin.000012';
  34. +------------------+------+----------------+-----------+-------------+-------------------------------------------------------------+
  35. | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
  36. +------------------+------+----------------+-----------+-------------+-------------------------------------------------------------+
  37. | mysql-bin.000012 | 4 | Format_desc | 6 | 123 | Server ver: 5.7.26-log, Binlog ver: 4
  38. | mysql-bin.000012 | 123 | Previous_gtids | 6 | 194 | a4b648cb-11b8-11ea-8636-000c2990cef0:1-6
  39. | mysql-bin.000012 | 194 | Gtid | 6 | 259 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:7' |
  40. | mysql-bin.000012 | 259 | Query | 6 | 350 | drop database if exists db |
  41. | mysql-bin.000012 | 350 | Gtid | 6 | 415 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:8' |
  42. | mysql-bin.000012 | 415 | Query | 6 | 516 | create database db charset utf8 |
  43. | mysql-bin.000012 | 516 | Gtid | 6 | 581 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:9' |
  44. | mysql-bin.000012 | 581 | Query | 6 | 675 | use `db`; create table t1 (id int) |
  45. | mysql-bin.000012 | 675 | Gtid | 6 | 740 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:10' |
  46. | mysql-bin.000012 | 740 | Query | 6 | 810 | BEGIN |
  47. | mysql-bin.000012 | 810 | Table_map | 6 | 853 | table_id: 110 (db.t1) |
  48. | mysql-bin.000012 | 853 | Write_rows | 6 | 903 | table_id: 110 flags: STMT_END_F |
  49. | mysql-bin.000012 | 903 | Table_map | 6 | 946 | table_id: 110 (db.t1) |
  50. | mysql-bin.000012 | 946 | Write_rows | 6 | 996 | table_id: 110 flags: STMT_END_F |
  51. | mysql-bin.000012 | 996 | Xid | 6 | 1027 | COMMIT /* xid=42 */ |
  52. | mysql-bin.000012 | 1027 | Gtid | 6 | 1092 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:11' |
  53. | mysql-bin.000012 | 1092 | Query | 6 | 1162 | BEGIN |
  54. | mysql-bin.000012 | 1162 | Table_map | 6 | 1205 | table_id: 110 (db.t1) |
  55. | mysql-bin.000012 | 1205 | Update_rows | 6 | 1251 | table_id: 110 flags: STMT_END_F |
  56. | mysql-bin.000012 | 1251 | Xid | 6 | 1282 | COMMIT /* xid=45 */ |
  57. | mysql-bin.000012 | 1282 | Gtid | 6 | 1347 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:12' |
  58. | mysql-bin.000012 | 1347 | Query | 6 | 1417 | BEGIN |
  59. | mysql-bin.000012 | 1417 | Table_map | 6 | 1460 | table_id: 110 (db.t1) |
  60. | mysql-bin.000012 | 1460 | Delete_rows | 6 | 1500 | table_id: 110 flags: STMT_END_F |
  61. | mysql-bin.000012 | 1500 | Xid | 6 | 1531 | COMMIT /* xid=47 */ |
  62. | mysql-bin.000012 | 1531 | Gtid | 6 | 1596 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:13' |
  63. | mysql-bin.000012 | 1596 | Query | 6 | 1666 | BEGIN |
  64. | mysql-bin.000012 | 1666 | Table_map | 6 | 1709 | table_id: 110 (db.t1) |
  65. | mysql-bin.000012 | 1709 | Write_rows | 6 | 1759 | table_id: 110 flags: STMT_END_F |
  66. | mysql-bin.000012 | 1759 | Xid | 6 | 1790 | COMMIT /* xid=49 */ |
  67. | mysql-bin.000012 | 1790 | Gtid | 6 | 1855 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:14' |
  68. | mysql-bin.000012 | 1855 | Query | 6 | 1941 | drop database db
  69. +------------------+------+----------------+-----------+-------------+-----------------------------------------------------------+
  70. 3.截取
  71. [root@db01 binlog]# mysqlbinlog --skip-gtids --include-gtids='a4b648cb-11b8-11ea-8636-000c2990cef0:7:7-13' mysql-bin.000012 >/tmp/bin.sql
  72. 参数说明:
  73. --skip-gtids 跳过gtid检测
  74. --include-gtids:包含的gtid
  75. --exclude-gtids:不包含的gtid --exclude-gtids='3ca79ab5-3e4d-11e9-a709-000c293b577e:8','3ca79ab5-3e4d-11e9-a709-000c293b577e:10'
  76. 4.恢复
  77. mysql> set sql_log_bin=0;
  78. mysql> source /tmp/bin.sql;
  79. mysql> set sql_log_bin=0;
  80. 5.验证数据
  81. mysql> select * from t1;
  82. +------+
  83. | id |
  84. +------+
  85. | 1 |
  86. | 2 |
  87. | 30 |
  88. | 5 |
  89. | 6 |
  90. | 7 |
  91. | 8 |
  92. | 9 |
  93. +------+
  94. 8 rows in set (0.00 sec)

二进制日志其他操作

  1. 1 自动清理日志
  2. show variables like '%expire%';
  3. expire_logs_days 0
  4. 自动清理时间,是要按照全备周期+1
  5. set global expire_logs_days=8;
  6. 永久生效:
  7. my.cnf
  8. expire_logs_days=15;
  9. 企业建议,至少保留两个全备周期+1binlog
  10. 2 手工清理
  11. mysql> show binary logs;
  12. +------------------+-----------+
  13. | Log_name | File_size |
  14. +------------------+-----------+
  15. | mysql-bin.000001 | 1052 |
  16. | mysql-bin.000002 | 201 |
  17. | mysql-bin.000003 | 1041 |
  18. | mysql-bin.000004 | 969 |
  19. | mysql-bin.000005 | 969 |
  20. | mysql-bin.000006 | 3149 |
  21. | mysql-bin.000007 | 201 |
  22. | mysql-bin.000008 | 2604 |
  23. | mysql-bin.000009 | 1072 |
  24. | mysql-bin.000010 | 602 |
  25. | mysql-bin.000011 | 1368 |
  26. | mysql-bin.000012 | 1941 |
  27. +------------------+-----------+
  28. 12 rows in set (0.01 sec)
  29. mysql> PURGE BINARY LOGS TO 'mysql-bin.000010';
  30. Query OK, 0 rows affected (0.00 sec)
  31. mysql> show binary logs;
  32. +------------------+-----------+
  33. | Log_name | File_size |
  34. +------------------+-----------+
  35. | mysql-bin.000010 | 602 |
  36. | mysql-bin.000011 | 1368 |
  37. | mysql-bin.000012 | 1941 |
  38. +------------------+-----------+
  39. 3 rows in set (0.00 sec)
  40. mysql> flush logs;
  41. Query OK, 0 rows affected (0.01 sec)
  42. mysql> show binary logs;
  43. +------------------+-----------+
  44. | Log_name | File_size |
  45. +------------------+-----------+
  46. | mysql-bin.000010 | 602 |
  47. | mysql-bin.000011 | 1368 |
  48. | mysql-bin.000012 | 1988 |
  49. | mysql-bin.000013 | 194 |
  50. +------------------+-----------+
  51. 4 rows in set (0.00 sec)
  52. 注意:不要手工rm binlog文件
  53. 1. my.cnf binlog关闭掉,启动数据库
  54. 2.把数据库关闭,开启binlog,启动数据库
  55. 删除所有binlog,并从000001开始重新记录日志
  56. reset master; 主从关系中,主库执行此操作,主从环境必崩
  57. 3 日志是怎么滚动
  58. flush logs;
  59. 重启mysql也会自动滚动一个新的
  60. 日志文件达到1G大小(max_binlog_size)
  61. max_binlog_size | 1073741824
  62. 备份时,加入参数也可以自动滚动

慢日志

  1. 1 作用
  2. 记录慢SQL语句的日志,定位低效SQL语句的工具日志
  3. 2 开启慢日志(默认没开启)
  4. 开关:
  5. slow_query_log=1
  6. 文件位置及名字
  7. slow_query_log_file=/data/mysql/slow.log
  8. 设定慢查询时间:
  9. long_query_time=0.1 # 根据业务的需求更改,默认是10s
  10. 没走索引的语句也记录:
  11. log_queries_not_using_indexes
  12. vim /etc/my.cnf
  13. slow_query_log=1
  14. slow_query_log_file=/data/mysql/slow.log
  15. long_query_time=0.1
  16. log_queries_not_using_indexes
  17. systemctl restart mysqld
  18. 3 mysqldumpslow 分析慢日志
  19. mysqldumpslow -s c -t 10 /data/mysql/slow.log
  20. # 第三方工具 这个工具很好用
  21. https://www.percona.com/downloads/percona-toolkit/LATEST/
  22. yum install perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL perl-Digest-MD5
  23. toolkit工具包中的命令:
  24. pt-query-diagest /data/mysql/slow.log
  25. Anemometer基于pt-query-digestMySQL慢查询可视化
  26. 优化案例:
  27. SELECT SUM(invalid_count) as invalid_count, SUM(confirm_count) as confirm_count,SUM(not_confirm_count) as not_confirm_count,SUM(total_count) as total_count
  28. FROM (
  29. select count(att.LogID) as invalid_count, 0 as confirm_count,0 as not_confirm_count,0 as total_count,bp.city_id
  30. FROM builder_project_info as bp
  31. JOIN attendentlog as att ON bp.project_id = att.CompanyID
  32. WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and att.LogTime like "2019-06%" and att.state = 2
  33. GROUP BY bp.city_id
  34. UNION all (
  35. select 0 as invalid_count, count(att.LogID) as confirm_count,0 as not_confirm_count,0 as total_count,bp.city_id
  36. FROM builder_project_info as bp
  37. JOIN attendentlog as att ON bp.project_id = att.CompanyID
  38. WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and att.LogTime like "2019-06%" and att.state = 1
  39. GROUP BY bp.city_id
  40. )
  41. UNION all (
  42. select 0 as invalid_count, 0 as confirm_count,count(att.LogID) as not_confirm_count,0 as total_count,bp.city_id
  43. FROM builder_project_info as bp
  44. JOIN attendentlog as att ON bp.project_id = att.CompanyID
  45. WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and att.LogTime like "2019-06%" and att.state = 0
  46. GROUP BY bp.city_id
  47. )
  48. UNION all (
  49. select 0 as invalid_count, 0 as confirm_count,0 as not_confirm_count,count(att.LogID) as total_count,bp.city_id
  50. FROM builder_project_info as bp
  51. JOIN attendentlog as att ON bp.project_id = att.CompanyID
  52. WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and att.LogTime like "2019-06%"
  53. GROUP BY bp.city_id
  54. )
  55. ) as a
  56. GROUP BY a.city_id
  57. desc city
  58. desc country
  59. show index from city

MySQL-13-日志管理的更多相关文章

  1. MySQl Study学习之--MySQl二进制日志管理

    MySQl Study学习之--MySQl二进制日志管理 MySQL二进制日志(Binary Log)   a.它包括的内容及作用例如以下:     包括了全部更新了数据或者已经潜在更新了数据(比方没 ...

  2. MySQL InnoDB 日志管理机制中的MTR和日志刷盘

    1.MTR(mini-transaction) 在MySQL的 InnoDB日志管理机制中,有一个很重要的概念就是MTR.MTR是InnoDB存储擎中一个很重要的用来保证物理写的完整性和持久性的机制. ...

  3. Go语言学习之13 日志管理平台开发

    主要内容: 1. ElasticSearch介绍与使用2. kibana介绍与使用 1. ElasticSearch安装 详见上节内容2. kibana安装 (1) 下载ES,下载地址:https:/ ...

  4. mysql的日志管理

    日志操作是数据库维护中最重要的手段之一,日志文件会记录MySQL服务器的各种信息,所以当MySQL服务器遭到意外损坏时,不仅可以通过日志文件来查看出错的原因,而且还可以通过日志文件进行数据恢复. MY ...

  5. mysql数据库-日志管理

    MySQL 支持丰富的日志类型 事务日志:transaction log 事务日志的写入类型为"追加",因此其操作为"顺序IO":通常也被称为:预写式日志 wr ...

  6. mysql慢日志管理

    一.日志切割 原理: 1.cp一个慢日志备份 2.清空原理的慢日志 3.写成脚本,每天一切,这样就ok啦 二.查找日志中的慢日志 1.做了日志切割(慢日志文件就小了) 2.查找某个时间的慢日志 日志时 ...

  7. 28.MysQL的日志管理及备份与恢复

    MySQL 索引.事务与存储引擎 目录 MySQL 索引.事务与存储引擎 MySQL 索引 索引的概念 索引的作用及副作用 索引的作用 索引的副作用 创建索引的原则依据 索引的分类和创建 普通索引 唯 ...

  8. MySQL日志管理

    MySQL日志管理 2013年09月26日 ⁄ MySQL ⁄ 共 14266字 ⁄ 评论数 ⁄ 被围观 , views+ 一.日志类型: MySQL有几个不同的日志文件,可以帮助你找出mysqld内 ...

  9. MySQL/MariaDB数据库的各种日志管理

    MySQL/MariaDB数据库的各种日志管理 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.事务日志 (transaction log) 1>.Innodb事务日志相 ...

  10. MySQL之表日志管理

    MySQL日志管理 mysql日志(默认存放在datadir): 同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志, ...

随机推荐

  1. CentOS-yum安装chrome+chromeDriver+xvfb

    安装chrome 创建yum源文件 $ vim /etc/yum.repos.d/google-chrome.repo [google-chrome] name=google-chrome baseu ...

  2. javax.naming.NoInitialContextException:Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

    小弟初次用JNDI,使用数据源连接数据库,配置完相关的xml文件后,激动的我赶紧测试了一下,结果悲剧了,报出了错误: javax.naming.NoInitialContextException:Ne ...

  3. ctf实验吧天网管理系统

    这明显不可能登上的,所以直接看源代码 这里如果不懂得,php中处理哈希值的方式,是只要是0e开头的哈希值,都认为是0,通过输入一个这样的0e开头的字符串,会被php解释成0. 这个可以去在线工具上得到 ...

  4. 二进制方式搭建Kubernetes集群

    环境准备: 演练暂时用单节点一台master和一台node节点来进行部署搭建(kubernetes 1.19版本) 角色 IP 组件 master 10.129.246.114 kube-apiser ...

  5. 章节1-Prometheus基础(1)

    目录 一.Prometheus安装部署 1. 简介 监控的目的 Prometheus的优势 2. Prometheus工作流程: 2.1 服务端 2.2 客户端 2.3 metrics主要数据类型 3 ...

  6. git rebase 和 git merger

    & git merge 在上图中,每一个绿框均代表一个commit.除了c1,每一个commit都有一条有向边指向它在当前branch当中的上一个commit. 图中的项目,在c2之后就开了另 ...

  7. Python - 基本数据类型_str 字符串

    前言 字符串是编程中最重要的数据类型,也是最常见的 字符串的表示方式 单引号 ' ' 双引号 " " 多引号 """ ""&quo ...

  8. ssh服务两句话

    ssh服务采用"非对称密钥系统":主要通过两把不一样的公钥和密钥来进行加密与解密的过程 公钥(Public Key):提供给远程主机进行数据加密 私钥(Private Key):远 ...

  9. python删除文件中某一行

    将文本中的 tasting123删除 with open("fileread.txt","r",encoding="utf-8") as f ...

  10. 『与善仁』Appium基础 — 4、常用ADB命令(一)

    目录 1.启动和关闭ADB服务 2.查看ADB版本 3.指定adb server的网络端口 4.查询已连接设备/模拟器 5.获取安卓系统版本 6.为命令指定目标设备 7.发送文件到手机 8.从手机拉取 ...