Preface
 
    The day before yesterday,there's a motif about the lock procedure when backing up MySQL using mysqldump or Xtrabackup in "Ask Ye" which is like a kind of Q&A originated by Mr. Ye.In my essay today,I'm gonna use the tool mysqlsump to do some tests then observe procedures of backing up MySQL database,here we go.
 
Introduction
 
    mysqldump is a protogenic logical backup tool of MySQL.As soon as you've installed MySQL server,it can be used in command line directly.
    mysqldump logically dumps data in the target database then gernerate a sql file which can be used to restore the the very database later.it's rather convenient to backup a single or multiple databases online(innodb only) but the side-effect is that it can hold lock when backing up.
 
Procedure
 
    In order to see the intact procedure of backup,we open the general log by setting "general_log=on"  in runtime first.
 
 ###Open General Log###
(root@localhost mysql3306.sock)[performance_schema]::>show variables like '%gener%';
+------------------+-------------------------------------+
| Variable_name | Value |
+------------------+-------------------------------------+
| general_log | OFF |
| general_log_file | /data/mysql/mysql3306/data/zlm2.log |
+------------------+-------------------------------------+
rows in set (0.00 sec) (root@localhost mysql3306.sock)[performance_schema]::>set general_log=on;
ERROR (HY000): Variable 'general_log' is a GLOBAL variable and should be set with SET GLOBAL
(root@localhost mysql3306.sock)[performance_schema]::>set global general_log=on;
Query OK, rows affected (0.14 sec) ###Create innodb table and myisam table in database zlm.###
(root@localhost mysql3306.sock)[zlm]::>create table test_innodb(id int primary key) engine=innodb;
Query OK, rows affected (0.02 sec) (root@localhost mysql3306.sock)[zlm]::>create table test_myisam(id int primary key) engine=myisam;
Query OK, rows affected (0.01 sec) (root@localhost mysql3306.sock)[zlm]::>show tables;
+---------------+
| Tables_in_zlm |
+---------------+
| test_innodb |
| test_myisam |
+---------------+
rows in set (0.00 sec) (root@localhost mysql3306.sock)[zlm]::> ###Gnerate First Backup(disable triggers)###
[root@zlm2 :: ~]
#mysqldump --triggers=false -B zlm > /data/backup/first_3306-`date +%Y%m%d`.sql
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. [root@zlm2 :: ~]
#ls -l /data/backup
total
-rw-r--r-- root root Jun : first_3306-.sql ###Check General Log###
[root@zlm2 :: /data/mysql/mysql3306/data]
#cat zlm2.log --15T07::.345850Z Connect root@localhost on using Socket
--15T07::.346193Z Query /*!40100 SET @@SQL_MODE='' */
--15T07::.346252Z Query /*!40103 SET TIME_ZONE='+00:00' */
--15T07::.346332Z Query SHOW VARIABLES LIKE 'gtid\_mode'
--15T07::.348422Z Query SELECT @@GLOBAL.GTID_EXECUTED
--15T07::.350309Z Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('zlm'))) GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, INITIAL_SIZE ORDER BY LOGFILE_GROUP_NAME
--15T07::.351603Z Query SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('zlm')) ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME
--15T07::.352151Z Query SHOW VARIABLES LIKE 'ndbinfo\_version'
--15T07::.354634Z Init DB zlm
--15T07::.354659Z Query SHOW CREATE DATABASE IF NOT EXISTS `zlm`
--15T07::.354660Z Query show tables
--15T07::.354747Z Query LOCK TABLES `test_innodb` READ /*!32311 LOCAL */,`test_myisam` READ /*!32311 LOCAL */
--15T07::.354815Z Query show table status like 'test\_innodb'
--15T07::.355067Z Query SET SQL_QUOTE_SHOW_CREATE=
--15T07::.355110Z Query SET SESSION character_set_results = 'binary'
--15T07::.355144Z Query show create table `test_innodb`
--15T07::.355188Z Query SET SESSION character_set_results = 'utf8'
--15T07::.355227Z Query show fields from `test_innodb`
--15T07::.355412Z Query show fields from `test_innodb`
--15T07::.355631Z Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `test_innodb`
--15T07::.356449Z Query show table status like 'test\_myisam'
--15T07::.356723Z Query SET SQL_QUOTE_SHOW_CREATE=
--15T07::.356759Z Query SET SESSION character_set_results = 'binary'
--15T07::.356819Z Query show create table `test_myisam`
--15T07::.356863Z Query SET SESSION character_set_results = 'utf8'
--15T07::.356900Z Query show fields from `test_myisam`
--15T07::.357109Z Query show fields from `test_myisam`
--15T07::.357349Z Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `test_myisam`
--15T07::.357420Z Query UNLOCK TABLES --Release the lock only after all tables have finished backup.
--15T07::.361654Z Quit
    I've make the output be simplest by disable triggers' backup.We can see from general log is that "LOCK TABLES test_innodb READ" then the "UNLOCK TABLES" statment.Let's see the difference with parameter "single-transaction".
 
 ###Gnerate Sceond Backup(enable transaction consistent backup)###
[root@zlm2 :: ~]
#mysqldump --triggers=false --single-transaction -B zlm > /data/backup/second_3306-`date +%Y%m%d`.sql
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. [root@zlm2 :: ~]
#ls -l /data/backup
total
-rw-r--r-- root root Jun : first_3306-.sql
-rw-r--r-- root root Jun : second_3306-.sql ###Check General Log###
[root@zlm2 :: /data/mysql/mysql3306/data]
#cat zlm2.log --15T08::.911566Z Connect root@localhost on using Socket
--15T08::.911733Z Query /*!40100 SET @@SQL_MODE='' */
--15T08::.911848Z Query /*!40103 SET TIME_ZONE='+00:00' */
--15T08::.912749Z Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
--15T08::.912839Z Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
--15T08::.912919Z Query SHOW VARIABLES LIKE 'gtid\_mode'
--15T08::.915228Z Query SELECT @@GLOBAL.GTID_EXECUTED
--15T08::.915371Z Query UNLOCK TABLES --Release lock here untill end,there're no more locks.
--15T08::.915568Z Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('zlm'))) GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, INITIAL_SIZE ORDER BY LOGFILE_GROUP_NAME
--15T08::.916737Z Query SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('zlm')) ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME
--15T08::.918498Z Query SHOW VARIABLES LIKE 'ndbinfo\_version'
--15T08::.920665Z Init DB zlm
--15T08::.920742Z Query SHOW CREATE DATABASE IF NOT EXISTS `zlm`
--15T08::.920787Z Query SAVEPOINT sp --Notice,there's a save opoint here.
--15T08::.920837Z Query show tables
--15T08::.921068Z Query show table status like 'test\_innodb'
--15T08::.921242Z Query SET SQL_QUOTE_SHOW_CREATE=
--15T08::.921290Z Query SET SESSION character_set_results = 'binary'
--15T08::.921334Z Query show create table `test_innodb`
--15T08::.921397Z Query SET SESSION character_set_results = 'utf8'
--15T08::.921444Z Query show fields from `test_innodb`
--15T08::.921833Z Query show fields from `test_innodb`
--15T08::.922279Z Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `test_innodb`
--15T08::.922380Z Query ROLLBACK TO SAVEPOINT sp --Rollback to savepoint sp
--15T08::.922487Z Query show table status like 'test\_myisam'
--15T08::.922694Z Query SET SQL_QUOTE_SHOW_CREATE=
--15T08::.922730Z Query SET SESSION character_set_results = 'binary'
--15T08::.922763Z Query show create table `test_myisam`
--15T08::.922872Z Query SET SESSION character_set_results = 'utf8'
--15T08::.922929Z Query show fields from `test_myisam`
--15T08::.923140Z Query show fields from `test_myisam`
--15T08::.923395Z Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `test_myisam`
--15T08::.923459Z Query ROLLBACK TO SAVEPOINT sp --Rollback to savepoint sp.
--15T08::.923487Z Query RELEASE SAVEPOINT sp --Release it where backup finish.
--15T08::.928411Z Quit
    There is only one piece of "UNLOCK TABLES"  which is related with lock can be found this time.At the very beginning of the general log,it shows "START TRANSACTION WITH CONSISTENT SNAPSHOT".As soon as it gets the gtid_executed variable,then it will realse the lock by execute statment "UNLOCK TABLES".Obviously,it's a very short time.
    After get the statment of creating database,there's a savepoint created.The rollback operation of the savepoint hapens after it gets the full table backup.Let's see another parameter "master-data" which can make something different,too.
 
 ###Gnerate Third Backup(add replication information)###
[root@zlm2 :: ~]
#mysqldump --triggers=false --single-transaction --master-data= -B zlm > /data/backup/third_3306-`date +%Y%m%d`.sql
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. [root@zlm2 :: ~]
#ls -l /data/backup
total
-rw-r--r-- root root Jun : first_3306-.sql
-rw-r--r-- root root Jun : second_3306-.sql
-rw-r--r-- root root Jun : third_3306-.sql ###Check General Log###
[root@zlm2 :: /data/mysql/mysql3306/data]
#cat zlm2.log --15T08::.848849Z Connect root@localhost on using Socket
--15T08::.849052Z Query /*!40100 SET @@SQL_MODE='' */
--15T08::.849153Z Query /*!40103 SET TIME_ZONE='+00:00' */
--15T08::.849290Z Query FLUSH /*!40101 LOCAL */ TABLES -- Difference .
--15T08::.855139Z Query FLUSH TABLES WITH READ LOCK -- Difference .
--15T08::.855196Z Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
--15T08::.855225Z Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */
--15T08::.855272Z Query SHOW VARIABLES LIKE 'gtid\_mode'
--15T08::.857074Z Query SELECT @@GLOBAL.GTID_EXECUTED
--15T08::.857406Z Query SHOW MASTER STATUS
--15T08::.857498Z Query UNLOCK TABLES
--15T08::.857622Z Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('zlm'))) GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, INITIAL_SIZE ORDER BY LOGFILE_GROUP_NAME
--15T08::.858662Z Query SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('zlm')) ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME
--15T08::.859309Z Query SHOW VARIABLES LIKE 'ndbinfo\_version'
--15T08::.861396Z Init DB zlm
--15T08::.862152Z Query SHOW CREATE DATABASE IF NOT EXISTS `zlm`
--15T08::.862255Z Query SAVEPOINT sp
--15T08::.862322Z Query show tables
--15T08::.862485Z Query show table status like 'test\_innodb'
--15T08::.862665Z Query SET SQL_QUOTE_SHOW_CREATE=
--15T08::.862724Z Query SET SESSION character_set_results = 'binary'
--15T08::.862777Z Query show create table `test_innodb`
--15T08::.862827Z Query SET SESSION character_set_results = 'utf8'
--15T08::.862880Z Query show fields from `test_innodb`
--15T08::.863198Z Query show fields from `test_innodb`
--15T08::.863476Z Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `test_innodb`
--15T08::.863597Z Query ROLLBACK TO SAVEPOINT sp
--15T08::.863668Z Query show table status like 'test\_myisam'
--15T08::.865590Z Query SET SQL_QUOTE_SHOW_CREATE=
--15T08::.865833Z Query SET SESSION character_set_results = 'binary'
--15T08::.865853Z Query show create table `test_myisam`
--15T08::.865853Z Query SET SESSION character_set_results = 'utf8'
--15T08::.865854Z Query show fields from `test_myisam`
--15T08::.866059Z Query show fields from `test_myisam`
--15T08::.867277Z Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `test_myisam`
--15T08::.867367Z Query ROLLBACK TO SAVEPOINT sp
--15T08::.867398Z Query RELEASE SAVEPOINT sp
--15T08::.869643Z Quit
    There're two differences above,one is "FLUSH TABLES" another one is "FLUSH TABLE WITH READ LOCK".In case of other transactoins hold the table lock related with the table to be backed up,the backup operation will be blocked until those transactions release the lock or waiting until time out(according to value of "lock_wait_timeout") occurs.
    As a matter of fact,we usually use "master-data" to backup our databases,we shoudn't make it happen in the case of there're massive dml operations continueously executed in rush hour.This will lead to failure of your backup.
 
Summary
  •     You've been clear about the whole procedure of backup using mysqldump now.But,there's still one thing make me confused is that why I used "master-data" in mysqldump but not got the change master statement.Is it due to my Group Replication environment?
  •     Look out,this tool doesnot support backing up in parallel mode.If your database is huge enough,do think twice or use another backup tool in stead of it. 
 

MySQL备份恢复之mysqldump的更多相关文章

  1. Mysql 备份恢复之 Mysqldump 工具

    目前正在学习中,看到mysqldump工具导出的数据都是文本形式的,如果是blob或text大对象类型导出的是什么格式的?这个需要后续研究.下面只先总结下简单的. 一.备份1.备份Mysql一个数据库 ...

  2. MySQL 备份脚本--mysqldump在slave 上进行备份

    MySQL 备份脚本--mysqldump在slave 上进行备份 使用mysqldump在slave上进行备份,建议使用stop slave sql_thread,start slave sql_t ...

  3. MySQL备份还原——mysqldump工具介绍

    mysqldump是一款MySQL逻辑备份的工具,他将数据库里面的对象(表)导出成SQL脚本文件.有点类似于SQL SEVER的"任务-生成脚本"的逻辑备份功能.mysqldump ...

  4. Mysql备份工具mysqldump和mysqlhotcopy

    (1).Mysql备份类型 1)按照备份时对数据库的影响分为 Hot backup(热备):也叫在线备份.指在数据库运行中直接备份,对正在运行的数据库没有任何影响. Cold backup(冷备):也 ...

  5. MySQL备份命令mysqldump参数说明与示例

    1. 语法选项说明 -h, --host=name主机名 -P[ port_num], --port=port_num用于连接MySQL服务器的的TCP/IP端口号 --master-data这个选项 ...

  6. Mysql备份迁移——Mysqldump(.NET调用Mysqldump.exe方式)——(解决视图嵌视图报错)

    利用Mysqldump备份和迁移,我想很多人都用过,具体参数不介绍了,这里主要讲.NET调用Mysqldump进行备份和.NET调用Mysql.exe进行导入数据. 这里使用的是5.1版的Mysqld ...

  7. MySQL备份恢复-mysqldump原理

    +++++++++++++++++++++++++++++++++++++++++++标题:mysqldump对MySQL数据库备份恢复原理时间:2019年2月23日内容:mysqldump工具重点: ...

  8. mysql备份脚本-mysqldump

    背景:全库备份 备份流程: 1.生成DB列表,将DB名字写入文件 2.定义备份函数,结果写入SQL文件 3.压缩文件,减少磁盘占用量 4.设置保留天数,定期删除n天之前的 5.通过for循环读取DB列 ...

  9. MySQL备份恢复之mydumper

      Preface       In my previous two blogs,we have known about the tool of backing up MySQL db.I'm gon ...

随机推荐

  1. 有关satement与preparedstatement

    satement 用于写入数据,例子如下: connection conn=DBHelper.getConnection(); Statement stmt=conn.createStatement( ...

  2. Stage6--Python简单爬虫

    正则表达式简单介绍 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 字符 ...

  3. 【Mood-15】DailyBuild 1月

    keywords: AsyncImageLoader universal-image-loader 2015-01-07 AsyncImageLoader:异步动态加载网络图片  类似listview ...

  4. ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 4、安装 ArcGIS for Server

    安装ArcGIS for Server 解压server安装包,tar -xzvf ArcGIS_Server_Linux_1051_156429.tar.gz 切换到arcgis账户静默安装serv ...

  5. keras 保存模型

    转自:https://blog.csdn.net/u010159842/article/details/54407745,感谢分享! 我们不推荐使用pickle或cPickle来保存Keras模型 你 ...

  6. Siebel计划和实施

    1.计划: 自上而下计划配置项目 1)首先,确定UI和应用产品功能 2)然后,确定为实现UI功能而需要在业务层所做的更改 3)最后,确定为实现业务层更改而需要在数据层所做的更改---尽可能少做更改 如 ...

  7. Struts2学习-拦截器

    1.新建项目user4,建立好和user3一样的目录,与之相比只是添加几个类,主要是struts.xml和action类的改变,其结果没有太大的变化 struts,xml <?xml versi ...

  8. idea打jar包经验总结

    关于在idea下打jar问题,在日常工作中经常用到,这里总结下流程. 1.在项目上鼠标右键 --> Open Module Settings 2.如下图,点击 '+' 3. 选择JAR --&g ...

  9. Oracle数据库克隆后temp文件因路径变化无法找到问题

    Oracle数据库克隆后temp文件因路径变化无法找到出现如下报错Errors in filexxxx.trc:ORA-01157: cannot identify/lock data file xx ...

  10. May 28th 2017 Week 22nd Sunday

    Behind every beautiful thing, there's some kind of pain. 美丽背后,必有某种努力. No pains, no gains. Maybe we n ...