刚刚听了吴老师是复制章节课程,对于GTID模式下备份数据--set-gtid-purged=OFF 参数有些不理解,于是乎做了实验,加深理解,得出些结论,如有错漏请批评指正!
部分备份:
[[email protected] mysql]# /usr/local/mysql/bin/mysqldump -uroot -p -S /data/mysql3306/data/mysql3306.sock lyh2 >/home/backup/lyh2-3306-`date +%Y%d%m`.sql Enter password: Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don‘t want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
检查部分备份文件内容:
出现了SET @@GLOBAL.GTID_PURGED=‘5adbcab4-fcbb-11e7-a900-000c29e774f1:1-347‘;
同时,备份的是指定库(没有像我猜想的会按照全局GTID,备份所有事物)
[[email protected] mysql]# less /home/backup/lyh2-3306-20182802.sql -- MySQL dump 10.13 Distrib 5.7.19, for linux-glibc2.12 (x86_64) -- -- Host: localhost Database: lyh2 -- ------------------------------------------------------ -- Server version 5.7.19-log /*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; /*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; /*!40101 SET @[email protected]@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @[email protected]@TIME_ZONE */; /*!40103 SET TIME_ZONE=‘+00:00‘ */; /*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; /*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN; SET @@SESSION.SQL_LOG_BIN= 0; -- -- GTID state at the beginning of the backup -- SET @@GLOBAL.GTID_PURGED=‘5adbcab4-fcbb-11e7-a900-000c29e774f1:1-347‘; -- -- Table structure for table `tb2` -- DROP TABLE IF EXISTS `tb2`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `tb2` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `tb2` -- LOCK TABLES `tb2` WRITE; /*!40000 ALTER TABLE `tb2` DISABLE KEYS */; INSERT INTO `tb2` VALUES (1); /*!40000 ALTER TABLE `tb2` ENABLE KEYS */; UNLOCK TABLES; SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN; /*!40103 SET [email protected]_TIME_ZONE */; /*!40101 SET [email protected]_SQL_MODE */; /*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; /*!40014 SET [email protected]_UNIQUE_CHECKS */; /*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; /*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; /*!40101 SET [email protected]_COLLATION_CONNECTION */; /*!40111 SET [email protected]_SQL_NOTES */;
完整备份:
[[email protected] mysql]# /usr/local/mysql/bin/mysqldump -uroot -p -S /data/mysql3306/data/mysql3306.sock -A --master-data=2 --single-transaction >/home/backup/full-3306-`date +%Y%d%m`.sql Enter password: Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don‘t want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
检查完整备份文件内容:
依然出现了SET @@GLOBAL.GTID_PURGED=‘5adbcab4-fcbb-11e7-a900-000c29e774f1:1-347‘;
以及完整的数据备份
[[email protected] mysql]# less /home/backup/full-3306-20182802.sql -- MySQL dump 10.13 Distrib 5.7.19, for linux-glibc2.12 (x86_64) -- -- Host: localhost Database: -- ------------------------------------------------------ -- Server version 5.7.19-log /*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; /*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; /*!40101 SET @[email protected]@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @[email protected]@TIME_ZONE */; /*!40103 SET TIME_ZONE=‘+00:00‘ */; /*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; /*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN; SET @@SESSION.SQL_LOG_BIN= 0; -- -- GTID state at the beginning of the backup -- SET @@GLOBAL.GTID_PURGED=‘5adbcab4-fcbb-11e7-a900-000c29e774f1:1-347‘; -- -- Position to start replication or point-in-time recovery from -- -- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000006‘, MASTER_LOG_POS=29189664; -- -- Current Database: `lyh1` -- CREATE DATABASE /*!32312 IF NOT EXISTS*/ `lyh1` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `lyh1`; -- -- Table structure for table `t1` -- DROP TABLE IF EXISTS `t1`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; 省略。。。。。
综上,无论是完整备份,还是部分备份,如果不设置 --set-gtid-purged=OFF 这个参数,最终的备份文件中会有这样一句话:SET @@GLOBAL.GTID_PURGED=‘5adbcab4-fcbb-11e7-a900-000c29e774f1:1-347‘;
这个的区间是主库中当前所完成的所有事务号。这条语句在备份被恢复的时候,起到的作用是,不再从主库同步1-347 这个范围内的事务了。
如果我们是部分备份,我们不应该阻止从库同步1-347全部区间的数据。因此部分备份是加上--set-gtid-purged=OFF 这句,不强行指定跳过这些操作。
根据上面实验可以看出,SET @@GLOBAL.GTID_PURGED 可以在从库指定跳过任何事物
那么,如果在从库恢复完数据备份,
SET @@GLOBAL.GTID_PURGED=‘5adbcab4-fcbb-11e7-a900-000c29e774f1:1-348‘;
指定到任意位置,如348,那么从库就会跳过348号以及以前的所有事物,认为已经同步完成。并且在show slave status 完全看不出空洞。
本例中从库只恢复到1-347号事务,没有开启同步时,主库执行了348,349号事务,由于从库设置了
SET @@GLOBAL.GTID_PURGED=‘5adbcab4-fcbb-11e7-a900-000c29e774f1:1-348‘;
从库再开启复制时,从库直接从349号事务开始同步,跳过348号事务,于是348号事务完美的丢失了,没有痕迹。
在从库show slave status
[email protected]:mysql3307.sock [lyh1] >show slave status\G *************************** 1. row *************************** 。。。。。 Slave_IO_Running: Yes Slave_SQL_Running: Yes 。。。。。 Retrieved_Gtid_Set: 5adbcab4-fcbb-11e7-a900-000c29e774f1:349 Executed_Gtid_Set: 5adbcab4-fcbb-11e7-a900-000c29e774f1:1-349
通过
show master status; 记录已执行的事务号
reset master;
SET @@GLOBAL.GTID_PURGED=‘已执行的事务号‘;
可以设置多源复制
转自
关于GTID模式下备份时 --set-gtid-purged=OFF 参数的实验 - FireFly Club
http://q.fireflyclub.org/?/question/65
原文地址:https://www.cnblogs.com/paul8339/p/9359770.html