Mysql 5.7
之后多了一个备份工具,
mysqlpump
mysqlpump客户端 执行的是
逻辑备份
Mysqlpump
新特性
并行执行 数据库和其中的对象,加快转储过程
更好的控制哪些数据库和数据库对象 来转储导出
导出用户账号
作为账号管理语句(
create user
,
grant
),而不是插入到mysql系统数据库中
备份出来直接生成压缩备份文件
备份进度指标(估计值
)
转储文件加载(还原),先建表后插入数据,最后建立索引,减少索引的维护开销,加快还原速度
.
备份可以排除或指定数据库
mysqlpump
需要的
权限,
不同的选项,需要不同的权限,可以在选项说明中查看。
功能 |
至少需要的权限 |
导出表 |
select 对应表权限 |
导出视图 |
show view 对应视图权限 |
导出存储过程 |
trigger 对应存储过程权限 |
|
|
导出用户定义 |
Select mysql 系统库的权限 |
加载dump文件,必须有执行 dump文件所包含的语句 的权限,如 create 等。
NOTE
在windows上使用powershell 导出时,重定向到一个新建文件,会使用 utf-16 编码,这会导致错误,因为MySQL 连接字符集不支持utf-16 .
shell>
mysqlpump
[
options
]
> dump
.
sql
错误
可以使用
–result
-file
选项,来输出到
ASCII
格式的文件上。
shell>
mysqlpump
[
options
]
–result-file
=
dump.sql
正确
Mysqldump 调用语法
shell>
mysqlpump
–all-databases
shell>
mysqlpump
db_name
shell>
mysqlpump
db_name tbl_name1 tbl_name2
…
导出指定的多个库
shell>
mysqlpump
–databases
db_name1 db_name2
…
默认情况,
mysql
p
ump
不导出用户账户定义,即使你导出含有授权表的
mysql
系统库。要以逻辑定义(
create user
和
grant
)形式导出授权表,使用
–users
选项
并且禁止所有数据库转储。
shell>
mysqlpump
–exclude-databases
=
%
–users
这里的
%
是个通配符,他匹配所有的库,
–exclude-database=%
即排除所有的库
Mysqlpump 支持几个选项,包含或排除数据库、表、存储过程、用户定义。看
mysqlpump object selection
,
要加载转储文件,执行它包含的语句,如下:
shell>
mysqlpump
[
options
]
> dump
.
sql
shell>
mysql
< dump
.
sql
Mysqlpump 选项概要
mysqlpump
支持命令行指定选项,也可以在参数文件的
[mysqlpump]
and
[client]
的组中指定。看
Section 4.2.6, “Using Option Files”
.
Table 4.15 mysqlpump Options
参数绝大多数和
mysqldump
一致
,
对于
mysqlpump
参数会用背景色
标记出来。
|
|
|
|
Add DROP DATABASE statement before each CREATE DATABASE statement 在建库之前,先执行删除库操作
|
|
|
Add DROP TABLE statement before each CREATE TABLE statement 在建表之前先执行删表操作。
|
|
|
Add DROP USER statement before each CREATE USER statement
|
|
|
Surround each table dump with LOCK TABLES and UNLOCK TABLES statements
LOCK TABLES `…`.`…` WRITE; |
|
|
备份所有库,-A。 |
|
|
Use specified network interface to connect to MySQL Server
|
|
|
Directory where character sets are installed
|
|
|
Use complete INSERT statements that include column names
|
|
|
Compress all information sent between client and server 在客户端和服务器传输的所有的数据包压缩,最后的备份集大小没有任何改变,-C。 若要改变备份集大小: compress=true |gzip
会几倍于 –compress-output呢? |
|
|
Output compression algorithm
shell> mysqlpump –compress-output=LZ4 > dump.lz4
shell> mysqlpump –compress-output=ZLIB > dump.zlib |
|
|
Interpret all name arguments as database names 手动指定要备份的库,支持多个数据库,用空格分隔,-B。 |
|
|
Write debugging log |
|
|
Print debugging information when program exits |
|
|
Print debugging information, memory, and CPU statistics when program exits |
|
|
Authentication plugin to use |
|
|
Specify default character set 指定备份的字符集。 |
|
|
Default number of threads for parallel processing
|
|
|
Read named option file in addition to usual option files |
|
|
Read only named option file |
|
|
Option group suffix value |
|
|
For reloading, defer index creation until after loading table rows
|
|
|
Dump events from dumped databases 备份数据库的事件,默认开启,关闭使用–skip-events参数。 |
|
|
Databases to exclude from dump
|
|
|
Events to exclude from dump |
|
|
Routines to exclude from dump |
|
|
Tables to exclude from dump |
|
|
Triggers to exclude from dump |
|
|
Users to exclude from dump |
|
|
Use multiple-row INSERT syntax |
|
|
Request RSA public key from server |
5.7.23 |
|
Display help message and exit |
|
|
Dump binary columns using hexadecimal notation 备份binary字段的时候使用十六进制计数法,受影响的字段类型有BINARY、VARBINARY、BLOB、BIT。 |
|
|
Host to connect to (IP address or hostname)
|
|
|
Databases to include in dump
|
|
|
Events to include in dump |
|
|
Routines to include in dump |
|
|
Tables to include in dump |
|
|
Triggers to include in dump |
|
|
Users to include in dump |
|
|
Write INSERT IGNORE rather than INSERT statements 备份用insert ignore语句代替insert语句。 |
|
|
Append warnings and errors to named file
|
|
|
|
|
|
Maximum packet length to send to or receive from server 备份时用于client/server直接通信的最大buffer包的大小。 |
|
|
Buffer size for TCP/IP and socket communication
|
|
|
Do not write CREATE DATABASE statements 备份不写CREATE DATABASE语句。要是备份多个库,需要使用参数-B,而使用-B的时候会出现create database语句,该参数可以屏蔽create database 语句。 |
|
|
Do not write CREATE TABLE statements that re-create each dumped table
|
|
|
Read no option files |
|
|
Specify schema-processing parallelism
|
|
|
Password to use when connecting to server |
|
|
Directory where plugins are installed |
|
|
TCP/IP port number for connection |
|
|
Print default options |
|
|
Connection protocol to use
|
|
|
Write REPLACE statements rather than INSERT statements
|
|
|
Direct output to a given file |
|
|
Dump stored routines (procedures and functions) from dumped databases
|
|
|
Do not send passwords to server in old (pre-4.1) format |
|
|
Path name to file containing RSA public key |
5.7.23 |
|
Add SET NAMES default_character_set to output 备份文件里写SET NAMES default_character_set 到输出,此参默认开启。 — skip-set-charset禁用此参数,不会在备份文件里面写出set names… |
|
|
Whether to add SET @@GLOBAL.GTID_PURGED to output |
5.7.18 |
|
Dump tables within single transaction
|
|
|
Omit DEFINER and SQL SECURITY clauses from view and stored program CREATE statements 忽略那些创建视图和存储过程用到的 DEFINER 和 SQL SECURITY 语句,恢复的时候,会使用默认值,否则会在还原的时候看到没有DEFINER定义时的账号而报错。 |
|
|
Do not dump table rows
|
|
|
For connections to localhost, the Unix socket file to use |
|
|
Enable encrypted connection
|
|
|
File that contains list of trusted SSL Certificate Authorities |
|
|
Directory that contains trusted SSL Certificate Authority certificate files |
|
|
File that contains X.509 certificate |
|
|
List of permitted ciphers for connection encryption |
|
|
File that contains certificate revocation lists |
|
|
Directory that contains certificate revocation list files |
|
|
File that contains X.509 key |
|
|
Security state of connection to server |
5.7.11 |
|
Verify host name against server certificate Common Name identity |
|
|
Protocols permitted for encrypted connections |
5.7.10 |
|
Dump triggers for each dumped table
|
|
|
Add SET TIME_ZONE='+00:00' to dump file |
|
|
-u |
|
|
Dump user accounts
|
|
|
Display version information and exit |
5.7.9 |
|
Display progress indicator
|
不支持的参数
–flush-logs –flush-privileges
看来
5.7
不需要导出时做这些动作了
–master-data
–dump-slave
没有这个怎么搭建从库呢
使用说明:
mysqlpump的架构如下图所示
:
mysqlpump支持基于库和表的并行导出,mysqlpump的并行导出功能的架构为:队列+线程,允许有多个队列(
–parallel-schemas
?),每个队列下有多个线程(N?),而一个队列可以绑定1个或者多个数据库(逗号分隔)。
mysqlpump的备份是基于表并行的,对于每张表的导出只能是单个线程的
,这里会有个限制是如果某个数据库有一张表非常大,可能大部分的时间都是消耗在这个表的备份上面,并行备份的效果可能就不明显。这里可以利用
mydumper
其是以chunk的方式批量导出,即
mydumper支持一张表多个线程以chunk的方式批量导出
。但是相对于mysqldump还是有了很大的提升。这里大致测试下mysqlpump和mysqldump的备份效率。
#mysqlpump
压缩备份
vs
数据库 三个并发线程备份,消耗时间:
222s
mysqlpump -uzjy -p -h292.168.123.70 –single-transaction –default-character-set=utf8 –compress-output=LZ4 –default-parallelism=3 -B vs > /home/zhoujy/vs_db.sql.lz4
#mysqldump
备份压缩
vs
数据库 单个线程备份,消耗时间:
900s
,
gzip
的压缩率比
LZ4
的高
mysqldump -uzjy -p -h292.168.123.70 –default-character-set=utf8 -P3306 –skip-opt –add-drop-table –create-options –quick –extended-insert –single-transaction -B vs | gzip > /home/zhoujy/vs.sql.gz
#mydumper
备份
vs
数据库 三个并发线程备份,消耗时间:
300s
,
gzip
的压缩率比
LZ4
的高
mydumper -u zjy -p -h 192.168.123.70 -P 3306 -t 3 -c -l 3600 -s 10000000 -B vs -o /home/zhoujy/vs/
#mydumper
备份
vs
数据库,五个并发线程备份,并且开启对一张表多个线程以
chunk
的方式批量导出,
-r
。消耗时间:
180s
mydumper -u zjy -p -h 192.168.123.70 -P 3306 -t 5 -c -r 300000 -l 3600 -s 10000000 -B vs -o /home/zhoujy/vs/
从上面看出,mysqlpump的备份效率是最快的,mydumper次之,mysqldump最差。所以在IO允许的情况下,能用多线程就别用单线程备份。