mysql dba系统学习(16)mysql的mysqldump备份

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

                mysql数据库的备份恢复

mysqldump备份数据库

 -B, --databases     Dump several databases. Note the difference in usage; in

                     this case no tables are given. All name arguments are

                     regarded as database names. 'USE db_name;' will be

                     included in the output.


 -e, --extended-insert

                     Use multiple-row INSERT syntax that include several

                     VALUES lists.  多行插入数据


    为了保证数据的一致性,我们要把表锁起来在dump

-F, --flush-logs    Flush logs file in server before starting dump. Note that

                     if you dump many databases at once (using the option

                     --databases= or --all-databases), the logs will be

                     flushed for each database dumped. The exception is when

                     using --lock-all-tables or --master-data: in this case

                     the logs will be flushed only once, corresponding to the

                     moment all tables are locked. So if you want your dump

                     and the log flush to happen at the same exact moment you

                     should use --lock-all-tables or --master-data with

                     --flush-logs.

               

-x, --lock-all-tables

                     Locks all tables across all databases. This is achieved

                     by taking a global read lock for the duration of the

                     whole dump. Automatically turns --single-transaction and

                     --lock-tables off.

 -l, --lock-tables   Lock all tables for read.



--master-data[=#]   This causes the binary log position and filename to be

                     appended to the output. If equal to 1, will print it as a

                     CHANGE MASTER command; if equal to 2, that command will

                     be prefixed with a comment symbol. This option will turn

                     --lock-all-tables on, unless --single-transaction is

                     specified too (in which case a global read lock is only

                     taken a short time at the beginning of the dump; don't

                     forget to read about --single-transaction below). In all

                     cases, any action on logs will happen at the exact moment

                     of the dump. Option automatically turns --lock-tables

                     off.


-t, --no-create-info

                     Don't write table creation info.

 -d, --no-data       No row information.

 -N, --no-set-names  Suppress the SET NAMES statement

--opt               Same as --add-drop-table, --add-locks, --create-options,

                     --quick, --extended-insert, --lock-tables, --set-charset,

                     and --disable-keys. Enabled by default, disable with

                     --skip-opt.


 -q, --quick         Don't buffer query, dump directly to stdout.   不缓存



 -R, --routines      Dump stored routines (functions and procedures).



 --single-transaction

                     Creates a consistent snapshot by dumping all tables in a

                     single transaction. Works ONLY for tables stored in

                     storage engines which support multiversioning (currently

                     only InnoDB does); the dump is NOT guaranteed to be

                     consistent for other storage engines. While a

                     --single-transaction dump is in process, to ensure a

                     valid dump file (correct table contents and binary log

                     position), no other connection should use the following

                     statements: ALTER TABLE, DROP TABLE, RENAME TABLE,

                     TRUNCATE TABLE, as consistent snapshot is not isolated

                     from them. Option automatically turns off --lock-tables.


 --dump-date         Put a dump date to the end of the output.


--skip-opt          Disable --opt. Disables --add-drop-table, --add-locks,

                     --create-options, --quick, --extended-insert,

                     --lock-tables, --set-charset, and --disable-keys.

实践之非事务性一直备份(备份期间数据库不可写)

mysql> use  test

mysql> create table tt(id int,name varchar(12));

Query OK, 0 rows affected (0.11 sec)

mysql> insert into tt values(1,'zz');

Query OK, 1 row affected (0.01 sec)

mysql> insert into tt values(2,'yy');

Query OK, 1 row affected (0.00 sec)


[root@test4 Desktop]# mysqldump  --databases test  --skip-opt --quick --extended-insert=false --lock-all-tables --master-data=2  -u root -p123456 >  /tmp/test.sql

  这就是dump的结果

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
[root@test4 Desktop]# cat /tmp/test.sql
-- MySQL dump  10.13   Distrib  5.1 . 70 for  unknown-linux-gnu (x86_64)
--
-- Host: localhost    Database: test
-- ------------------------------------------------------
-- Server version    5.1 . 70 -log
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */ ;
/*!40103 SET TIME_ZONE='+00:00' */ ;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */ ;
/*!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 */ ;
--
-- Position to start replication or point- in -time recovery from
--
-- CHANGE MASTER TO MASTER_LOG_FILE= 'mysqlbin.000166' , MASTER_LOG_POS= 798 ;
--
-- Current Database: `test`
--
CREATE DATABASE  /*!32312 IF NOT EXISTS*/  `test`  /*!40100 DEFAULT CHARACTER SET utf8 */ ;
USE `test`;
--
-- Table structure  for  table `tt`
--
/*!40101 SET @saved_cs_client     = @@character_set_client */ ;
/*!40101 SET character_set_client = utf8 */ ;
CREATE TABLE `tt` (
   `id`  int ( 11 ) DEFAULT NULL,
   `name`  var char( 12 ) DEFAULT NULL
);
/*!40101 SET character_set_client = @saved_cs_client */ ;
--
-- Dumping data  for  table `tt`
--
INSERT INTO `tt` VALUES ( 1 , 'zz' );
INSERT INTO `tt` VALUES ( 2 , 'yy' );
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */ ;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */ ;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */ ;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */ ;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */ ;
-- Dump completed on  2013 - 09 - 05  20 : 15 : 38

     由于我们dump的时候用了master-data参数,这个时候记录了日志位置和日志文件名

-- CHANGE MASTER TO MASTER_LOG_FILE='mysqlbin.000166', MASTER_LOG_POS=798;

实践之事务性一直备份(备份期间数据库可写)

mysql> use  test

mysql> create table tt(id int,name varchar(12)) engine=innodb ;

Query OK, 0 rows affected (0.11 sec)

mysql> insert into tt values(1,'zz');

Query OK, 1 row affected (0.01 sec)

mysql> insert into tt values(2,'yy');

Query OK, 1 row affected (0.00 sec)


[root@test4 Desktop]# mysqldump  --databases test  --skip-opt --quick --extended-insert=false  --single-transaction   --master-data=2  -u root -p123456 >  /tmp/test.sql




本文转自陈仲阳0 51CTO博客,原文链接:http://blog.51cto.com/wolfword/1289596
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
344
分享
相关文章
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
在Ubuntu系统的Docker上安装MySQL的方法
以上的步骤就是在Ubuntu系统的Docker上安装MySQL的详细方法,希望对你有所帮助!
28 12
纯PHP+MySQL手搓高性能论坛系统!代码精简,拒绝臃肿
本内容分享了一套经实战验证的社交系统架构设计,支撑从1到100万用户的发展,并历经6次流量洪峰考验。架构涵盖客户端层(App、小程序、公众号)、接入层(API网关、负载均衡、CDN)、业务服务层(用户、内容、关系、消息等服务)、数据层(MySQL、Redis、MongoDB等)及运维监控层(日志、监控、告警)。核心设计包括数据库分库分表、多级缓存体系、消息队列削峰填谷、CQRS模式与热点数据动态缓存。同时提供应对流量洪峰的弹性伸缩方案及降级熔断机制,并通过Prometheus实现全链路监控。开源建议结构清晰,适合大型社交平台构建与优化。
58 11
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
120 14
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
【MySQL基础篇】全面学习总结SQL语法、DataGrip安装教程
本文详细介绍了MySQL中的SQL语法,包括数据定义(DDL)、数据操作(DML)、数据查询(DQL)和数据控制(DCL)四个主要部分。内容涵盖了创建、修改和删除数据库、表以及表字段的操作,以及通过图形化工具DataGrip进行数据库管理和查询。此外,还讲解了数据的增、删、改、查操作,以及查询语句的条件、聚合函数、分组、排序和分页等知识点。
【MySQL基础篇】全面学习总结SQL语法、DataGrip安装教程
Linux环境下MySQL数据库自动定时备份实践
数据库备份是确保数据安全的重要措施。在Linux环境下,实现MySQL数据库的自动定时备份可以通过多种方式完成。本文将介绍如何使用`cron`定时任务和`mysqldump`工具来实现MySQL数据库的每日自动备份。
362 3
Linux环境下MySQL数据库自动定时备份策略
在Linux环境下,MySQL数据库的自动定时备份是确保数据安全和可靠性的重要措施。通过设置定时任务,我们可以每天自动执行数据库备份,从而减少人为错误和提高数据恢复的效率。本文将详细介绍如何在Linux下实现MySQL数据库的自动定时备份。
202 3
Linux系统如何设置自启动服务在MySQL数据库启动后执行?
【10月更文挑战第25天】Linux系统如何设置自启动服务在MySQL数据库启动后执行?
331 3
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库

热门文章

最新文章

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等