日志管理
1.错误日志
1.1作用
MySQL 启动及工作过程中,状态、报错、警告
1.2 怎么配置
xiong[(none)]>select @@log_error;
默认在 /data/3306/data/db01.err
可以在 /etc/my.cnf 中设置
重启生效
1.3 如何查看错误日志
关注[ERROR]的上下文
2.二进制日志
2.1 作用
数据恢复必备的日志
主从复制依赖的日志
2.2怎么配置
2.2.1 修改配置文件
server_id=6
log_bin=/data/binlog/mysql-bin
/data/binlog/ :存放路径
mysql-bin :文件名前缀
2.2.2 创建目录授权
[root@db01 /data/3306/data]#mkdir -p /data/binlog
[root@db01 /data/3306/data]#chown -R mysql.mysql /data/*
重启数据库
replication: 复制
2.3 日志记录了什么
2.3.1 引入
除了查询类的语句,都会记录
所有数据库变更类的语句
2.3.2 记录语句种类
DDL
DCL
DML
2.3.4 不同语句的记录格式说明
DDL,DCL:直接以语句(statement)方式记录
DML语句: insert , update , delete
xiong[(none)]>select @@binlog_format;
SBR :statement 做什么记录什么
RBR :row 记录数据行的变化。默认模式 不会出现误差*****
MBR :mixed 自动判断记录模式
面试题: 说明 SBR和RBR的区别?
SBR : statement 做什么记录什么,记录的就是SQL语句,
可读性较强,日志量相对较少,日志记录可能会有误差
RBR : row 记录数据行的变化。默认模式 可读性差,
日志量大,日志记录准确不会出现误差
2.3.5 binlog events (二进制日志事件)
1.简介
二进制日志内容以事件为最小记录单元。
对于DDL和DCL,一个DDL语句就是一个事件。
对于DML(标准的事务语句): 只记录已提交的事务的DML语句
begin ; 事件1
a 事件2
b 事件3
commit; 事件4
2.事件的构成(为了截取日志)
[root@db01 /data/binlog]#mysqlbinlog mysql-bin.000001
# at 219 事件开始的位置
end_log_pos 317 事件结束的位置
#190814 18:52:46 事件发生的时间
create database xiong 事件内容
2.3.6 二进制日志的基本查看
(1)查看二进制日志的配置信息
xiong[(none)]>show variables like '%log_bin%';
+---------------------------------+------------------------------+
| Variable_name | Value |
+---------------------------------+------------------------------+
| log_bin | ON |
| log_bin_basename | /data/binlog/mysql-bin |
| log_bin_index | /data/binlog/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+------------------------------+
6 rows in set (0.01 sec)
(2)二进制日志基本信息
xiong[(none)]>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 317 |
+------------------+-----------+
1 row in set (0.00 sec)
xiong[(none)]>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 317 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
(3)查看二进制日志事件信息
xiong[(none)]>show binlog events in 'mysql-bin.000001';
2.4 内容查看和截取
2.4.1 内容查看命令
[root@db01 ~]# mysqlbinlog /data/binlog/mysql-bin.000005
[root@db01 ~]# mysqlbinlog --base64-output=decode-rows -vvv /data/binlog/mysql-bin.000005
2.4.2 日志的截取
--start-position
--stop-position
mysqlbinlog --start-position=xxx
演练
xiong[(none)]>create database binlog charset utf8mb4;
xiong[(none)]>use binlog
xiong[binlog]>create table t1(id int)engine=innodb charset=utf8mb4;
xiong[binlog]>insert into t1 values(1),(2),(3);
xiong[binlog]>commit;
xiong[binlog]>insert into t1 values(11),(12),(13);
xiong[binlog]>commit;
xiong[binlog]>update t1 set id=10 where id>10;
xiong[binlog]>commit;
xiong[binlog]>select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 10 |
| 10 |
| 10 |
+------+
(2)删除
xiong[binlog]>drop database binlog;
(3)数据恢复
确认起点和终点
xiong[(none)]>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 1511 | | | |
+------------------+----------+--------------+------------------+-------------------+
xiong[(none)]>xiong[(none)]>show binlog events in 'mysql-bin.000003';
起点
| mysql-bin.000003 | 219 | Query | 6 | 335 | create database binlog charset utf8mb4
终点:
| mysql-bin.000003 | 1413 | Query | 6 | 1511 | drop database binlog
(4)截取日志
mysqlbinlog --start-position=219 --stop-position=1413 /data/binlog/mysql-bin.000003>/data/bin.sql
(5)恢复日志
oldguo[(none)]>set sql_log_bin=0; ## 临时关闭当前会话的binlog记录,不影响其他会话日志记录
oldguo[(binlog)]>source
2.5 基于gtid的binlog管理
2.5.0引入
5.6版本以后,binlog加入了新的日志记录方式,GTID
主要作用:
简化binlog截取
提供在主从复制中的高级功能
5.7版本之后,进行了GTID增强
主从性能,高可用环境,集群
2.5.1 什么是gtid
全局唯一的事务编号
幂等性
GTID:Server-uuid:Tx_id
[root@db01 /data/3306/data]#cat auto.cnf
[auto]
server-uuid=ce5f18bf-b827-11e9-872c-000c29be994e
2.5.2 配置
xiong[(none)]>show variables like '%gtid%';
gtid_mode=on # 开关
enforce_gtid_consistency=true # 强制GTID一致性
log_slave_updates=1 # 主从复制中从库记录binlog,并统一GTID信息
xiong[(none)]>show variables like '%gtid%';
+----------------------------------+----------------+
| Variable_name | Value |
+----------------------------------+----------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed_compression_period | 1000 |
| gtid_mode | OFF_PERMISSIVE |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+----------------+
8 rows in set (0.01 sec)
2.5.3 基于gtid日志信息
DDL , DCL 一个事务是一个ID
DML
2.5.4 基于gtid截取日志信息
截取1-2号事务
mysqlbinlog --include-gtids='ce5f18bf-b827-11e9-872c-000c29be994e:1-2' /data/binlog/mysql-bin.000007
截取 1-10 gtid事务,跳过6号事务.
mysqlbinlog --include-gtids='ce5f18bf-b827-11e9-872c-000c29be994e:1-10' /data/binlog/mysql-bin.000007 --exclude-gtids='ce5f18bf-b827-11e9-872c-000c29be994e:6'
2.5.5演练
xiong[(none)]>create database gtid charset utf8mb4;
xiong[(none)]>use gtid
xiong[gtid]>create table t1 (id int) engine=innodb charset=utf8mb4;
xiong[gtid]>insert into t1 values(1),(2),(3);
xiong[gtid]>commit;
xiong[gtid]>insert into t1 values(11),(22),(33);
xiong[gtid]>commit;
xiong[gtid]>select * from t1 ;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 11 |
| 22 |
| 33 |
+------+
(2)删除
xiong[gtid]>drop database gtid;
(3)查看gtid 并导出日志
xiong[(none)]>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000007 | 1553 | | | ce5f18bf-b827-11e9-872c-000c29be994e:1-7 |
+------------------+----------+--------------+------------------+------------------------------------------+
xiong[(none)]>show binlog events in 'mysql-bin.000007';
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| mysql-bin.000007 | 4 | Format_desc | 6 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| mysql-bin.000007 | 123 | Previous_gtids | 6 | 154 | |
| mysql-bin.000007 | 154 | Gtid | 6 | 219 | SET @@SESSION.GTID_NEXT= 'ce5f18bf-b827-11e9-872c-000c29be994e:1' |
| mysql-bin.000007 | 219 | Query | 6 | 326 | create database db1 charset utf8mb4 |
| mysql-bin.000007 | 326 | Gtid | 6 | 391 | SET @@SESSION.GTID_NEXT= 'ce5f18bf-b827-11e9-872c-000c29be994e:2' |
| mysql-bin.000007 | 391 | Query | 6 | 502 | use `db1`; create table t1(id int) charset utf8mb4 |
| mysql-bin.000007 | 502 | Gtid | 6 | 567 | SET @@SESSION.GTID_NEXT= 'ce5f18bf-b827-11e9-872c-000c29be994e:3' |
| mysql-bin.000007 | 567 | Query | 6 | 677 | create database gtid charset utf8mb4 |
| mysql-bin.000007 | 677 | Gtid | 6 | 742 | SET @@SESSION.GTID_NEXT= 'ce5f18bf-b827-11e9-872c-000c29be994e:4' |
| mysql-bin.000007 | 742 | Query | 6 | 870 | use `gtid`; create table t1 (id int) engine=innodb charset=utf8mb4 |
| mysql-bin.000007 | 870 | Gtid | 6 | 935 | SET @@SESSION.GTID_NEXT= 'ce5f18bf-b827-11e9-872c-000c29be994e:5' |
| mysql-bin.000007 | 935 | Query | 6 | 1007 | BEGIN |
| mysql-bin.000007 | 1007 | Table_map | 6 | 1052 | table_id: 108 (gtid.t1) |
| mysql-bin.000007 | 1052 | Write_rows | 6 | 1102 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000007 | 1102 | Xid | 6 | 1133 | COMMIT /* xid=27 */ |
| mysql-bin.000007 | 1133 | Gtid | 6 | 1198 | SET @@SESSION.GTID_NEXT= 'ce5f18bf-b827-11e9-872c-000c29be994e:6' |
| mysql-bin.000007 | 1198 | Query | 6 | 1270 | BEGIN |
| mysql-bin.000007 | 1270 | Table_map | 6 | 1315 | table_id: 108 (gtid.t1) |
| mysql-bin.000007 | 1315 | Write_rows | 6 | 1365 | table_id: 108 flags: STMT_END_F |
| mysql-bin.000007 | 1365 | Xid | 6 | 1396 | COMMIT /* xid=29 */ |
| mysql-bin.000007 | 1396 | Gtid | 6 | 1461 | SET @@SESSION.GTID_NEXT= 'ce5f18bf-b827-11e9-872c-000c29be994e:7' |
| mysql-bin.000007 | 1461 | Query | 6 | 1553 | drop database gtid |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
[root@db01 /etc]#mysqlbinlog --skip-gtids --include-gtids='ce5f18bf-b827-11e9-872c-000c29be994e:3-6' /data/binlog/mysql-bin.000007>/data/gtid.sql
(4)导入恢复
xiong[(none)]>set sql_log_bin=0;
xiong[(none)]>source /data/gtid.sql;
2.6 二进制日志其他操作
2.6.1 自动清理日志
show variables like '%expire%';
expire_logs_days 0
自动清理时间,是要按照全备周期+1
set global expire_logs_days=8;
永久生效:
my.cnf
expire_logs_days=15;
企业建议,至少保留两个全备周期+1的binlog
2.6.2 手工清理
PURGE BINARY LOGS BEFORE now() - INTERVAL 3 day;
PURGE BINARY LOGS TO 'mysql-bin.000009';
注意:不要手工 rm binlog文件
1. my.cnf binlog关闭掉,启动数据库
2.把数据库关闭,开启binlog,启动数据库
删除所有binlog,并从000001开始重新记录日志
*reset master; 主从关系中,主库执行此操作,主从环境必崩
2.6.3 binlog的滚动
oldguo[(none)]>flush logs;flush logs;
重启数据库
select @@max_binlog_size;
备份时,某些参数会触发.
3.慢日志
3.1 简介
记录运行较慢的语句,记录到slowlog中
功能是辅助优化的工具日志
应激性的慢 ----> show processlist;
一段时间慢 ----> slow 记录,统计
3.2 配置
oldguo[(none)]>show variables like '%slow%';
oldguo[(none)]>select @@long_query_time;
oldguo[(none)]>show variables like '%not_using_indexes%';
slow_query_log=1
slow_query_log_file=/data/3306/data/db01-slow.log
long_query_time=0.1
log_queries_not_using_indexes
配置完重启生效
3.3 慢语句模拟
[root@db01 /data/3306/data]#mysqldumpslow -s -c -t 5 /data/3306/data/db01-slow.log
3.5 自己扩展一下
pt-query-digest /data/3306/data/db01-slow.log
集成: pt-query-digest+Anemometer=WEB方式:(分析慢日志,二进制日志,错误日志...)