再谈MySQL全库备份
简介
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,如需转载请自行联系原作者
再谈MySQL全库备份相关推荐
- mysql 全库 备份 恢复_从MySQL全库备份中恢复某个库和某张表
在Mysqldump官方工具中,如何只恢复某个库呢? 全库备份 [root@HE1 ~]# mysqldump -uroot -p --single-transaction -A --master-d ...
- MySQL 数据库备份一键执行脚本 --- 全库备份和增量备份
文章目录 1. 全库备份 2. 增量备份 3. 开启定时任务 1. 全库备份 vim all_db_bak.sh#!/bin/bash #MySQL 全库备份PATH="/usr/local ...
- mysql 快照能否恢复某个表_mysql全库备份恢复某个表
早上小红过来问我说网站的一个功能没了,看了下数据库,少了个表.好吧,心里mmp,开始恢复数据 环境: 全库备份 恢复某一个表 1.1 查看备份数据 [aiye@aiye mysql_backup]$l ...
- MySQL中如何用全库备份恢复单张表
一.概述 模拟场景:下面模拟逻辑备份与物理备份两种方式恢复单表方法,利用完整备份+binlog恢复单表,某天某张表意外删除,如何从全备中恢复单表? 二.安装部署 2.1Mysql安装 (我的模拟环境数 ...
- 魔兽假设把mysql卸了_【原创】假设在有最后一次全库备份之后,你误删除了一张表,请使用备份+归档来将数据库...
topic:[原创]假设在有最后一次全库备份之后,你误删除了一张表,请使用备份+归档来将数据库恢复到删除表之前的状态.(不完全恢复)更多精彩内容尽在www.leonar topic:[原创]假设在有最 ...
- oracle8616,ORACLE11G-数据库备份恢复之RMAN全库备份恢复
文章模拟数据库在有rman全库备份并在备份后有事务产生后数据库崩溃的恢复过程,欢迎交流学习. 1.rman全库备份 RMAN> backup as compressed backupset da ...
- DM8:达梦数据库定时自动全库备份与删除备份
DM8:达梦数据库定时自动全库备份与删除备份 环境介绍 1 备份条件 2 测试手动备份是否成功 2.1 报错 -718 2.2 错误解决方法 方法1: 在数据库执行SQL 方法2:重启 数据库实例服务 ...
- 【原创】rman 全库备份脚本
rman 全库备份脚本 run { allocate channel d1 type disk; allocate channel d2 type disk; backup full database ...
- liunx系统mysql全量备份和增量备份
前提 在互联网项目中最终还是读数据进行操作,都离不开曾删改查,那么数据是重中之重,数据库的备份就显得格外重要. 但是每次都直接导出整个数据库的sql文件,显然是不现实的.对数据库的性能影响比较 ...
最新文章
- C#种死锁:事务(进程 ID 112)与另一个进程被死锁在 锁
- Django之model
- Gui系统之View体系(2)---View的setContent
- python中sendkeys.sendkeys_python的webbrowser模块和SendKeys模块
- 无差异曲线matlab算法,引入Matlab提高经济类线性代数应用能力
- 第5章--电商项目-mysql实战--数据库主从配置
- 使用复制存储过程执行解决“事务复制中的表大量更新导致无法及时同步”的问题 (转)...
- Python个 flask 教程地址 做个记录方便以后学习用
- 电脑排行榜笔记本_热门笔记本电脑排行榜推荐_windows7教程
- python工资一般多少西安-Python面试经验分享——西安贝业思数据
- 在线考试系统源码 在线教育源码
- 平衡小车制作系列之一——捋清制作流程
- [译]eBay Elasticsearch性能调优实践
- html5css3菜鸟教程,HTML5+CSS3实现拖放(Drag and Drop)示例
- 我们怎么保证软件开发的质量?
- STM32显示软件取模图片
- “汉语编程”是解决安全问题的终极之路?
- 【Python 每日一技】建立多个值和单个键的映射
- 自然语言处理(NLP)任务中常用的分词工具及底层算法支持
- java 遍历阿斯克吗_身为一个合格的java开发,这些java集合的知识你得知道,写的很细...