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)

大家都在看

  • Java的try-resource

    原来是这个,这个是之前的try catch,包括最后进行关流等操作。 private String getBodyString(final ServletRequest reque…

    数据库 2023年6月11日
    062
  • JavaScript进阶内容——BOM详解

    JavaScript进阶内容——BOM详解 在上一篇文章中我们学习了DOM,接下来让我们先通过和DOM的对比来简单了解一下BOM 首先我们先来复习一下DOM: 文档对象模型 DOM…

    数据库 2023年6月14日
    0144
  • 【黄啊码】MySQL入门—5、数据库小技巧:单个列group by就会,多个列呢?

    大家好!我是黄啊码,上节课留的作业大家都做了吗?没做的话回去做完再来听课。 好了,先来解答上节课留下的问题: 我们在数据库表中新增一列user_height表示身高,然后拿到所有数…

    数据库 2023年6月16日
    074
  • AI+医疗:使用神经网络进行医学影像识别分析 ⛵

    💡 作者:韩信子@ShowMeAI📘 计算机视觉实战系列:https://www.showmeai.tech/tutorials/46📘 行业名企应用系列:https://www….

    数据库 2023年6月14日
    098
  • MSQL–>存储引擎

    概述 MySQL体系结构图 Innodb引擎是在mysql的5.5版本之后的默认存储引擎。 Index是在引擎层次的,不同的存储引擎index的用法不同。 存储引擎就是存储数据,建…

    数据库 2023年6月14日
    075
  • 索引的树结构

    二分查找 二叉树 二叉平衡树 B-TREE :二叉平衡树的基础上,使加载一次节点,可以加载更多路径数据,同时把查询范围缩减到更小 缺点:业务数据的大小可能远远超过了索引数据的大小,…

    数据库 2023年5月24日
    061
  • MySQL45讲之count操作

    本文介绍 MyISAM 和 InnoDB 如何执行 count 操作,如果是一个需要使用 count 进行大量计数的场景,应该如何设计实现,以及不同 count 操作的效率。 My…

    数据库 2023年5月24日
    080
  • MySQL知识点总结(完整版)

    登录和退出MySQL服务器 &#x767B;&#x5F55;MySQL $ mysql -u root -p12345612 &#x9000;&#x…

    数据库 2023年5月24日
    060
  • 2022年5月数据库排名

    数据来源:https://db-engines.com/en/ranking Original: https://www.cnblogs.com/chujian007/p/1631…

    数据库 2023年6月11日
    074
  • Pod控制器类型

    Pod是kubernetes的最小管理单元,在kubernetes中,按照pod的创建方式可以将其分为两类: 自主式pod:kubernetes直接创建出来的Pod,这种pod删除…

    数据库 2023年6月14日
    069
  • Linux远程终端连接工具:SecureCRT

    SecureCRT SecureCRT是一款支持 SSH2、SSH1、Telnet、Telnet/SSH、Relogin、Serial、TAPI、RAW 等协议的终端仿真程序 Se…

    数据库 2023年6月11日
    094
  • MySQL锁:03.InnoDB行锁

    传送门:MySQL锁:01.总览传送门:MySQL锁:02.InnoDB锁传送门:MySQL锁:03.InnoDB行锁 InnoDB 行锁 锁排查可以用的视图和数据字典 InnoD…

    数据库 2023年6月16日
    0106
  • 有趣的网络知识

    简单的网络入侵方法 命令 描述 attrib +s +a +h +r &#x78C1;&#x76D8;:&#x6587;&#x4EF6;&#…

    数据库 2023年6月11日
    085
  • Java面试题(十)–Spring Cloud

    1 基础知识篇 1、什么是微服务架构? 微服务架构是一种架构模式或者说是架构风格,它提倡将单一应用程序划分成一组小的服务。每个服务运行在其独立的自己的进程中服务之间相互配合、相互协…

    数据库 2023年6月16日
    097
  • MVCC多版本并发控制的理解

    前置知识 当前读与快照读 当前读什么是当前读:读取的是 &#x6700;&#x65B0;的数据,不会读到老数据。 &#x4F55;&#x65F6;&…

    数据库 2023年5月24日
    054
  • 机器学习—神经网络

    BP神经网络的综述 1.1神经网络的定义 神经网络(neural network) 是由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系统对真实世界物体所做…

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