MySQL 学习笔记(五)–mysqldump

mysqldump 与 –set-gtid-purged 设置

(1) mysqldump

The mysqldump client utility performs logical backups, producing a set of SQL statements that can be executed to reproduce the original database object definitions and table data. It dumps one or more MySQL databases for backup or transfer to another SQL server. The mysqldump command can also generate output in CSV, other delimited text, or XML format.

mysqldump requires at least the SELECT privilege for dumped tables, SHOW VIEW for dumped views, TRIGGER for dumped triggers, LOCK TABLES if the –single-transaction option is not used, and (as of MySQL 5.7.31) PROCESS if the –no-tablespaces option is not used.

mysqldump advantages include the convenience and flexibility of viewing or even editing the output before restoring. You can clone databases for development and DBA work, or produce slight variations of an existing database for testing. It is not intended as a fast or scalable solution for backing up substantial amounts of data. With large data sizes, even if the backup step takes a reasonable time, restoring the data can be very slow because replaying the SQL statements involves disk I/O for insertion, index creation, and so on. For large-scale backup and restore, a physical backup is more appropriate, to copy the data files in their original format that can be restored quickly.

(2) –set-gtid-purged=value

This option enables control over global transaction ID (GTID) information written to the dump file, by indicating whether to add a SET @@GLOBAL.gtid_purged statement to the output. This option may also cause a statement to be written to the output that disables binary logging while the dump file is being reloaded. The following table shows the permitted option values.

The default value is AUTO.

Value Meaning OFF Add no SET statement to the output. ON Add a SET statement to the output. An error occurs if GTIDs are not enabled on the server. AUTO Add a SET statement to the output if GTIDs are enabled on the server.

A partial dump from a server that is using GTID-based replication requires the –set-gtidpurged={ON|OFF} option to be specified. Use ON if the intention is to deploy a new replication slave using only some of the data from the dumped server. Use OFF if the intention is to repair a table by copying it within a topology. Use OFF if the intention is to copy a table between replication topologies that are disjoint and will remain so.

(3) –set-gtid-purged 与 导出文件中SET @@SESSION.SQL_LOG_BIN=0

The –set-gtid-purged option has the following effect on binary logging when the dump file is reloaded:

• –set-gtid-purged=OFF: SET @@SESSION.SQL_LOG_BIN=0; is not added to the output.

• –set-gtid-purged=ON: SET @@SESSION.SQL_LOG_BIN=0; is added to the output.

• –set-gtid-purged=AUTO: SET @@SESSION.SQL_LOG_BIN=0; is added to the output if GTIDs are enabled on the server you are backing up (that is, if AUTO evaluates to ON).

(4) 举例说明

在开启GTID模式的实例上执行mysqldump,假如导出命令如下:

/usr/local/mysql/bin/mysqldump --master-data=2 -u账......号 -p密......码 --databases 数据库1 数据库2 --single-transaction -R --triggers > /data/dbdump/db1_db2_dump.sql

执行,我们会收到一个Warning,如下:

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.

命令执行后,查看导出的文件,会在文件的开头看到以下命令:

...................................................................................................。。。。。。。。。。。。。。。
/*!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='66fe6059-18c7-22e6-1d21-000c27cswbda:1908761-14';

--
-- Position to start replication or point-in-time recovery from
--

-- CHANGE MASTER TO MASTER_LOG_FILE='1ogbin-003413', MASTER_LOG_POS=999;

--
...................................................................................................。。。。。。。。。。。。。。。

可以看出,在开启GITD的server上,执行mysqldump,将–set-gtid-purged 设置为on 或者不设置(The default value is AUTO.)时,产生的sql文件,会显式指明(导入时会执行) GLOBAL.GTID_PURGED 和将当前session 设置为 SQL_LOG_BIN= 0。

(5) 场景延申

假设:当前有一个mysql 集群123,一主一从,主为serverA1,从为Server B1,现在又新搭建了一个集群321, 也是一主一从(注意已搭建主从复制)一主一从,主为serverA2,从为Server B2.

目前需求是将集群123 中的部分数据库 –数据库2、数据库4—-同步到新集群321 中,并建立主从。

示意图如下:

MySQL 学习笔记(五)--mysqldump

注意四台机器都已开启GTID模式

我们使用mysqldump来完成这个工作会遇到什么挑战呢?

首先是导出,导出就是上面的命令,没有带–set-gtid-purged。接着时修改ServerA2实例的my.cnf配置文件,指定复制的库。然后,将dump文件copy到新实例serverA2上。

现在我们主要看看导入时遇到的问题。

(1)导入命令

bash;gutter:true; mysql -u用......户......名 -p密......码 < /data/dumprestore/db1_db2_dump.sql</p> <pre><code> 收到报错信息 </code></pre> <p>ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.</p> <pre><code> 解决方案,执行以下命令 </code></pre> <p>reset master;</p> <pre><code> (2) 如果执行了 reset master,Server A2 和 Server B2的主从关系就会坏掉。 (3) 如果在导入前,主从都是没有业务数据库的新集群。主从修复,可以都执行了 reset master,重新搭建主从。 </code></pre> <p>change master to master_host='IP地址', master_port=端口号, master_user='用 ......户 ......名', master_password='密......码', master_auto_position=1;</p> <pre><code> 因为是刚刚reset master ,导入时不会遇到ERROR 1840 ...,,可以将数据灌入到Server A2中。 但是,这个时候你会发现 Server A2 和 Server B2的主从已不能同步新导入的数据。 因为你执行导入的session,执行设定了 **SET @@SESSION.SQL_LOG_BIN= 0;** (4)执行时间过长 建议 nohup,后台执行。 mysql导入数据耗时远远大于mysqldump导出耗时。测试中,我们将大小都是60G的两个数据库备份还原,mysqldump 执行55分钟,mysql导入执行了5个小时。 (5)灌入数据后,可以通过xtraback备份还原来修复重建ServerA2 和 ServerB2的主从。 (6) 如果导入数据时不想破坏掉ServerA2 和 ServerB2的主从关系,可以考虑,导入前先注释掉 **SET @@SESSION.SQL_LOG_BIN= 0;**,再导入。 (7) 另外,如果场景更复杂,例如新集群321 需要承接、同步来自多个集群(不仅仅是来自集群123)的数据,也可以考虑 通过传统模式搭建主从(指定binlog文件+位点),这个场景下,mysqldump 时, --set-gtid-purged 设置为off,或者导出后,考虑将 **SET @@SESSION.SQL_LOG_BIN= 0;和** **SET @@GLOBAL.GTID_PURGED='??????????????';**注释掉。 查看位点的命令: </code></pre> <p>head -n 100 dump文件.sql | grep 'CHANGE MASTER TO'

Original: https://www.cnblogs.com/xuliuzai/p/15426872.html
Author: 东山絮柳仔
Title: MySQL 学习笔记(五)–mysqldump

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/620970/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 代码随想录-数组篇

    上次刷没刷完整,和李哥做字节的题感觉先前刷的题白刷了,故打算从头到尾完整走一遍。 二分法 1-1.二分查找 力扣题目链接 给定一个 n 个元素有序的(升序)整型数组 nums 和一…

    数据库 2023年6月14日
    0103
  • MySQL-配置参数时 报错:remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu……

    报错:remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu…… 原因: 1、第一次配置参数时,不完整…

    数据库 2023年6月14日
    097
  • JUC学习笔记(五)

    创建线程的方法-一种是通过创建 Thread 类,另一种是通过使用 Runnable 创建线程。但是,Runnable 缺少的一项功能是,当线程终止时(即 run()完成时),我们…

    数据库 2023年6月6日
    0127
  • 分享封装好的异步Mysql动态的库(DyNetMysql.dll) + 项目源码

    在做C++项目时,经常会用到Mysql数据库,Mysql接口提供给我们的数据是相当原始的,如:字段名、字段类型,字段长度等等,一般情况我们都想一种更方便获得数据 如: XXXStr…

    数据库 2023年6月14日
    080
  • 「萌新指南」SOA vs. 微服务:What’s the Difference?

    实话实说,在我还没有实习之前,我是连 SOA 是啥都不知道的,只听说过微服务,毕竟微服务实在太火了,想不知道都难,我觉得实习的时候肯定也是微服务,进组之后发现是 SOA 架构,当时…

    数据库 2023年6月6日
    0106
  • mysql的半同步复制

    binlog dump线程何时向从库发送binlog mysql在server层进行了组提交之后,为了提高并行度,将提交阶段分为了 flush sync commit三个阶段,根据…

    数据库 2023年6月9日
    085
  • 手把手教你使用 Java 在线生成 pdf 文档

    一、介绍 在实际的业务开发的时候,研发人员往往会碰到很多这样的一些场景,需要提供相关的电子凭证信息给用户,例如网银/支付宝/微信购物支付的电子发票、订单的库存打印单、各种电子签署合…

    数据库 2023年6月14日
    0113
  • 钻石价格预测的ML全流程!从模型构建调优道部署应用!⛵

    💡 作者:韩信子@ShowMeAI📘 数据分析 ◉ 技能提升系列:http://www.showmeai.tech/tutorials/33📘 AI 面试题库系列:http://w…

    数据库 2023年6月15日
    090
  • 猴子吃桃(递归)

    递归案例实践分析 猴子偷桃 题目描述: 猴子第一天摘下若干桃子,当即吃了一半,觉得好吃不过瘾,于是又多吃了一个,,第二天又吃了前天剩余桃子数量的一半,觉得好不过瘾,于是又多吃了一个…

    数据库 2023年6月16日
    0157
  • MySQL 主从同步延迟监控

    MySQL5.7和8.0支持通过 replication_applier_status 表获同步延迟时间,当从库出现延迟后,该表中的字段 REMAINING_DELAY 记录延迟秒…

    数据库 2023年6月11日
    0103
  • 如何设计一个更通用的查询接口

    临近放假,手头的事情没那么多,老是摸鱼也不好,还是写写博客吧。 今天来聊聊: 如何设计一个通用的查询接口。 首先,我们从一个简单的场景开始。现在,我需要一个订单列表,用来查询【我的…

    数据库 2023年6月6日
    099
  • 如何优化前端性能?

    导读:随着前端的范畴逐渐扩大,深度逐渐下沉,富前端必然带来的一个问题就是性能。特别是在大型复杂项目中,重前端业务可能因为一个小小的数据依赖,导致整个页面卡顿甚至崩溃。本文基于Qui…

    数据库 2023年6月14日
    068
  • Java绘图基础

    Graphics 绘图类 Graphic是一个抽象的画笔对象,可以在组件上绘制丰富多彩的几何图形和位图。Graphics类封装了Java支持的基本绘图操作所需的属性,主要包括 颜色…

    数据库 2023年6月16日
    077
  • SpringMvc(三)- CRUD

    1、springMvc的form表单 1.1 标签 1.2 标签 使用springMvc的form表单,快速开发表单及数据自动回显; 原理:在 数据模型中添加一个 参数名为 com…

    数据库 2023年6月16日
    096
  • Java中的命名规则

    在查找java命名规则时,未在国内相关网站查找到较为完整的文章,这是一篇国外程序开发人员写的java命名规则的文章,原文是英文写的,为了便于阅读,遂翻译为汉语,以便帮助国内开发者有…

    数据库 2023年6月11日
    091
  • 线程简介

    线程简介以多线程在Windows操作系统中的运行模式为例:Windows操作系统是 多任务操作系统,它以进程为 单位。每个独立执行的程序都被称为 进程( 比如正在运行的QQ是一个进…

    数据库 2023年6月16日
    0124
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球