在MySQL 5.7版本中使用mysqldump导出数据时,如果未显式指定set-gtid-purged参数,会报下面错误:
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.
mysqldump帮助文档中说明如下:
Add 'SET @@GLOBAL.GTID_PURGED' to the output. Possible values for this option are ON, OFF and AUTO. If ON is used and GTIDs are not enabled on the server, an error is generated. If OFF is used, this option does nothing. If AUTO is used and GTIDs are enabled on the server, 'SET @@GLOBAL.GTID_PURGED' is added to the output. If GTIDs are disabled, AUTO does nothing. If no value is supplied then the default (AUTO) value will be considered.
未设置set-gtid-purged或设置set-gtid-purged=ON时,其导出脚本为:
-- MySQL dump 10.13 Distrib 5.7.19, for Linux (x86_64) -- -- Host: 127.0.0.1 Database: db003 -- ------------------------------------------------------ -- Server version 5.7.19-log /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@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='025fd638-89ea-11e9-a749-40f2e9cf3aaa:10-13'; -- -- Current Database: `db003` -- CREATE DATABASE /*!32312 IF NOT EXISTS*/ `db003` /*!40100 DEFAULT CHARACTER SET utf8 */;
设置set-gtid-purged=OFF时,其导出脚本为:
-- MySQL dump 10.13 Distrib 5.7.19, for Linux (x86_64) -- -- Host: 127.0.0.1 Database: db003 -- ------------------------------------------------------ -- Server version 5.7.19-log /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Current Database: `db003` -- CREATE DATABASE /*!32312 IF NOT EXISTS*/ `db003` /*!40100 DEFAULT CHARACTER SET utf8 */;
踩坑指南1:主库不产生BINLOG
在MySQL升级迁移过程中,使用mysqldump+set-gtid-purged=ON导出数据脚本,并将导出的数据脚本导入到已搭建好的群集主库上,导入完成后发现群集无数据,原因在于数据脚本中设置SET @@SESSION.SQL_LOG_BIN= 0;,因此导入过程未生产BINLOG,无法同步到群集从库。
踩坑指南2:主库BINLOG未正常应用到从库
由于导出数据脚本的数据脚本中存在SET @@GLOBAL.GTID_PURGED,于是进行如下操作:
1、在群集主库上执行RESET MASTER
2、群集从库复制异常,由于主从数据相同,使用CHANGE MASTER+最新主库位点重新搭建复制。
3、主库执行DDL/DML操作,发现群集从库无数据变化。
问题原因:
在RESET MASTER前,群集主库和从库的Executed_Gtid_Set为:025fd638-89ea-11e9-a749-40f2e9cf3aaa:1-1300
在RESET MASTER后,群集主库Executed_Gtid_Set被清空,执行的事务GTID值从1开始,这些事务的BINLOG事件传递到从库,由于与从库当前的Executed_Gtid_Set重合,因此判定这些事务的BINLOG事件为“已在从库执行”而被“忽略”,导致主从数据异常。