mysql【週一】day14

日誌管理
錯誤日誌
作用
記錄數據庫啓動以來,狀態、警告、報錯。診斷數據庫報錯問題。
配置
默認: 開啓狀態。存放在數據目錄下(/data/3306/data),名字:主機名.err 。

查詢配置:

mysql> select @@log_error;
+-------------+
| @@log_error |
+-------------+
| ./db01.err  |
+-------------+
1 row in set (0.00 sec)

mysql> select @@datadir;
+------------------+
| @@datadir        |
+------------------+
| /data/3306/data/ |
+------------------+
1 row in set (0.00 sec)
mysql> 
修改配置: 
[root@db01 ~]# mkdir -p /data/3306/logs
[root@db01 ~]# chown -R mysql.mysql /data/*
[root@db01 ~]# vim /etc/my.cnf
[mysqld]
#添加:
log_error=/data/3306/logs/mysql.err

重啓報錯

  
[root@db01 ~]# /etc/init.d/mysqld restart
++++++++++++++++++++++
Shutting down MySQL.. SUCCESS! 
Starting MySQL.2020-05-09T06:51:36.457401Z mysqld_safe error: log-error set to '/data/3306/logs/mysql.err', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/data/3306/data/db01.pid).
++++++++++++++++++++++
[root@db01 ~]# touch /data/3306/logs/mysql.err 
[root@db01 ~]# chown -R mysql.mysql /data/*

重新啓動

[root@db01 ~]# /etc/init.d/mysqld restart

查看日誌
#模擬故障

[root@db01 ~]# chown -R root.root /data/3306/data/ibdata1 
[root@db01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. ERROR! The server quit without updating PID file (/data/3306/data/db01.pid).
[root@db01 ~]# 

排查思路 :

[ERROR]  行: 
2020-05-09T06:56:09.226056Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable

切割:

cp mysql.err mysql.err_`date +%F`
echo > mysql.err

二進制日誌(binlog) *****
作用
數據恢復。
主從複製。

記錄的內容介紹
記錄修改類操作(邏輯日誌,類似於SQL記錄)。
DML: insert update delete
DDL: create drop alter trucate
DCL: grant revoke

配置方法
默認: 未開啓 。
基礎參數查詢:

mysql> select @@log_bin;
mysql> select @@log_bin_basename;
mysql> select @@server_id;

設置基礎參數:

vim /etc/my.cnf
server_id=6                        # 主機ID,在主從複製會使用
log_bin=/data/3306/logs/mysql-bin  # 開關+文件路徑+文件名前綴  最終格式: mysql-bin.000001 

重啓生效

[root@db01 logs]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 

binlog內容的記錄格式
事件(event)的方式記錄
最小的記錄單元。
每個事件:
(1)事件描述 : 事件戳、server_id、加密方式、開始的位置(start_pos)、結束位置點(end_pos)
(2)事件內容 : 修改類的操作:SQL 語句 或者 數據行變化。

重點關注:
開始的位置(start_pos) : 1000字節
結束位置點(end_pos) :
事件內容

二進制日誌事件內容格式

mysql> select @@binlog_format;
+-----------------+
| @@binlog_format |
+-----------------+
| ROW             |
+-----------------+

作用:
對於DDL、DCL語句,直接將SQL本身記錄到binlog中
對於DML : insert、update、delete 受到binlog_format參數控制。
SBR : Statement : 語句模式。之前版本,默認模式
RBR : ROW : 行記錄模式。5.7以後,默認模式
MBR : miexd : 混合模式。

SBR、RBR區別:
update t1 set name=‘zhangsan’ where id<100;
SBR: 記錄SQL本身 。RBR: 100個數據行的變化。
SBR日誌量少,RBR日誌量大。
SBR記錄不夠準確,RBR記錄夠準確。

例如函數操作:
now()
rand()

binlog 的應用

查詢
日誌文件情況查詢 :

查看所有的日誌文件信息

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       154 |
+------------------+-----------+

刷新一個新的日誌

mysql> flush logs;
mysql> flush logs;
mysql> flush logs;
mysql> flush logs;
mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       201 |
| mysql-bin.000002 |       201 |
| mysql-bin.000003 |       201 |
| mysql-bin.000004 |       201 |
| mysql-bin.000005 |       154 |
+------------------+-----------+

當前數據庫使用的二進制日誌

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

內容查詢
數據模擬

mysql> create database ku charset utf8mb4;
mysql> use ku
Database changed
mysql> create table biao (id int);
mysql> insert into biao values(1);
mysql> commit;

查看日誌事件

mysql> show binlog events in 'mysql-bin.000005';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000005 |   4 | Format_desc    |         6 |         123 | Server ver: 5.7.28-log, Binlog ver: 4 |
| mysql-bin.000005 | 123 | Previous_gtids |         6 |         154 |                                       |
| mysql-bin.000005 | 154 | Anonymous_Gtid |         6 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql-bin.000005 | 219 | Query          |         6 |         323 | create database ku charset utf8mb4    |
| mysql-bin.000005 | 323 | Anonymous_Gtid |         6 |         388 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql-bin.000005 | 388 | Query          |         6 |         484 | use `ku`; create table biao (id int)  |
| mysql-bin.000005 | 484 | Anonymous_Gtid |         6 |         549 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql-bin.000005 | 549 | Query          |         6 |         619 | BEGIN                                 |
| mysql-bin.000005 | 619 | Table_map      |         6 |         664 | table_id: 108 (ku.biao)               |
| mysql-bin.000005 | 664 | Write_rows     |         6 |         704 | table_id: 108 flags: STMT_END_F       |
| mysql-bin.000005 | 704 | Xid            |         6 |         735 | COMMIT /* xid=24 */                   |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
mysql> 

查看日誌內容

[root@db01 logs]# mysqlbinlog /data/3306/logs/mysql-bin.000005 

create table 日誌內容

# at 388
#200509 15:49:59 server id 6  end_log_pos 484 CRC32 0x8b05dfaf 	Query	thread_id=3	exec_time=0	error_code=0
use `ku`/*!*/;
create table biao (id int)
/*!*/;
# at 484



#### insert 操作的日誌內容
# at 549
#200509 15:50:36 server id 6  end_log_pos 619 CRC32 0x515eec96 	Query	thread_id=3	exec_time=0	error_code=0
SET TIMESTAMP=1589010636/*!*/;
BEGIN
/*!*/;
# at 619
#200509 15:50:36 server id 6  end_log_pos 664 CRC32 0xedfb82fb 	Table_map: `ku`.`biao` mapped to number 108
# at 664
#200509 15:50:36 server id 6  end_log_pos 704 CRC32 0x5762132e 	Write_rows: table id 108 flags: STMT_END_F
BINLOG '
zGC2XhMGAAAALQAAAJgCAAAAAGwAAAAAAAEAAmt1AARiaWFvAAEDAAH7gvvt
zGC2Xh4GAAAAKAAAAMACAAAAAGwAAAAAAAEAAgAB//4BAAAALhNiVw=='
/*!*/;
# at 704
#200509 15:50:40 server id 6  end_log_pos 735 CRC32 0xb06a212b 	Xid = 24
COMMIT/*!*/;

[root@db01 logs]# mysqlbinlog --base64-output=decode-rows -vv  /data/3306/logs/mysql-bin.000005 

.....略。
BEGIN
/*!*/;
# at 619
#200509 15:50:36 server id 6  end_log_pos 664 CRC32 0xedfb82fb 	Table_map: `ku`.`biao` mapped to number 108
# at 664
#200509 15:50:36 server id 6  end_log_pos 704 CRC32 0x5762132e 	Write_rows: table id 108 flags: STMT_END_F
### INSERT INTO `ku`.`biao`
### SET
###   @1=1 /* INT meta=0 nullable=1 is_null=0 */
# at 704
#200509 15:50:40 server id 6  end_log_pos 735 CRC32 0xb06a212b 	Xid = 24
COMMIT/*!*/;

binlog日誌截取及恢復演練
故障模擬

mysql> drop database ku;
Query OK, 1 row affected (0.01 sec)

需求恢復ku的所有數據到刪庫之前。

思路:

  1. 截取從建庫以來到刪庫之前的所有binlog。
    起點: 建庫的位置點(position)。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      886 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> show binlog events in 'mysql-bin.000005';

| mysql-bin.000005 | 219 | Query          |         6 |         323 | create database ku charset utf8mb4   

終點:刪庫的位置點(position)。

| mysql-bin.000005 | 800 | Query          |         6 |         886 | drop database ku   


[root@db01 logs]# mysqlbinlog --start-position=219 --stop-position=800 /data/3306/logs/mysql-bin.000005 >/tmp/bin.sql 
 
2. 將截取的日誌進行回放。
mysql> set sql_log_bin=0;
mysql> source /tmp/bin.sql;
mysql> set sql_log_bin=1;

彩蛋:
思考一下:如果生產中會有什麼痛點?

  1. 需要的日誌在多個文件中,怎麼截取?
    場景模擬:
mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      886 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
mysql> create database tongdian charset=utf8mb4;
mysql> use tongdian 
mysql> create table t1 (id int);
mysql> flush logs;
mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
mysql> insert into t1 values(1),(2),(3);
mysql> commit;
mysql> flush logs;
mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
mysql> create table t2(id int);
mysql> insert into t2 values(1),(2),(3);
mysql> commit;
mysql> flush logs;
mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 |      154 |              |                  |                   |

mysql> insert into t2 values(11),(22),(33);
mysql> commit;
mysql> drop database tongdian;

恢復方法:
方法1: 分段截取
–start-position --stop-position

方法2: 時間戳截取

  1. 找起點 :建庫的時間戳

(1) 起點: postion 號

mysql> show binlog events in 'mysql-bin.000005';
| mysql-bin.000005 |  951 | Query          |         6 |        1073 | create database tongdian charset=utf8mb4 |

(2) 通過position 過濾時間戳

[root@db01 logs]# mysqlbinlog --start-position=951  --stop-position=1073 mysql-bin.000005 |grep -A 1 '^\#\ at\ 951'
# at 951
#200509 17:11:23 server id 6  end_log_pos 1073 CRC32 0x220759ef 	Query	thread_id=8	exec_time=0	error_code=0
  1. 找終點
[root@db01 logs]# mysql -e "show binlog events in  'mysql-bin.000008'"
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000008 |   4 | Format_desc    |         6 |         123 | Server ver: 5.7.28-log, Binlog ver: 4 |
| mysql-bin.000008 | 123 | Previous_gtids |         6 |         154 |                                       |
| mysql-bin.000008 | 154 | Anonymous_Gtid |         6 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql-bin.000008 | 219 | Query          |         6 |         295 | BEGIN                                 |
| mysql-bin.000008 | 295 | Table_map      |         6 |         344 | table_id: 112 (tongdian.t2)           |
| mysql-bin.000008 | 344 | Write_rows     |         6 |         394 | table_id: 112 flags: STMT_END_F       |
| mysql-bin.000008 | 394 | Xid            |         6 |         425 | COMMIT /* xid=114 */                  |
| mysql-bin.000008 | 425 | Anonymous_Gtid |         6 |         490 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql-bin.000008 | 490 | Query          |         6 |         594 | drop database tongdian                |

 mysqlbinlog  mysql-bin.000008
 
[root@db01 logs]#  mysqlbinlog  mysql-bin.000008
#200509 17:13:52 
  1. 截取日誌
[root@db01 logs]# mysqlbinlog  --start-datetime="2020-05-09 17:11:23"  --stop-datetime="2020-05-09 17:14:01"   mysql-bin.000005 mysql-bin.000006 mysql-bin.000007 mysql-bin.000008 >/tmp/data.sql

遺留:

10個文件 :

1 和 10 pos
中間

mysqlbinlog start-pos 1 >/tmp/1.sql 
mysqlbinlog    2、9  > /tmp/2.sql  
mysqlbinlog stop-pos 10  >/tmp/3.sql 

方法3:gtid (後面講)

  1. binlog屬於全局日誌,日誌中有其他庫的操作,怎麼排除掉?
mysqlbinlog  -d oldboy mysql-bin.000008 > /tmp/bin.sql 
  1. binlog中100w個事件,怎麼快速找到drop database的位置點?
mysql> pager grep "DROP" 
[root@db01 ~]# mysql -e "show binlog events in 'mysql-bin.000014'" |less
[root@db01 ~]# mysql -e "show binlog events in 'mysql-bin.000014'" |grep
  1. 比如刪除的庫,建庫是在2年前操作的。這種情況怎麼辦?
    每天全備,binlog完好的。
    可以使用 全備+binlog方式實現恢復數據故障之前。

基於GTID的binlog應用
GTID 全局事務ID。
對每個事務,進行單獨編號。連續不斷進行增長。
表示方式
server_uuid:N

配置
查看參數

mysql> show variables like '%GTID%';

設置參數

vim /etc/my.cnf 
gtid_mode=ON                  #開關
enforce_gtid_consistency=ON   #強制GTID一致性
log_slave_updates=ON          #強制從庫更新binlog

建議: 5.7版本以後,都開啓GTID。最好是搭建環境就開啓。

應用

模擬環境

mysql> create database gtdb charset utf8mb4;
Query OK, 1 row affected (0.00 sec)
mysql> show master status ;
+------------------+----------+--------------+------------------+----------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000016 |      329 |              |                  | 202628e9-9265-11ea-b4a0-000c29248f69:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
mysql> use gtdb;
mysql> create table t1(id int);
mysql> show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000016 |      491 |              |                  | 202628e9-9265-11ea-b4a0-000c29248f69:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
mysql> begin;
mysql> insert into t1 values(1);
mysql> insert into t1 values(2);
mysql> insert into t1 values(3);
mysql> commit;
mysql> show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000016 |      914 |              |                  | 202628e9-9265-11ea-b4a0-000c29248f69:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+

通過GTID方式截取日誌

錯誤的截取

[root@db01 data]# mysqlbinlog --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:1-3' /data/3306/logs/mysql-bin.000016 >/tmp/gt.sql

補充: 爲什麼報錯?
gtid有“冪等性”檢查。GTID的生成,通過Set gtid_next命令實現的。
例如:

SET @@SESSION.GTID_NEXT= '202628e9-9265-11ea-b4a0-000c29248f69:1'

執行Set命令時,自動檢查當前系統是否包含這個GTID信息,如果有就跳過。

正確的方式:

[root@db01 data]# mysqlbinlog --skip-gtids --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:1-3' /data/3306/logs/mysql-bin.000016 >/tmp/gt1.sql

思考問題:
開了GTID方式之後,是否可以pos方式截取?需要不需要加skip-gtids?
可以。需要加–skip-gtids

擴展用法
需求: gtid : 1-10 跳過第5個

mysqlbinlog --skip-gtids --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:1-10' --exclude-gtids='202628e9-9265-11ea-b4a0-000c29248f69:5'

需求: 跨文件截取,bin001 bin002 bin003 202628e9-9265-11ea-b4a0-000c29248f69:1-10

mysqlbinlog --skip-gtids --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:1-10'  bin001 bin002  bin003

2.6.5 日誌滾動

命令觸發

mysql> flush logs; 
shell# mysqladmin flush-logs
shell# mysql -e "flush logs"
shell# mysqldump -F

自動觸發

mysql> select @@max_binlog_size;
+-------------------+
| @@max_binlog_size |
+-------------------+
|        1073741824 |
+-------------------+

重啓數據庫,會觸發刷新

2.6.6 日誌刪除方式
默認:不自動清理。直到空間寫滿。

配置自動清理
mysql> select @@expire_logs_days;
最少設置多少天合適?
參考全備時間週期。
例如: 全備週期是7天。可以保留8天。一般生產中保留兩輪備份週期的日誌,15天。

手工清理

Examples:
PURGE BINARY LOGS TO 'mysql-bin.010';
PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26';
  1. 慢日誌 SlOWLOG
    3.1 作用: 記錄MySQL工作過程中較慢的語句
    默認沒有開啓。按需求打開。
    3.2 如何配置
#在線配置
mysql> select @@slow_query_log;      # 開關
mysql> set global slow_query_log=1;  # 在線改
mysql> select @@slow_query_log_file; # 文件位置。離線改。
mysql> select @@long_query_time;     # 慢查詢時間設定。
mysql> set global long_query_time=0.1; # 在線設置,最低微秒級別。
mysql> select @@log_queries_not_using_indexes #如果沒走索引會被記錄
mysql> set global log_queries_not_using_indexes=1; #在線設置 
#永久生效:  
vim /etc/my.cnf
slow_query_log=1
slow_query_log_file=/data/3306/logs/slow.log 
long_query_time=0.1
log_queries_not_using_indexes=1

#重啓生效。

隨機模擬慢語句
略。

慢日誌分析

[root@db01 logs]# mysqldumpslow -s c -t 5 slow.log 
Reading mysql slow query log from slow.log
Count: 7  Time=0.01s (0s)  Lock=0.00s (0s)  Rows=177.1 (1240), root[root]@localhost
  select * from t100w as a join t100w as b limit N
Count: 6  Time=0.57s (3s)  Lock=0.00s (0s)  Rows=33.7 (202), root[root]@localhost
  select k1,count(*) from t100w where id<N group by k1 having count(*)>N
Count: 5  Time=0.59s (2s)  Lock=0.00s (0s)  Rows=893.0 (4465), root[root]@localhost
  select k1,count(*) from t100w where num<N group by k1,k2
Count: 5  Time=0.61s (3s)  Lock=0.00s (0s)  Rows=37.0 (185), root[root]@localhost
  select * from t100w where id<N order by num  desc
Count: 4  Time=0.61s (2s)  Lock=0.00s (0s)  Rows=496.0 (1984), root[root]@localhost
  select k1,count(*) from t100w where id<N group by k1,k2

自己擴展: pt-query-digest

備份恢復

  1. 關於備份恢復方面的職責
    (1)備份、恢復策略的設計。
    備份週期、備份工具、備份方式、恢復方式全部流程化
    (2)日常備份檢查
    日誌、備份內容
    (3)定期的恢復演練
    (4)數據故障時,利用現有的資源,快速恢復
    (5)數據遷移、升級。

** 備份工具介紹**
邏輯備份

mysqldump  / source   *****
mysqlbinlog  /source 
mydumper / myloader
select into outfile / load data infile 
binlog2sql
myflashback

物理備份

Percona Xtrabackup (PXB,XBK) *****
遷移表空間 
Mysql Enterpise backup(MEB,企業版)
8.0 clone plugin (8.0.17)

選型
100G 以內: 邏輯
100G 以上: 物理

超大型: 邏輯

mysqldump 工具使用

介紹:
mdp數據邏輯備份工具。(Create database\ create table \ insert)
MySQL 自帶的客戶端命令。可以實現遠程和本地備份。

參數
連接參數
-u
-p
-S
-h
-P
備份參數
-A 全備

[root@db01 ~]# mkdir -p /data/backup
[root@db01 ~]# mysqldump -uroot -p123  -A >/data/backup/full.sql

-B 單庫或多庫

[root@db01 ~]# mysqldump -uroot -p123  -B world gtdb test >/data/backup/db.sql

備份單表或多表

[root@db01 ~]# mysqldump -uroot -p123  world t1 country >/data/backup/tab.sql

–master-data=2
功能: 1.自動記錄備份時的binlog信息(註釋)
2.自動鎖定所有表,自動解鎖(global read lock)。最好配合–single-transaction 參數,減少鎖表時間。

--master-data[=#]   This causes the binary log position and filename to be
                      appended to the output. If equal to 1, will print it as a
                      CHANGE MASTER command; if equal to 2, that command will
                      be prefixed with a comment symbol. This option will turn
                      --lock-all-tables on, unless --single-transaction is
                      specified too (in which case a global read lock is only
                      taken a short time at the beginning of the dump; don't
                      forget to read about --single-transaction below). In all
                      cases, any action on logs will happen at the exact moment
                      of the dump. Option automatically turns --lock-tables
                      off.
[root@db01 ~]# mysqldump -uroot -p123  -A  --master-data=2  >/data/backup/full.sql

–single-transaction
功能:
1. 對於InnoDB表,開啓獨立事務,通過快照備份表數據,不鎖表備份,可以理解爲熱備。

通過照片查人數。

[root@db01 ~]# mysqldump -uroot -p123  -A  --master-data=2  --single-transaction  >/data/backup/full.sql

–max_allowed_packet=64M 最大允許的數據包大小

[root@db01 ~]# mysqldump -uroot -p123  -A  --master-data=2  --single-transaction  --max_allowed_packet=64M  >/data/backup/full.sql

小故障
備份時超出最大數據包大小。

1153 - Got a packet bigger than 'max_allowed_packet' bytes 

-R -E --triggers 備份特殊對象使用

[root@db01 ~]# mysqldump -uroot -p123  -A  --master-data=2  --single-transaction  --max_allowed_packet=64M  -R -E  --triggers >/data/backup/full.sql

格式化備份文件

[root@db01 ~]# mysqldump -uroot -p  -A  --master-data=2  --single-transaction  --max_allowed_packet=64M  -R -E  --triggers >/data/backup/full_`date +%F`.sql

故障恢復演練(mdp+binlog,每天全備)
模擬環境

mysql> create database mdp charset utf8mb4;
mysql> use mdp
mysql> create table t1(id int);
mysql> insert into t1 values(1),(2),(3);
mysql> commit;

模擬 週一23:00 的全備

[root@db01 ~]# mysqldump -uroot -p  -A  --master-data=2  --single-transaction  --max_allowed_packet=64M  -R -E  --triggers >/data/backup/full_`date +%F`.sql

查看 GTID相關信息 :GTID截取起點。

SET @@GLOBAL.GTID_PURGED='202628e9-9265-11ea-b4a0-000c29248f69:1-35';

查看pos號,備份開始時binlog位置點信息。

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000023', MASTER_LOG_POS=158999;

模擬週二白天數據變化

mysql> use mdp;
mysql> create table t2 (id int);
mysql> insert into t2 values(1),(2),(3);
mysql> commit;

週二下午2點,誤刪除了mdp核心庫

mysql> drop database mdp;

故障恢復
思路:
(1) 恢復全備到週一晚上23:00
檢查全備:

vim /data/backup/full_2020-05-11.sql 

查看 GTID相關信息 :GTID截取起點。

SET @@GLOBAL.GTID_PURGED='202628e9-9265-11ea-b4a0-000c29248f69:1-35';

查看pos號,備份開始時binlog位置點信息。

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000023', MASTER_LOG_POS=158999;

(2) 截取日日誌:
起點:

mysql-bin.000023 202628e9-9265-11ea-b4a0-000c29248f69:36 或者 mysql-bin.000023 pos=158999

#終點: drop

[root@db01 backup]# mysql -uroot -p123 -e "show binlog events in 'mysql-bin.000023'"|grep -B 1 "drop database mdp"
mysql-bin.000023	159421	Gtid	6	159486	SET @@SESSION.GTID_NEXT= '202628e9-9265-11ea-b4a0-000c29248f69:38'
mysql-bin.000023	159486	Query	6	159575	drop database mdp
[root@db01 backup]# 

截取日誌

[root@db01 backup]# mysqlbinlog --skip-gtids --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:36-37' /data/3306/logs/mysql-bin.000023 >/data/backup/bin.sql

(3) 恢復

mysql> set sql_log_bin=0;
mysql> source /data/backup/full_2020-05-11.sql 
mysql> source /data/backup/bin.sql
mysql> set sql_log_bin=1;

(4) 檢查數據

mysql> use mdp
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_mdp |
+---------------+
| t1            |
| t2            |
+---------------+
2 rows in set (0.01 sec)

mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

mysql> select * from t2;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

mysqldump多種備份策略和恢復策略介紹
場景:
100G 全庫數據 全庫備份 30分鐘-40分鐘,恢復整庫需要5倍時間2.5-3小時之間
一張表 1G 被誤刪除了。

mysqldump 備份策略 :

  1. mdp full+ binlog 增量備份
    恢復單表數據思路:
    (1) 提取full全備中的故障表數據 ,恢復數據
# sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `t1`/!d;q'  full.sql>createtable.sql
# grep -i 'INSERT INTO `t1`'  full.sql >data.sql 

(2) binlog中截取全備到誤刪除t1之間對於這張表的修改

  1. 單庫單表備份+binlog增量
    恢復單表數據思路:
    (1)恢復單表的備份
    (2)binlog中截取備份到誤刪除t1之間對於這張表的修改

故障模擬
模擬原始數據

create database oldboy charset utf8mb4;
 use oldboy;
 create table oldguo (id int);
 insert into oldguo values(1),(2),(3);
 commit;

週一晚上全庫備份

mysqldump -uroot -p  -A  --master-data=2  --single-transaction  --max_allowed_packet=64M  -R -E  --triggers >/data/backup/full.sql

模擬週二白天的數據變化

use oldboy ;
insert into oldguo values(11),(22),(33);
commit;
create table  oldli(id int);
insert into oldli values(1),(2),(3);
commit;
insert into oldguo values(111),(222),(333);
commit;

模擬週二下午2點,誤刪除oldguo表

drop table  oldguo;

恢復過程

  1. 處理全備
[root@db01 ~]# sed -n '/CREATE TABLE `oldguo` /,/\;/p' /data/backup/full.sql >/data/backup/create.sql
[root@db01 ~]# grep -i 'INSERT INTO `oldguo`'  /data/backup/full.sql >/data/backup/insert.sql
  1. binlog 的截取
    範圍:
    起點: 通過備份。
   SET @@GLOBAL.GTID_PURGED='202628e9-9265-11ea-b4a0-000c29248f69:1-47';
終點: 通過 
	[root@db01 ~]#  mysql -uroot -p123 -e "show binlog events in 'mysql-bin.000023'" |grep -B 1 'DROP TABLE\ `oldguo`'
	mysql-bin.000023	163044	Gtid	6	163109	SET @@SESSION.GTID_NEXT= '202628e9-9265-11ea-b4a0-000c29248f69:54'
	mysql-bin.000023	163109	Query	6	163232	use `oldboy`; DROP TABLE `oldguo` /* generated by server */

[root@db01 ~]# 
[root@db01 ~]# mysqlbinlog --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:48-53' /data/3306/logs/mysql-bin.000023 |grep -B 8  '`oldboy`.`oldguo`'|grep 'GTID_NEXT'
SET @@SESSION.GTID_NEXT= '202628e9-9265-11ea-b4a0-000c29248f69:49'/*!*/;
SET @@SESSION.GTID_NEXT= '202628e9-9265-11ea-b4a0-000c29248f69:50'/*!*/;
SET @@SESSION.GTID_NEXT= '202628e9-9265-11ea-b4a0-000c29248f69:53'/*!*/;

截取:
方法1:

[root@db01 ~]# mysqlbinlog --skip-gtids --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:48-53' --exclude-gtids='202628e9-9265-11ea-b4a0-000c29248f69:51-52' /data/3306/logs/mysql-bin.000023 >/data/backup/bin.sql

方法2:

 mysqlbinlog --skip-gtids --include-gtids='202628e9-9265-11ea-b4a0-000c29248f69:48-50','202628e9-9265-11ea-b4a0-000c29248f69:53' /data/3306/logs/mysql-bin.000023 >/data/backup/bin1.sql
  1. 恢復數據
use oldboy;
set sql_log_bin=0;
source /data/backup/create.sql
source /data/backup/insert.sql
commit;
source /data/backup/bin.sql
set sql_log_bin=1;

實現單庫單表備份

shell# mkdir -p /data/backup/single_bak
mysql> select concat("mysqldump -uroot -p123 ",table_schema," ",table_name," >/data/backup/single_bak/",table_schema,"_",table_name,".sql") from information_schema.tables where table_schema not in ('sys','information_schema','performance_schema') into outfile '/tmp/single_bak.sh';
shell# sh /tmp/single_bak.sh &>/tmp/bak.log
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章