使用mysqlpump进行MySQL数据库的逻辑备份与恢复

Posted 时栈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用mysqlpump进行MySQL数据库的逻辑备份与恢复相关的知识,希望对你有一定的参考价值。

目录

一、背景

二、简介

三、备份

1. 打开“命令提示符”

2. 执行命令(在命令行下)

2.1 如果有需要,请先切换到对应的目录下

2.2 输入命令,进行备份

2.3 其他的参数

四、恢复


一、背景

        因为数据库时常需要进行备份,而每次备份都需要对每个数据库进行相应的操作,耗费很多的时间和精力,后来偶然发现可以使用mysql数据库提供的逻辑备份工具mysqlpump(mysqldump也可以,不过mysqlpump会显示进度,个人感觉更好用一些)进行数据库的备份,这可以省去很多的时间。

二、简介

        以下内容源自mysql手册。
        mysqlpump程序执行逻辑备份。可以生成一组 SQL 语句,通过执行这些语句可以恢复原始数据库对象定义和表数据。

        其功能包括:

  • 并行处理数据库和数据库中的对象,以加快转储过程(相较于mysqldump)

  • 更好地控制要转储的数据库和数据库对象(表、存储的程序、用户帐户)

  • 将用户帐户转储为帐户管理语句(CREAT USER、GRANT),而不是插入到系统数据库mysql中

  • 创建压缩输出的能力

  • 进度指示器(值为估计值)

  • 对于转储文件重新加载,通过在插入行后添加索引来更快地为表创建二级索引InnoDB

        更多内容请参考:mysqlpump — A Database Backup Program

三、备份

1. 打开“命令提示符

        可以使用“Windows键 + R,输入cmd,点击回车”打开

        也可以以管理员权限打开命令提示符

2. 执行命令(在命令行下)

2.1 如果有需要,请先切换到对应的目录下

cd 对应的目录

2.2 输入命令,进行备份

如果对本地的数据库进行备份(可以省略IP),使用以下命令:
mysqlpump -u用户名 -p密码 --all-databases > 备份文件名.sql

如果对远程数据库进行备份,则需要提供IP信息:
mysqlpump -h你的IP地址 -u用户名 -p密码 --all-databases > 备份文件名.sql

        可以看到:

        注:如果提示找不到该命令,则需要切换到MySQL程序安装目录的bin文件夹下执行上述命令。

2.3 其他的参数

Option NameDescriptionIntroducedDeprecated
--add-drop-databaseAdd DROP DATABASE statement before each CREATE DATABASE statement
--add-drop-tableAdd DROP TABLE statement before each CREATE TABLE statement
--add-drop-userAdd DROP USER statement before each CREATE USER statement
--add-locksSurround each table dump with LOCK TABLES and UNLOCK TABLES statements
--all-databasesDump all databases
--bind-addressUse specified network interface to connect to MySQL Server
--character-sets-dirDirectory where character sets are installed
--column-statisticsWrite ANALYZE TABLE statements to generate statistics histograms
--complete-insertUse complete INSERT statements that include column names
--compressCompress all information sent between client and server8.0.18
--compress-outputOutput compression algorithm
--compression-algorithmsPermitted compression algorithms for connections to server8.0.18
--databasesInterpret all name arguments as database names
--debugWrite debugging log
--debug-checkPrint debugging information when program exits
--debug-infoPrint debugging information, memory, and CPU statistics when program exits
--default-authAuthentication plugin to use
--default-character-setSpecify default character set
--default-parallelismDefault number of threads for parallel processing
--defaults-extra-fileRead named option file in addition to usual option files
--defaults-fileRead only named option file
--defaults-group-suffixOption group suffix value
--defer-table-indexesFor reloading, defer index creation until after loading table rows
--eventsDump events from dumped databases
--exclude-databasesDatabases to exclude from dump
--exclude-eventsEvents to exclude from dump
--exclude-routinesRoutines to exclude from dump
--exclude-tablesTables to exclude from dump
--exclude-triggersTriggers to exclude from dump
--exclude-usersUsers to exclude from dump
--extended-insertUse multiple-row INSERT syntax
--get-server-public-keyRequest RSA public key from server
--helpDisplay help message and exit
--hex-blobDump binary columns using hexadecimal notation
--hostHost on which MySQL server is located
--include-databasesDatabases to include in dump
--include-eventsEvents to include in dump
--include-routinesRoutines to include in dump
--include-tablesTables to include in dump
--include-triggersTriggers to include in dump
--include-usersUsers to include in dump
--insert-ignoreWrite INSERT IGNORE rather than INSERT statements
--log-error-fileAppend warnings and errors to named file
--login-pathRead login path options from .mylogin.cnf
--max-allowed-packetMaximum packet length to send to or receive from server
--net-buffer-lengthBuffer size for TCP/IP and socket communication
--no-create-dbDo not write CREATE DATABASE statements
--no-create-infoDo not write CREATE TABLE statements that re-create each dumped table
--no-defaultsRead no option files
--parallel-schemasSpecify schema-processing parallelism
--passwordPassword to use when connecting to server
--password1First multifactor authentication password to use when connecting to server8.0.27
--password2Second multifactor authentication password to use when connecting to server8.0.27
--password3Third multifactor authentication password to use when connecting to server8.0.27
--plugin-dirDirectory where plugins are installed
--portTCP/IP port number for connection
--print-defaultsPrint default options
--protocolTransport protocol to use
--replaceWrite REPLACE statements rather than INSERT statements
--result-fileDirect output to a given file
--routinesDump stored routines (procedures and functions) from dumped databases
--server-public-key-pathPath name to file containing RSA public key
--set-charsetAdd SET NAMES default_character_set to output
--set-gtid-purgedWhether to add SET @@GLOBAL.GTID_PURGED to output
--single-transactionDump tables within single transaction
--skip-definerOmit DEFINER and SQL SECURITY clauses from view and stored program CREATE statements
--skip-dump-rowsDo not dump table rows
--skip-generated-invisible-primary-keyDo not dump information about generated invisible primary keys8.0.30
--socketUnix socket file or Windows named pipe to use
--ssl-caFile that contains list of trusted SSL Certificate Authorities
--ssl-capathDirectory that contains trusted SSL Certificate Authority certificate files
--ssl-certFile that contains X.509 certificate
--ssl-cipherPermissible ciphers for connection encryption
--ssl-crlFile that contains certificate revocation lists
--ssl-crlpathDirectory that contains certificate revocation-list files
--ssl-fips-modeWhether to enable FIPS mode on client side
--ssl-keyFile that contains X.509 key
--ssl-modeDesired security state of connection to server
--ssl-session-dataFile that contains SSL session data8.0.29
--ssl-session-data-continue-on-failed-reuseWhether to establish connections if session reuse fails8.0.29
--tls-ciphersuitesPermissible TLSv1.3 ciphersuites for encrypted connections8.0.16
--tls-versionPermissible TLS protocols for encrypted connections
--triggersDump triggers for each dumped table
--tz-utcAdd SET TIME_ZONE='+00:00' to dump file
--userMySQL user name to use when connecting to server
--usersDump user accounts
--versionDisplay version information and exit
--watch-progressDisplay progress indicator
--zstd-compression-levelCompression level for connections to server that use zstd compression8.0.18

比如我需要备份“以a开头”和“以BC开头”的数据库,文件存储在bakup.sql中。同时,我希望先清空所有“以a开头”和“以BC开头”的数据库之后再把备份的内容写入到数据库,可以这么写:

mysqlpump -u用户名 -p密码 --add-drop-database --add-drop-table --include-databases=a%,BC% > bakup.sql

注:
--add-drop-database 在每个创建数据库语句之前添加 DROP DATABASE 语句
--add-drop-table 在每个 CREATE TABLE 语句之前添加 DROP TABLE 语句
--include-databases=a%,BC% 只对“以a开头”和“以BC开头”的数据库进行备份

         更多内容请参考:mysqlpump — A Database Backup Program

四、恢复

        删除上面的所有用于测试的数据库,观察是否能够恢复:

mysql -u用户 -p -f < 备份文件名称.sql

注:-f意为遇到错误仍然继续

 最后可以发现,把数据库都删除之后,数据库被成功复原。

如有不当或错误之处,恳请您的指正,谢谢!!!

以上是关于使用mysqlpump进行MySQL数据库的逻辑备份与恢复的主要内容,如果未能解决你的问题,请参考以下文章

mysql5.7逻辑备份工具mysqlpump

MySQL数据的导入与导出

MySQL逻辑物理备份测试

MySQL5.7.18 备份Mysqldump,mysqlpump,xtrabackup,innobackupex 全量,增量备份,数据导入导出

mysqldump 逻辑备份搭建备库

mysqldump 逻辑备份搭建备库