新聞中心
一、gtid特性介紹:

GTID(global transaction identifier)是MySQL 5.6的新特性,可以唯一的標(biāo)識(shí)一個(gè)事務(wù),由UUID+TID組成:
- UUID是MySQL實(shí)例的唯一標(biāo)識(shí)
- TID是該實(shí)例上已提交的事務(wù)的數(shù)量
在主從復(fù)制中,GTID代替了classic的復(fù)制方法,不再使用binlog+pos開啟復(fù)制,而是使用master_auto_postion = 1的方式自動(dòng)匹配GTID斷點(diǎn)進(jìn)行復(fù)制。
要開啟GTID,只需在MySQL參數(shù)文件中添加以下參數(shù):
- gtid-mode = ON
- enforce_gtid_consistency = 1
二、數(shù)據(jù)恢復(fù)需求:
需要將MySQL(以下簡(jiǎn)稱A庫)恢復(fù)到一天前的凌晨12:00左右的狀態(tài) 需要具備的前提條件如下:
- 開啟GTID
- 有A庫昨天凌晨12:00前的xtra備份文件
- 開啟binlog日志(廢話)
三、恢復(fù)操作:
在另一臺(tái)MySQL(B庫)上進(jìn)行數(shù)據(jù)的恢復(fù),這樣可以避免影響線上業(yè)務(wù)
1. 將B庫data目錄移走,拷貝A庫備份文件到B庫
- mv data data_20160716 #移走B庫data
- mv A_back_20160714 data #移入A庫備份文件
- chown -R mysql12300.mysql data/
2. 開啟B庫,配置主從
查看data目錄下xtrabackup_binlog_info文件中記錄的GTID:
- [root@service-test1 data]$ cat xtrabackup_binlog_info
- mysql-bin.000012 46455554 8133046e-4282-11e6-848e-026ca51d284c:1-4920155
在B庫(slave)設(shè)置@@global.gtid_purged跳過備份包含的GTID,并執(zhí)行change master to指定A庫為主庫:
- mysql> SET GLOBAL gtid_purged="8133046e-4282-11e6-848e-026ca51d284c:1-4920155";
- Query OK, 0 rows affected (0.00 sec)
- mysql> change master to Master_Host ='10.11.21.14',Master_Port=3306,Master_User='replica',Master_Password='XXXXXXXXX',MASTER_AUTO_POSITION=1;
- Query OK, 0 rows affected, 2 warnings (0.01 sec)
注意: xtrabackup_binlog_info中的GTID有時(shí)不止一個(gè),設(shè)置@@global.gtid_purged時(shí)指定多個(gè)即可,以逗號(hào)隔開。
四、在A庫binlog中找到恢復(fù)點(diǎn)并進(jìn)行恢復(fù)
需要特別注意的是,在上述操作后,不要直接start slave,否則B庫也又會(huì)跑到當(dāng)前A庫的狀態(tài)
將A庫binlog轉(zhuǎn)換為sql語句:
- mysqlbinlog -vv mysql-bin.000011 > mysql-bin.000011.sql
找到前一天凌晨12:00左右的位置并記錄GTID:
- # at 561467475
- #160521 0:24:31 server id 212177500 end_log_pos 561467523 CRC32 0x216072ca GTID [commit=yes]
- SET @@SESSION.GTID_NEXT= '542ef021-9a64-11e5-bc49-025d3d22c211:1348360'/*!*/;
在B庫開啟slave并指定恢復(fù)到的位置:
- mysql> start slave until SQL_BEFORE_GTIDS='542ef021-9a64-11e5-bc49-025d3d22c211:1348360';
- Query OK, 0 rows affected (0.00 sec)
當(dāng)執(zhí)行到了指定的GTID,SQL線程便會(huì)停止,但I(xiàn)O線程還會(huì)繼續(xù)復(fù)制:
- mysql> show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 10.30.21.11
- Master_User: replica
- Master_Port: 7500
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000011
- Read_Master_Log_Pos: 810203081
- Relay_Log_File: relay-bin.000002
- Relay_Log_Pos: 5707357
- Relay_Master_Log_File: mysql-bin.000011
- Slave_IO_Running: Yes
- Slave_SQL_Running: No
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 561467475
- Relay_Log_Space: 254443161
- Until_Condition: SQL_BEFORE_GTIDS
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: NULL
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 21117500
- Master_UUID: 63f38fe7-9e3b-11e5-9554-02eeb2c1042f
- Master_Info_File: /data1/mysql10070/data/master.info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State:
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set: 542ef021-9a64-11e5-bc49-025d3d22c211:1341313-1368005
- Executed_Gtid_Set: 44226252-9e38-11e5-9540-02212401d46f:1,
- 542ef021-9a64-11e5-bc49-025d3d22c211:1-1348359,
- 63f38fe7-9e3b-11e5-9554-02eeb2c1042f:1
- Auto_Position: 1
- 1 row in set (0.00 sec)
好啦,想看昨天凌晨的哪些數(shù)據(jù)呀?都在B庫里啦~~~
附:常見問題
在設(shè)置@@global.gtid_purged時(shí),可能會(huì)遇到報(bào)錯(cuò):
- mysql> SET GLOBAL gtid_purged="8133046e-4282-11e6-848e-026ca51d284c:1-4920155";
- ERROR 1840 (HY000): @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
這是因?yàn)檫@臺(tái)MySQL的@@GLOBAL.GTID_EXECUTED并不是空的,執(zhí)行以下reset master操作就好了:
- mysql> SET GLOBAL gtid_purged="8133046e-4282-11e6-848e-026ca51d284c:1-4920155";
- ERROR 1840 (HY000): @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
- mysql> show master status;
- +------------------+----------+--------------+------------------+-------------------------------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+------------------+-------------------------------------------+
- | mysql-bin.000002 | 191 | | | 3857c25c-2caa-11e6-b61b-021feddc3827:1-14 |
- +------------------+----------+--------------+------------------+-------------------------------------------+
- 1 row in set (0.00 sec)
- mysql> reset master;
- Query OK, 0 rows affected (0.01 sec)
- mysql> show master status;
- +------------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+------------------+-------------------+
- | mysql-bin.000001 | 151 | | | |
- +------------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- mysql> SET GLOBAL gtid_purged="8133046e-4282-11e6-848e-026ca51d284c:1-4920155";
- Query OK, 0 rows affected (0.00 sec)
文章題目:MySQL基于gtid特性與xtrabackup的數(shù)據(jù)恢復(fù)
瀏覽路徑:http://m.fisionsoft.com.cn/article/dpojidj.html


咨詢
建站咨詢
