再谈MySQL全库备份

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

简介

Part1:写在最前

在很早之前,我写过一个MySQL生产库全库备份脚本,今天有同事问我是不是要再加一个-R参数来备份存储过程,理由的话是由于mysqldump --help中 关于存储过程的默认备份是false。

routines                          FALSE



MySQL生产库全库备份脚本

http://suifu.blog.51cto.com/9167728/1758022



实战

Part1:写在最前

我备份一般就三个参数

--single-transaction -A --master-data=2

分别是预防锁,全库备份和记录复制信息


有人问单库怎么恢复?可以移步

从MySQL全库备份中恢复某个库和某张表

http://suifu.blog.51cto.com/9167728/1830651


Part2:创建存储过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@HE3 ~] # mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 7
Server version: 5.6.25-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2015, Oracle and /or  its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and /or  its
affiliates. Other names may be trademarks of their respective
owners.
 
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
 
mysql> create database helei;
Query OK, 1 row affected (0.02 sec)
 
mysql> use helei;
Database changed
mysql> create table helei(
     ->  id  int(10) unsigned NOT NULL AUTO_INCREMENT,
     -> c1 int(10) NOT NULL DEFAULT  '0' ,
     -> c2 int(10) unsigned DEFAULT NULL,
     -> c5 int(10) unsigned NOT NULL DEFAULT  '0' ,
     -> c3 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     -> c4 varchar(200) NOT NULL DEFAULT  '' ,
     -> PRIMARY KEY( id ),
     -> KEY idx_c1(c1),
     -> KEY idx_c2(c2)
     -> )ENGINE=InnoDB ;
Query OK, 0 rows affected (0.02 sec)
 
mysql> delimiter $$
mysql> drop procedure  if  exists `insert_helei` $$
and()*row_num),now(),repeat( 'su' , floor(rand()*20)));
set  i = i+1;
  END  while ;
 
end$$Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> create procedure `insert_helei`( in  row_num int )
     -> begin
     ->   declare  i int  default 0;
     ->   while  i < row_num  do
     -> insert into helei(c1, c2, c5,c3, c4) values( floor(rand()*row_num),floor(rand()*row_num),floor(rand()*row_num),now(),repeat( 'su' , floor(rand()*20)));
     ->  set  i = i+1;
     ->  END  while ;
     -> 
     -> end$$
Query OK, 0 rows affected (0.00 sec)
 
mysql>



Part3:执行不同的备份脚本

参数分别是

--single-transaction -A --master-data=2

--single-transaction -A -R  --master-data=2


Part4:对比SQL文件

可以发现加了-R的文件中记录了创建存储过程的SQL语句,没加-R的没有记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- Dumping routines  for  database  'helei'
--
/*!50003 DROP PROCEDURE IF EXISTS `insert_helei` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              =  'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'  */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_helei`( in  row_num int )
begin
  declare  i int  default 0;
  while  i < row_num  do
insert into helei(c1, c2, c5,c3, c4) values( floor(rand()*row_num),floor(rand()*row_num),floor(rand()*row_num),now(),repeat( 'su' , floor(rand()*20)));
set  i = i+1;
  END  while ;
end ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;




验证

Part1:验证

清空linux环境,重装mysql,导入不带-R参数即不带创建存储过程SQL的备份文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@HE3 ~] # mysql -uroot -p < Master_db_201612051722.sql 
Enter password: 
[root@HE3 ~] # mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 9
Server version: 5.6.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and /or  its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and /or  its
affiliates. Other names may be trademarks of their respective
owners.
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
mysql> use helei;
Reading table information  for  completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>  select  * from helei;
Empty  set  (0.00 sec)
mysql> call insert_helei(5);
Query OK, 1 row affected (0.01 sec)
mysql>  select  * from helei;
+----+----+------+----+---------------------+------------------------------------+
id  | c1 | c2   | c5 | c3                  | c4                                 |
+----+----+------+----+---------------------+------------------------------------+
|  1 |  1 |    2 |  0 | 2016-12-05 17:51:37 | sususususususususususu             |
|  2 |  2 |    1 |  0 | 2016-12-05 17:51:37 | sususususususususususususususususu |
|  3 |  4 |    1 |  3 | 2016-12-05 17:51:37 | sususususususususususu             |
|  4 |  3 |    2 |  3 | 2016-12-05 17:51:37 | sususususususususususu             |
|  5 |  0 |    2 |  4 | 2016-12-05 17:51:37 | sususususususususu                 |
+----+----+------+----+---------------------+------------------------------------+
5 rows  in  set  (0.00 sec)
mysql>


发现即便没有添加-R参数,也依然可以调用之前创建的存储过程。




——总结——

存储过程存储在mysql.proc表中,而笔者由于采用了-A全库备份的策略,即包含mysql库,所以存储过程也得以备份。-R参数一般用于单库备份或者多库备份。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。



简介

Part1:写在最前

在很早之前,我写过一个MySQL生产库全库备份脚本,今天有同事问我是不是要再加一个-R参数来备份存储过程,理由的话是由于mysqldump --help中 关于存储过程的默认备份是false。

routines                          FALSE



MySQL生产库全库备份脚本

http://suifu.blog.51cto.com/9167728/1758022



实战

Part1:写在最前

我备份一般就三个参数

--single-transaction -A --master-data=2

分别是预防锁,全库备份和记录复制信息


有人问单库怎么恢复?可以移步

从MySQL全库备份中恢复某个库和某张表

http://suifu.blog.51cto.com/9167728/1830651


Part2:创建存储过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@HE3 ~] # mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 7
Server version: 5.6.25-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2015, Oracle and /or  its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and /or  its
affiliates. Other names may be trademarks of their respective
owners.
 
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
 
mysql> create database helei;
Query OK, 1 row affected (0.02 sec)
 
mysql> use helei;
Database changed
mysql> create table helei(
     ->  id  int(10) unsigned NOT NULL AUTO_INCREMENT,
     -> c1 int(10) NOT NULL DEFAULT  '0' ,
     -> c2 int(10) unsigned DEFAULT NULL,
     -> c5 int(10) unsigned NOT NULL DEFAULT  '0' ,
     -> c3 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     -> c4 varchar(200) NOT NULL DEFAULT  '' ,
     -> PRIMARY KEY( id ),
     -> KEY idx_c1(c1),
     -> KEY idx_c2(c2)
     -> )ENGINE=InnoDB ;
Query OK, 0 rows affected (0.02 sec)
 
mysql> delimiter $$
mysql> drop procedure  if  exists `insert_helei` $$
and()*row_num),now(),repeat( 'su' , floor(rand()*20)));
set  i = i+1;
  END  while ;
 
end$$Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> create procedure `insert_helei`( in  row_num int )
     -> begin
     ->   declare  i int  default 0;
     ->   while  i < row_num  do
     -> insert into helei(c1, c2, c5,c3, c4) values( floor(rand()*row_num),floor(rand()*row_num),floor(rand()*row_num),now(),repeat( 'su' , floor(rand()*20)));
     ->  set  i = i+1;
     ->  END  while ;
     -> 
     -> end$$
Query OK, 0 rows affected (0.00 sec)
 
mysql>



Part3:执行不同的备份脚本

参数分别是

--single-transaction -A --master-data=2

--single-transaction -A -R  --master-data=2


Part4:对比SQL文件

可以发现加了-R的文件中记录了创建存储过程的SQL语句,没加-R的没有记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- Dumping routines  for  database  'helei'
--
/*!50003 DROP PROCEDURE IF EXISTS `insert_helei` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              =  'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'  */ ;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_helei`( in  row_num int )
begin
  declare  i int  default 0;
  while  i < row_num  do
insert into helei(c1, c2, c5,c3, c4) values( floor(rand()*row_num),floor(rand()*row_num),floor(rand()*row_num),now(),repeat( 'su' , floor(rand()*20)));
set  i = i+1;
  END  while ;
end ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;




验证

Part1:验证

清空linux环境,重装mysql,导入不带-R参数即不带创建存储过程SQL的备份文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@HE3 ~] # mysql -uroot -p < Master_db_201612051722.sql 
Enter password: 
[root@HE3 ~] # mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 9
Server version: 5.6.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and /or  its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and /or  its
affiliates. Other names may be trademarks of their respective
owners.
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
mysql> use helei;
Reading table information  for  completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>  select  * from helei;
Empty  set  (0.00 sec)
mysql> call insert_helei(5);
Query OK, 1 row affected (0.01 sec)
mysql>  select  * from helei;
+----+----+------+----+---------------------+------------------------------------+
id  | c1 | c2   | c5 | c3                  | c4                                 |
+----+----+------+----+---------------------+------------------------------------+
|  1 |  1 |    2 |  0 | 2016-12-05 17:51:37 | sususususususususususu             |
|  2 |  2 |    1 |  0 | 2016-12-05 17:51:37 | sususususususususususususususususu |
|  3 |  4 |    1 |  3 | 2016-12-05 17:51:37 | sususususususususususu             |
|  4 |  3 |    2 |  3 | 2016-12-05 17:51:37 | sususususususususususu             |
|  5 |  0 |    2 |  4 | 2016-12-05 17:51:37 | sususususususususu                 |
+----+----+------+----+---------------------+------------------------------------+
5 rows  in  set  (0.00 sec)
mysql>


发现即便没有添加-R参数,也依然可以调用之前创建的存储过程。




——总结——

存储过程存储在mysql.proc表中,而笔者由于采用了-A全库备份的策略,即包含mysql库,所以存储过程也得以备份。-R参数一般用于单库备份或者多库备份。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。



 本文转自 dbapower 51CTO博客,原文链接:http://blog.51cto.com/suifu/1879662,如需转载请自行联系原作者






相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
关系型数据库 MySQL API
Flink CDC产品常见问题之mysql整库同步到starrock时任务挂掉如何解决
Flink CDC(Change Data Capture)是一个基于Apache Flink的实时数据变更捕获库,用于实现数据库的实时同步和变更流的处理;在本汇总中,我们组织了关于Flink CDC产品在实践中用户经常提出的问题及其解答,目的是辅助用户更好地理解和应用这一技术,优化实时数据处理流程。
|
1月前
|
分布式计算 DataWorks 关系型数据库
DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
【2月更文挑战第14天】DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
56 8
|
2月前
|
分布式计算 DataWorks 关系型数据库
DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
30 4
|
2月前
|
SQL 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
188 7
|
2天前
|
SQL 关系型数据库 MySQL
用MySQL创建公司资料库表格
创建了员工、分支、客户及工作关系的数据库表格。员工与分支间有works_with表记录销售数据,外键关联并处理删除操作(set null或cascade)。插入数据后,通过SQL查询获取员工、客户信息,使用聚合函数、通配符、联合查询和JOIN操作。子查询用于复杂条件筛选。数据库设计确保了数据完整性和参照完整性。
8 0
|
4天前
|
关系型数据库 MySQL
MySQL全局库表查询准确定位字段
information_schema.COLUMNS 详细信息查询
|
1月前
|
关系型数据库 MySQL API
Flink CDC产品常见问题之mysql整库同步到starrock时任务挂掉如何解决
Flink CDC产品常见问题之mysql整库同步到starrock时任务挂掉如何解决
|
2月前
|
关系型数据库 MySQL Linux
Linux环境下定时备份mysql数据库
Linux环境下定时备份mysql数据库
|
2月前
|
存储 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
21 7
|
2月前
|
监控 容灾 安全
规划阿里云RDS跨区迁移并构建容灾与备份策略
规划阿里云RDS(Relational Database Service)跨区迁移并构建容灾与备份策略
109 2