日志管理

日志管理

1.錯誤日志

1.1作用

MySQL 啟動及工作過程中,狀態(tài)、報錯、警告

1.2 怎么配置

xiong[(none)]>select @@log_error;

image.png

默認在 /data/3306/data/db01.err

可以在 /etc/my.cnf 中設置

image.png

重啟生效

1.3 如何查看錯誤日志

關注[ERROR]的上下文

2.二進制日志

2.1 作用

數(shù)據(jù)恢復必備的日志

主從復制依賴的日志

2.2怎么配置

2.2.1 修改配置文件

image.png
server_id=6
log_bin=/data/binlog/mysql-bin
/data/binlog/ :存放路徑
mysql-bin :文件名前綴

2.2.2 創(chuàng)建目錄授權

[root@db01 /data/3306/data]#mkdir -p /data/binlog
[root@db01 /data/3306/data]#chown -R mysql.mysql /data/*
重啟數(shù)據(jù)庫
image.png

replication: 復制

2.3 日志記錄了什么

2.3.1 引入

除了查詢類的語句,都會記錄

所有數(shù)據(jù)庫變更類的語句

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          記錄數(shù)據(jù)行的變化。默認模式 不會出現(xiàn)誤差*****
MBR  :mixed        自動判斷記錄模式

面試題: 說明 SBR和RBR的區(qū)別?

SBR : statement 做什么記錄什么,記錄的就是SQL語句,
可讀性較強,日志量相對較少,日志記錄可能會有誤差  
RBR : row 記錄數(shù)據(jù)行的變化。默認模式 可讀性差,
日志量大,日志記錄準確不會出現(xiàn)誤差

2.3.5 binlog events (二進制日志事件)

1.簡介

二進制日志內(nèi)容以事件為最小記錄單元。
對于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        事件發(fā)生的時間
create database  xiong  事件內(nèi)容

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)查看二進制日志事件信息

image.png
xiong[(none)]>show binlog events in 'mysql-bin.000001';
image.png

2.4 內(nèi)容查看和截取

2.4.1 內(nèi)容查看命令

[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)數(shù)據(jù)恢復

確認起點和終點

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增強
主從性能,高可用環(huán)境,集群

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,并統(tǒng)一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日志信息

image.png

DDL , DCL 一個事務是一個ID
DML

image.png

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;
企業(yè)建議,至少保留兩個全備周期+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關閉掉,啟動數(shù)據(jù)庫
2.把數(shù)據(jù)庫關閉,開啟binlog,啟動數(shù)據(jù)庫
刪除所有binlog,并從000001開始重新記錄日志
*reset master;     主從關系中,主庫執(zhí)行此操作,主從環(huán)境必崩

2.6.3 binlog的滾動

oldguo[(none)]>flush logs;flush logs;
重啟數(shù)據(jù)庫
select @@max_binlog_size;
備份時,某些參數(shù)會觸發(fā).

3.慢日志

3.1 簡介

記錄運行較慢的語句,記錄到slowlog中
功能是輔助優(yōu)化的工具日志
應激性的慢 ----> show processlist;
一段時間慢 ----> slow 記錄,統(tǒng)計

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方式:(分析慢日志,二進制日志,錯誤日志...)

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容