日誌管理
一、錯誤日誌(log_error)
記錄啓動\關閉\日常運行過程中,狀態信息,警告,錯誤
錯誤日誌配置
默認就是開啓的: /數據路徑下/hostname.err
手工設定:
Master [(none)]>select @@log_error;
vim /etc/my.cnf
log_error=/var/log/mysql.log
log_timestamps=system
重啓生效
show variables like 'log_error';
二、binlog(binary logs):二進制日誌
(1)備份恢復必須依賴二進制日誌
(2)主從環境必須依賴二進制日誌
2.1 binlog配置 (5.7必須加server_id)
注意:MySQL默認是沒有開啓二進制日誌的。
基礎參數查看:
開關:
[(none)]>select @@log_bin;
日誌路徑及名字
[(none)]>select @@log_bin_basename;
服務ID號:
[(none)]>select @@server_id;
二進制日誌格式:
[(none)]>select @@binlog_format;
雙一標準之二:
[(none)]>select @@sync_binlog; 每次事務提交,必然保證binlog cache中的日誌落到磁盤
2.1.1 創建日誌目錄
mkdir /data/binlog
chown -R mysql.mysql /data/binlog
2.1.2 修改配置文件
vim /etc/my.cnf
server_id=6 ----->5.6中,單機可以不需要此參數
log_bin=/data/binlog/mysql-bin
binlog_format=row
2.1.3 重啓數據庫生效
/etc/init.d/mysqld restart
2.1.4 參數說明
server_id=3306
主要是在主從複製過程中必須要加的,但是在5.7版本中,要用以下參數(log_bin),開啓binlog日誌,即使是單機也是必加的
log_bin=/data/binlog/mysql-bin
(1)開啓二進制日誌功能
(2)設置二進制日誌目錄及名稱前綴
binlog_format=row
binlog是SQL層的功能。記錄的是變更SQL語句,不記錄查詢語句。
2.2 日誌文件查看
2.2.1 查看日誌的開啓情況
log_bin參數設置的路徑,可以找到二進制日誌
Master [(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.2.2 查看一共多少個binlog
Master [(none)]>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 154 |
+------------------+-----------+
1 row in set (0.01 sec)
1.flush logs滾動一個新的日誌
Master [(none)]>flush logs; 刷新出新的日誌文件,往最新的日誌裏寫
Query OK, 0 rows affected (0.03 sec)
Master [(none)]>flush logs;
Query OK, 0 rows affected (0.01 sec)
Master [(none)]>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 201 |
| mysql-bin.000002 | 201 |
| mysql-bin.000003 | 154 |
+------------------+-----------+
3 rows in set (0.00 sec)
2.2.3 查看mysql正在使用的日誌文件
show master status;
Master [(none)]>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
Master [(none)]>
file:當前MySQL正在使用的文件名
Position:最後一個事件的結束位置號
2.2.4 模擬binlog恢復數據
show binlog events in ‘mysql-bin.000001’ limit 100
mysql -e "show binlog events in ‘mysql-bin.000001’ " | grep xxx
1.建庫建表插入數據
2.模擬數據
create database bindb charset utf8mb4;
use bindb
create table t1(id int);
begin;
insert into t1 values(1),(2),(3);
commit;
drop database bindb;
3.數據恢復
在沒備份的情況下,通過日誌來恢復。
通過日誌找到建庫這個時間點,和drp之前的時間點
3.1 分析binlog
起點:
終點:
mysql>show master status
file:當前MySQL正在使用的文件名
mysql>show binlog events in 'mysql-bin.filenumXX'
關注點在Pos
通過上面的語句找到建庫create那一行的位置
通過上面的語句找到刪庫drop那一行的位置
4.截取日誌
mysqlbinlog --start-position=219 --stop-position=1357 /data/binlog/mysql-bin.filenumXXX > /tmp/bin.sql
5.恢復日誌
當前會話臨時關閉日誌記錄。 因爲重新導入會產生新的重複的binlog,而且是無用的。所以先關閉記錄
mysql>set sql_log_bin=0;
mysql>source /tmp/bin.sql
mysql>set sql_log_bin=1;
6.驗證數據
思考:如果是生產環境中,此種恢復手段有什麼弊端?
-
binlog記錄不單單是一個數據庫的操作,可能還會對其他數據庫造成影響
解決(-d):mysqlbinlog -d bindb --start-position=219 --stop-position /data/binlog/mysql-bin.000005 -
斷斷續續的截取
-
數據行多
-
需要的日誌再多個文件中分佈,跨文件
起點:假如,mysql-bin.000001
終點:一般是最後一個文件,假如,mysql-bin.0002
mysqlbinlog --start-datetime= xx --stop-datetime= xx mysql-bin.000001 mysql-bin.000002
通過時間來截取二進制日誌,但一秒內可能發生多次。 -
binlog實際上是數據恢復時配合備份一起恢復數據的手段
2.3 日誌內容查看
2.3.1 event查看
Master [binlog]>show binlog events in 'mysql-bin.000003';
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
| mysql-bin.000003 | 4 | Format_desc | 6 | 123 | Server ver: 5.7.20-log, Binlog ver: 4 |
| mysql-bin.000003 | 123 | Previous_gtids | 6 | 154 | |
| mysql-bin.000003 | 154 | Anonymous_Gtid | 6 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000003 | 219 | Query | 6 | 319 | create database binlog |
| mysql-bin.000003 | 319 | Anonymous_Gtid | 6 | 384 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000003 | 384 | Query | 6 | 486 | use `binlog`; create table t1 (id int) |
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
Log_name:binlog文件名
Pos:開始的position *****
Event_type:事件類型
Format_desc:格式描述,每一個日誌文件的第一個事件,多用戶沒有意義,MySQL識別binlog必要信息
Server_id:mysql服務號標識
End_log_pos:事件的結束位置號 *****
Info:事件內容*****
補充:
SHOW BINLOG EVENTS
[IN 'log_name']
[FROM pos]
[LIMIT [offset,] row_count]
[root@db01 binlog]# mysql -e "show binlog events in 'mysql-bin.000004'" |grep drop
2.3.2 binlog文件內容詳細查看
mysqlbinlog /data/mysql/mysql-bin.000006
查看日誌解碼後的內容:mysqlbinlog --base64-output=decode-rows -vvv /data/binlog/mysql-bin.000003 > /tmp/a.sql 再利用vim 查看
mysqlbinlog -d binlog /data/binlog/mysql-bin.000003
[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
2.4 基於Position號進行日誌截取
核心就是找截取的起點和終點
--start-position=321
--stop-position=513
mysqlbinlog --start-position=219 --stop-position=1347 /data/binlog/mysql-bin.000003 >/tmp/bin.sql
案例: 使用binlog日誌進行數據恢復
模擬:
1.
[(none)]>create database binlog charset utf8;
2.
[(none)]>use binlog;
[binlog]>create table t1(id int);
3.
[binlog]>insert into t1 values(1);
[binlog]>commit;
[binlog]>insert into t1 values(2);
[binlog]>commit;
[binlog]>insert into t1 values(3);
[binlog]>commit;
4.
[binlog]>drop database binlog;
恢復:
[(none)]>show master status ;
[(none)]>show binlog events in 'mysql-bin.000004';
[root@db01 binlog]# mysqlbinlog --start-position=1227 --stop-position=2342 /data/binlog/mysql-bin.000004 >/tmp/bin.sql
[(none)]>set sql_Log_bin=0;
[(none)]>source /tmp/bin.sql
面試案例:
1. 備份策略每天全備,有全量的二進制日誌
2.業務中一共10個庫,其中一個被誤drop了
3. 需要在其他9個庫正常工作過程中進行數據恢復
2.5 binlog 維護操作
2.5.1 日誌滾動
日誌滾動的觸發情景:
mysql>flush logs;
mysql>select @@max_binlog_size 達到指定的大小會自動日誌滾動一次。
mysqladmin -uroot -p123 flush-logs
mysqldump -F
重啓數據庫自動滾動
2.5.2 日誌刪除
注意:不要使用rm命令刪除日誌。
自動刪除機制
mysql>select @@expire_logs_days;
默認是0,單位是天,代表永不刪除。
問題:到底設置多少天合適?閾值?
一個全備份週期是x,一般生產一般建議最少2x+1
set global expire_logs_days=15;
手工刪除
to表示刪除到xxx
mysql> purge binary logs to ‘xxxxx’;
全部清空
mysql>reset master;
比較危險,在主庫執行此操作,主從必宕
2.6 binlog日誌的GTID新特性
無gtid:
mysqlbinlog --start-position --stop-position
mysqlbinlog -d world --start-position --stop-position
2.6.1 GTID 介紹
5.6 版本新加的特性,5.7中做了加強
5.6 中不開啓,沒有這個功能.
5.7 中的GTID,即使不開也會有自動生成
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
2.6.2 GTID(Global Transaction ID)
主要是爲了主從複製一致性。
它的方便之處:
刷新前:show master status;
刷新:flush logs;
刷新後:show master status; 會發現GTID是連續的
是對於一個已提交事務的編號,並且是一個全局唯一的編號。
它的官方定義如下:
GTID = source_id :transaction_id
7E11FA47-31CA-19E1-9E56-C43AA21293967:29
重要參數介紹:
vim /etc/my.cnf
gtid-mode=on
enforce-gtid-consistency=true
systemctl restart mysqld
select @@gtid_mode;
Master [(none)]>create database gtid charset utf8;
Query OK, 1 row affected (0.01 sec)
Master [(none)]>show master status ;
+------------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000004 | 326 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)
Master [(none)]>use gtid
Database changed
Master [gtid]>create table t1 (id int);
Query OK, 0 rows affected (0.01 sec)
Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 | 489 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
Master [gtid]>create table t2 (id int);
Query OK, 0 rows affected (0.01 sec)
Master [gtid]>create table t3 (id int);
Query OK, 0 rows affected (0.02 sec)
Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 | 815 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-4 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
Master [gtid]>begin;
Query OK, 0 rows affected (0.00 sec)
Master [gtid]>insert into t1 values(1);
Query OK, 1 row affected (0.00 sec)
Master [gtid]>commit;
Query OK, 0 rows affected (0.00 sec)
Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 | 1068 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
Master [gtid]>begin;
Query OK, 0 rows affected (0.00 sec)
Master [gtid]>insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)
Master [gtid]>commit;
Query OK, 0 rows affected (0.01 sec)
Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 | 1321 | | | dff98809-55c3-11e9-a58b-000c2928f5dd:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
基於DML語句的爲一個GTID
2.6.3 基於GTID進行查看binlog
具備GTID後,截取查看某些事務日誌:
--include-gtids
--exclude-gtids
--skip-gtids
mysqlbinlog --include-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:1-6' --exclude-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:4' /data/binlog/mysql-bin.000004
2.6.4 GTID日誌截取
Info
起點:
單純看binlog,有點麻煩 一個個看
mysql>show binlog events in ‘mysqk-bin.000002’;
終點:
mysql>show master status;
mysql>show binlog events in ‘mysql-bin.XXXXXX’;
從以上可以得出,GTID範圍是 5 - 11
確認範圍後確認文件:
確認文件:mysql-bin.000002 mysql-bin.000003 mysql-bin.000004
截取:
cd /data/inlog/
mysqlbinlog --include-gtids='9b8e7056xxxxxxxxxxxxxx:5-11' mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 > /tmp/gtid.sql
如果只想截取1到5,7到10
mysqlbinlog --include-gtids='9b8e7056xxxxxxxxxxxxxx:5-11','9b8e7056xxxxxxxxxxxxxx:7-10' mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 > /tmp/gtid.sql
如果想截取1到14,跳過6
mysqlbinlog --include-gtids='9b8e7056xxxxxxxxxxxxxx:1-14' --exclude-gtids='9b8e7056xxxxxxxxxxxxxx:6' mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 > /tmp/gtid.sql
如果在備份的時候選擇 --skip-gtids,就不包含原有的gtid,相當於新的操作,這樣恢復的時候也不會去檢測到重複出錯
cd /data/inlog/
mysqlbinlog --skip-gtids --include-gtids='9b8e7056xxxxxxxxxxxxxx:5-11' mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 > /tmp/gtid.sql
2.6.5 GTID數據恢復
基於2.6.4
mysql>set sql_log_bin=0; 臨時關閉日誌記錄
mysql>source /tmp/gtid.sql
發現報錯,因爲少了 –skip-gtids 參數
這是因爲GTID的冪等性
開啓GTID後,MySQL恢復Binlog時,發現GTID存在。重複GTID的事務不會再執行了(即同一操作不能重複)。因此要過濾 gtid信息。
cd /data/inlog/
mysqlbinlog --skip-gtids --include-gtids='9b8e7056xxxxxxxxxxxxxx:5-11' mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 > /tmp/gtid1.sql
mysql>set sql_log_bin=0;
mysql>source /tmp/gtid1.sql
三、慢日誌
作用:
記錄慢SQL語句的日誌,定位低效SQL語句的工具日誌
開啓慢日誌(默認沒開啓)
開關:
slow_query_log=1
文件位置及名字
slow_query_log_file=/data/mysql/slow.log
設定慢查詢時間:
long_query_time=0.1
沒走索引的語句也記錄:
log_queries_not_using_indexes
vim /etc/my.cnf
slow_query_log=1
slow_query_log_file=/data/mysql/slow.log
long_query_time=0.1
log_queries_not_using_indexes
systemctl restart mysqld
mysqldumpslow 分析慢日誌
mysqldumpslow -s c -t 10 /data/mysql/slow.log
# 第三方工具(自己擴展)
https://www.percona.com/downloads/percona-toolkit/LATEST/
yum install perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL perl-Digest-MD5
toolkit工具包中的命令:
./pt-query-diagest /data/mysql/slow.log
Anemometer基於pt-query-digest將MySQL慢查詢可視化