mydumper备份MySQL数据库示例
mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具,备份方式为逻辑备份。它支持多线程,备份速度远高于原生态的mysql
mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具,备份方式为逻辑备份。它支持多线程,备份速度远高于原生态的mysqldump以及众多优异特性。因此该工具是DBA们的不二选择。本文主要描述该工具的使用方法并给出示例。
1、mydumper的特点
a、多线程逻辑备份,生产的多个备份文件
b、与mysqldump相同,,备份时对 MyISAM 表施加FTWRL (FLUSH TABLES WITH READ LOCK), 会阻塞DML 语句
c、保证备份数据的一致性
d、支持文件压缩,支持导出binlog,支持多线程恢复,支持将备份文件切块
e、支持以守护进程模式工作,定时快照和连续二进制日志
2、mydumper语法
mydumper -u [USER] -p [PASSWORD] -h [HOST] -P [PORT] -t [THREADS] -b -c -B [DB] -o [directory]
参数说明
-B, --database 需要备份的库
-T, --tables-list 需要备份的表,用逗号分隔
-o, --outputdir 输出文件的目录
-s, --statement-size 生成插入语句的字节数, 默认 1000000
-r, --rows 分裂成很多行块表
-c, --compress 压缩输出文件
-e, --build-empty-files 即使表没有数据,还是产生一个空文件
-x, --regex 正则表达式: 'db.table'
-i, --ignore-engines 忽略的存储引擎,用逗号分隔
-m, --no-schemas 不导出表结构
-k, --no-locks 不执行共享读锁 警告:这将导致不一致的备份
-l, --long-query-guard 设置长查询时间,默认60秒
--kill-long-queries kill掉长时间执行的查询
-b, --binlogs 导出binlog
-D, --daemon 启用守护进程模式
-I, --snapshot-interval dump快照间隔时间,默认60s,需要在daemon模式下
-L, --logfile 日志文件
-h, --host The host to connect to
-u, --user Username with privileges to run the dump
-p, --password User password
-P, --port TCP/IP port to connect to
-S, --socket UNIX domain socket file to use for connection
-t, --threads 使用的线程数,默认4
-C, --compress-protocol 在mysql连接上使用压缩协议
-V, --version Show the program version and exit
-v, --verbose 更多输出, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2
3、mydumper的工作过程
a、连接目标数据库;
b、通过show processlist来判断是否有长查询,根据参数long-query-guard和kill-long-queries决定退出或杀掉长查询;
c、锁定myisam表,flush tables with read lock;针对innodb table开启事务,start transaction;
d、创建worker子线程,缺省为4个;
e、确定候选表,根据类别分别插入innodb_table,non_innodb_table以及table_schemas链表(表结构);
f、将候选表通过g_async_queue_push加入任务队列(队列最后元素是thread shutdown),由worker子线程从队列中读取表信息并执行数据导出
g、执行unlock tables,处理完myisam表后立即解锁,以减少锁定时间;
h、等待worker退出;
4、备份文件相关信息
a、所有的备份文件在一个目录中,未指定时为当前目录, 且自动生成备份日期时间文件夹,如export-20150703-145806
b、目录中包含一个 metadata 文件,该文件记录了备份时间点的二进制日志文件名,日志的写入位置
c、如果是在从库进行备份,还会记录备份时同步至主库的二进制日志文件及写入位置
d、每个表有两个备份文件:database.table-schema.sql 表结构文件,database.table.sql 表数据文件
e、如果对表文件分片,将生成多个备份数据文件,可以指定行数或指定大小分片
5、常用备份示例:
###备份单个库
# mydumper -u leshami -p pwd -B sakila -o /tmp/bak
###备份所有数据库,全库备份期间除了information_schema与performance_schema之外的库都会被备份
# mydumper -u leshami -p pwd -o /tmp/bak
###备份单表
# mydumper -u leshami -p pwd -B sakila -T actor -o /tmp/bak
###备份多表
# mydumper -u leshami -p pwd -B sakila -T actor,city -o /tmp/bak
###当前目录自动生成备份日期时间文件夹,不指定-o参数及值时,如文件夹为:export-20150703-145806
mydumper -u leshami -p pwd -B sakila -T actor
###不带表结构备份表
# mydumper -u leshami -p pwd -B sakila -T actor -m
###压缩备份及连接使用压缩协议(非本地备份时)
# mydumper -u leshami -p pwd -B sakila -o /tmp/bak -c -C
###备份特定表
# mydumper -u leshami -p pwd -B sakila --regex=actor* -o /tmp/bak
###过滤特定库,如本来不备份mysql及test库
# mydumper -u leshami -p pwd -B sakila --regex '^(?!(mysql|test))' -o /tmp/bak
###基于空表产生表结构文件
# mydumper -u leshami -p pwd -B sakila -T actor -e -o /tmp/bak
##设置长查询的上限,如果存在比这个还长的查询则退出mydumper,也可以设置杀掉这个长查询
#mydumper -u leshami -p pwd -B sakila --long-query-guard 200 --kill-long-queries
###备份时输出详细日志
# mydumper -u leshami -p pwd -B sakila -T actor -v 3 -o /tmp/bak
###导出binlog,使用-b参数,会自动在导出目录生成binlog_snapshot文件夹及binlog
# mydumper -u leshami -p pwd -P 3306 -b -o /tmp/bak
本文永久更新链接地址:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)
