
概念描述
MySQL 8.0以后,对于MySQL DBA来说,一个不得不说的工具横空出世-MySQL Shell, 今天这里只讲MySQL Shell的 Utilities工具部分,且只讲MySQL Shell的导出导入功能,这个备份工具具有里程碑的意义,因为它强大到你可以安心放弃mysqldump了。
核心优势
1. 并行 Parallelization
默认4个线程进行导出或者导入数据,可根据情况自己定义:
util.dumpInstance(’/backup/mybackup’,{threads:8})
util.loadDump("/backup/mybackup",{loadUsers: true,threads:8})
2. 分块 Chunking
默认开启,将每个表数据分成大小相等的数据文件,默认64兆一个文件,由参数bytesPerChunk控制,最小128k(针对那些小表,小于64兆的表)。
chunking:true 默认开启,不用设置。
bytesPerChunk:“64M” 默认64M,不用设置。
3. 压缩 Compression
默认开启压缩,压缩类型为时下最新的zstd压缩,另外还支持gzip压缩,zstd在压缩和解压之间获得了一个折中的平衡,既让压缩率很高的情况下还获得了一个快速的解压时间,相对比gzip压缩率高的情况下还要比gzip解压时间更短。
compression:“zstd” 默认开启,不用设置。
4. 输出格式 Output format
MySQL Shell Utilities工具默认使用load data语句预期的默认格式进行数据导出和导入,这会让输出文件更小,同时导入速度也更加快速。
mysqldump,msqlpump,mydumper这三类导出工具都是以insert语句的形式导出数据, 这在速度上明显要落后于load data语句,官方文档中描述load data方式要比insert方式快20倍,具体描述请参考链接: https://dev.mysql.com/doc/refman/8.0/en/insert-optimization.html
5.优势总结:
我们之所以用MySQL Shell进行数据的导出导入和相关的备份工作,最大的好处就是:快速且节省空间!,MySQL Shell要比其他任何的逻辑导出工具都要快,压缩比也高,尤其适合我们空间不足,且需要快速进行数据备份的场景,一个更慢且没有压缩浪费空间(mysqldump),一个更快且压缩比高省空间,没有理由不使用MySQL Shell进行数据备份恢复工作。
使用举例
- util.dumpInstance(): 导出整个实例,包括用户
- util.dumpSchemas(): 导出部分数据库
- util.loadDump(): 将导出的数据文件导入到数据库中
导出示例:
#> cd /mysqldata/backup/mysqlshell8032/bin
#> ./mysqlsh --mysql -uroot -hlocalhost -C -S /data/mysql/run/mysql.sock
Creating a session to 'root@/data%2Fmysql%2F8030%2Frun%2Fmysql.sock'
Please provide the password for 'root@/data%2Fmysql%2F8030%2Frun%2Fmysql.sock': *********
Save password for 'root@/data%2Fmysql%2F8030%2Frun%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No): yes
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 8966
Server version: 8.0.30 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
MySQL localhost JS > util.dumpInstance('/mysqldata/backup/mybackup',{threads:8})
Acquiring global read lock
Global read lock acquired
Initializing - done
2 out of 6 schemas will be dumped and within them 6 tables, 0 views, 2 events, 2 routines.
3 out of 6 users will be dumped.
Gathering information - done
All transactions have been started
Locking instance for backup
Global read lock has been released
Writing global DDL files
Writing users DDL
Running data dump using 8 threads.
NOTE: Progress information uses estimated values and may not be accurate.
Writing schema metadata - done
Writing DDL - done
Writing table metadata - done
Starting data dump
101% (2.11M rows / ~2.08M rows), 962.38K rows/s, 186.39 MB/s uncompressed, 84.69 MB/s compressed
Dump duration: 00:00:02s
Total duration: 00:00:02s
Schemas dumped: 2
Tables dumped: 6
Uncompressed data size: 392.70 MB
Compressed data size: 176.44 MB
Compression ratio: 2.2
Rows written: 2113148
Bytes written: 176.44 MB
Average uncompressed throughput: 185.57 MB/s
Average compressed throughput: 83.38 MB/s
MySQL localhost JS > \q
Bye!
导入示例:
[root@dcm-attachdb1 bin]# ./mysqlsh --uri=root@localhost -S /mysqldata/run/mysql.sock
Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a Classic session to 'root@/mysqldata%2Frun%2Fmysql.sock'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 31
Server version: 5.7.39-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
No default schema selected; type \use <schema> to set one.
MySQL localhost JS > util.loadDump("/mysqldata/backup/mybackup",{resetProgress:true,loadUsers: true,threads:8})
Loading DDL, Data and Users from '/mysqldata/backup/bktest0307' using 8 threads.
Opening dump...
Target is MySQL 5.7.39-enterprise-commercial-advanced-log. Dump was produced from MySQL 5.7.39-enterprise-commercial-advanced-log
Scanning metadata - done
Checking for pre-existing objects...
Executing common preamble SQL
Executing DDL - done
Executing user accounts SQL...
NOTE: Skipping CREATE/ALTER USER statements for user 'root'@'localhost'
NOTE: Skipping GRANT statements for user 'root'@'localhost'
Executing view DDL - done
Starting data load
2 thds loading - 100% (37.75 GB / 37.75 GB), 8.64 MB/s, 54 / 54 tables done
Recreating indexes - done
Executing common postamble SQL
1034 chunks (169.16M rows, 37.75 GB) for 54 tables in 1 schemas were loaded in 38 min 12 sec (avg throughput 16.49 MB/s)
0 warnings were reported during the load.
MySQL localhost JS > \q
重点参数说明
-
dump参数
defaultCharacterSet:缺省字符集,默认为utf8mb4
where: 设置导出数据的条件,可以指定通过where条件来导出表的数据
ddlOnly: 仅仅导出表的ddl语句,默认为false
dataOnly: 仅仅导出数据,默认false
users: 导出用户,缺省为true, (Instance dump utility only)
excludeUsers: 排除用户,缺省为false, (Instance dump utility only)
includeUsers: 导出时指定包含的用户, (Instance dump utility only)
excludeSchemas: 导出时指定排除的DB, (Instance dump utility only)
includeSchemas: 导出时指定包含的DB, (Instance dump utility only)
excludeTables: 导出时指定排除的表, (Instance dump utility and schema dump utility only)
includeTables: 导出时指定包含的表, (Instance dump utility and schema dump utility only) -
load参数
excludeEvents: 忽略某些定时器的导入。
excludeRoutines:忽略某些函数和存储过程的导入。
excludeSchemas: 忽略某些库的导入。
excludeTables: 忽略某些表的导入。
excludeTriggers:忽略某些触发器的导入。
excludeUsers: 忽略某些账号的导入。
includeEvents: 导入指定定时器。
includeRoutines:导入指定函数和存储过程。
includeSchemas: 导入指定库。
includeTables: 导入指定表。
includeTriggers:导入指定触发器。
includeUsers: 导入指定账号。
loadData: 是否导入数据,默认为 true。
loadDdl: 是否导入 DDL 语句,默认为 true。
loadUsers: 是否导入账号,默认为 false。注意,即使将 loadUsers 设置为 true,也不会导入当前正在执行导入操作的用户。
ignoreExistingObjects: 是否忽略已经存在的对象,默认为 off。
backgroundThreads: 获取元数据和 DDL文件内容的线程数。备份集如果存储在本地,backgroundThreads 默认和 threads 一致。
threads: 并发线程数,默认为 4。
maxBytesPerTransaction:指定单个 LOAD DATA 操作可加载的最大字节数。默认与 bytesPerChunk 一致。这个参数可用来规避大事务。
progressFile: 在导入的过程中,会在备份目录生成一个progressFile,用于记录加载过程中的进度信息,这个进度信息可用来实现断点续传功能。默认为load-progress…progress。
resetProgress: 如果备份目录中存在progressFile,默认会从上次完成的地方继续执行。如果要从头开始执行,需将resetProgress 设置为 true。该参数默认为 off。
skipBinlog: 是否设置 sql_log_bin=0 ,默认 false。这一点与 mysqldump、mydumper 不同,后面这两个工具默认会禁用 Binlog。
updateGtidSet: 更新 GTID_PURGED。可设置:off(不更新,默认值), replace(替代目标实例的 GTID_PURGED), append(追加)。
使用限制
MySQL Shell工具目前在数据导出导入时,目标库的版本只适用于MySQL5.7和8.0版本,不适用低于5.7的版本。
参考文档
https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-utilities-dump-instance-schema.html




