search
HomeDatabaseMysql Tutorialmydumper备份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

本文永久更新链接地址

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),