1. Introduction of gtid features:
GTID (global transaction identifier) is a new feature of MySQL 5.6, which can uniquely identify a transaction and consists of UUID+TID:
- UUID is the unique identifier of a MySQL instance
- TID is the number of committed transactions on this instance
In master-slave replication, GTID replaces the classic replication method. Instead of using binlog+pos to start replication, master_auto_postion = 1 is used to automatically match GTID breakpoints for replication.
To enable GTID, simply add the following parameters to the MySQL parameter file:
gtid-mode = ON
enforce_gtid_consistency = 1
2. Data recovery requirements:
To restore MySQL (hereinafter referred to as the A library) to the state of about 12:00 a.m. a day ago, the prerequisites are as follows:
- Enable GTID
- There are xtra backup files of A library before 12:00 in the morning yesterday
- Open binlog log (nonsense)
3. Recovery operation:
Perform data recovery on another MySQL (B database) to avoid affecting online business
1. Move the data directory of the B library and copy the backup file of the A library to the B library
mv data data_20160716 #移走B库data
mv A_back_20160714 data #移入A库备份文件
chown -R mysql12300.mysql data/
2. Open the B library and configure the master-slave
Check the GTID recorded in the xtrabackup_binlog_info file in the data directory:
[root@service-test1 data]$ cat xtrabackup_binlog_info
mysql-bin.000012 46455554 8133046e-4282-11e6-848e-026ca51d284c:1-4920155
Set @@global.gtid_purged in the B library (slave) to skip the GTID included in the backup, and execute change master to designate the A library as the main library:
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)
Note: Sometimes there is more than one GTID in xtrabackup_binlog_info. You can specify multiple GTIDs when setting @@global.gtid_purged, separated by commas .
Fourth, find the recovery point in the A library binlog and restore it
It should be noted that after the above operation, do not start slave directly, otherwise the B library will also run to the current state of the A library
Convert A library binlog to sql statement:
mysqlbinlog -vv mysql-bin.000011 > mysql-bin.000011.sql
Find the location around 12:00am the previous day and record the 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'/*!*/;
Open the slave in the B library and specify the location to restore to:
mysql> start slave until SQL_BEFORE_GTIDS='542ef021-9a64-11e5-bc49-025d3d22c211:1348360';
Query OK, 0 rows affected (0.00 sec)
When the specified GTID is reached, the SQL thread will stop, but the IO thread will continue to copy:
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)
Well, what data do you want to see yesterday morning? It's all in the B library~~~
Attachment: Frequently Asked Questions
When setting @@global.gtid_purged, you may encounter an error:
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.
This is because the @@GLOBAL.GTID_EXECUTED of this MySQL is not empty, just execute the following reset master operation:
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)